CARI Infonet

 Forgot password?
 Register

ADVERTISEMENT

View: 2499|Reply: 6

Camner nak wat game tic tac toe pki C??

[Copy link]
Post time 29-9-2006 10:40 AM | Show all posts |Read mode
aku nak mintak tlg kat sesaper yg tau camnetr nak wat game tic tac toe pki C...

dis is 4 my project...bg tau camner nak wat pun leh gak...laie bes kalo korang bg jer coding dia...huhuhu

apa2 pun tq in advance yaa...
Reply

Use magic Report


ADVERTISEMENT


Post time 29-9-2006 11:06 AM | Show all posts
hmmm... assignment yer :bgrin:
dah cuba buat sendiri ke belum?
Reply

Use magic Report

Post time 29-9-2006 11:11 AM | Show all posts

Dah Google ke belum?

bersepah-sepah coding untuk tic-tac-toe ada kat internet.
saya buat simple google search je dah dapat source code.

:hmm:
Reply

Use magic Report

 Author| Post time 29-9-2006 03:37 PM | Show all posts
wat nak panduan jer...yg tgk dr byk sumber..ader jumpa kat buku tp meleret sgt coding dia...
Reply

Use magic Report

Post time 29-9-2006 03:59 PM | Show all posts
no difference if you ask here or search thru the net.

hmmmmm... takpe yang ni saya bagi free. next time try usaha sendiri. :jeling:

try and copy/paste this code.


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>

  4. int board[10] = {2,2,2,2,2,2,2,2,2,2};
  5. int turn = 1,flag = 0;
  6. int player,comp;

  7. void menu();
  8. void go(int n);
  9. void start_game();
  10. void check_draw();
  11. void draw_board();
  12. void player_first();
  13. void put_X_O(char ch,int pos);


  14. main()
  15. {
  16.         clrscr();
  17.         _setcursortype(_NOCURSOR);
  18.         menu();
  19.         getch();
  20.         return(0);
  21. }

  22. void menu()
  23. {
  24.         int choice;
  25.         printf("\n--------MENU--------");
  26.         printf("\n1 : Play with X");
  27.         printf("\n2 : Play with O");
  28.         printf("\n3 : Exit");
  29.         printf("\nEnter your choice:>");
  30.         scanf("%d",&choice);

  31.         turn = 1;
  32.         switch (choice)
  33.         {
  34.                 case 1:
  35.                         player = 1;
  36.                         comp = 0;
  37.                         player_first();
  38.                         break;
  39.                 case 2:
  40.                         player = 0;
  41.                         comp = 1;
  42.                         start_game();
  43.                         break;
  44.                 case 3:
  45.                         exit(0);
  46.                 default:
  47.                         menu();
  48.         }
  49. }

  50. int make2()
  51. {
  52.         if(board[5] == 2)
  53.                 return 5;
  54.         if(board[2] == 2)
  55.                 return 2;
  56.         if(board[4] == 2)
  57.                 return 4;
  58.         if(board[6] == 2)
  59.                 return 6;
  60.         if(board[8] == 2)
  61.                 return 8;
  62.         return 0;
  63. }

  64. int make4()
  65. {
  66.         if(board[1] == 2)
  67.                 return 1;
  68.         if(board[3] == 2)
  69.                 return 3;
  70.         if(board[7] == 2)
  71.                 return 7;
  72.         if(board[9] == 2)
  73.                 return 9;
  74.         return 0;
  75. }

  76. int posswin(int p)
  77. {
  78. // p==1 then X   p==0  then  O
  79.         int i;
  80.         int check_val,pos;

  81.         if(p == 1)
  82.                 check_val = 18;
  83.         else
  84.                 check_val = 50;

  85.         i = 1;
  86.         while(i<=9)//row check
  87.         {
  88.                 if(board[i] * board[i+1] * board[i+2] == check_val)
  89.                 {
  90.                         if(board[i] == 2)
  91.                                 return i;
  92.                         if(board[i+1] == 2)
  93.                                 return i+1;
  94.                         if(board[i+2] == 2)
  95.                                 return i+2;
  96.                 }
  97.                 i+=3;
  98.         }

  99.         i = 1;
  100.         while(i<=3)//column check
  101.         {
  102.                 if(board[i] * board[i+3] * board[i+6] == check_val)
  103.                 {
  104.                         if(board[i] == 2)
  105.                                 return i;
  106.                         if(board[i+3] == 2)
  107.                                 return i+3;
  108.                         if(board[i+6] == 2)
  109.                                 return i+6;
  110.                 }
  111.                 i++;
  112.         }

  113.         if(board[1] * board[5] * board[9] == check_val)
  114.         {
  115.                 if(board[1] == 2)
  116.                         return 1;
  117.                 if(board[5] == 2)
  118.                         return 5;
  119.                 if(board[9] == 2)
  120.                         return 9;
  121.         }

  122.         if(board[3] * board[5] * board[7] == check_val)
  123.         {
  124.                 if(board[3] == 2)
  125.                         return 3;
  126.                 if(board[5] == 2)
  127.                         return 5;
  128.                 if(board[7] == 2)
  129.                         return 7;
  130.         }
  131.         return 0;
  132. }

  133. void go(int n)
  134. {
  135.         if(turn % 2)
  136.                 board[n] = 3;
  137.         else
  138.                 board[n] = 5;
  139.         turn++;
  140. }

  141. void player_first()
  142. {
  143.         int pos;

  144.         check_draw();
  145.         draw_board();
  146.         gotoxy(30,18);
  147.         printf("Your Turn :> ");
  148.         scanf("%d",&pos);

  149.         if(board[pos] != 2)
  150.                 player_first();

  151.         if(pos == posswin(player))
  152.         {
  153.                 go(pos);
  154.                 draw_board();
  155.                 gotoxy(30,20);
  156.                 textcolor(128+RED);
  157.                 cprintf("Player Wins");
  158.                 getch();
  159.                 exit(0);
  160.         }

  161.         go(pos);
  162.         draw_board();
  163.         start_game();
  164. }

  165. void start_game()
  166. {
  167.         // p==1 then X   p==0  then  O
  168.         if(posswin(comp))
  169.         {
  170.                 go(posswin(comp));
  171.                 flag = 1;
  172.         }
  173.         else
  174.         if(posswin(player))
  175.                 go(posswin(player));
  176.         else
  177.         if(make2())
  178.                 go(make2());
  179.         else
  180.                 go(make4());
  181.         draw_board();

  182.         if(flag)
  183.         {
  184.                 gotoxy(30,20);
  185.                 textcolor(128+RED);
  186.                 cprintf("Computer wins");
  187.                 getch();
  188.         }
  189.         else
  190.                 player_first();
  191. }

  192. void check_draw()
  193. {
  194.         if(turn > 9)
  195.         {
  196.                 gotoxy(30,20);
  197.                 textcolor(128+RED);
  198.                 cprintf("Game Draw");
  199.                 getch();
  200.                 exit(0);
  201.         }
  202. }

  203. void draw_board()
  204. {
  205.         int j;

  206.         for(j=9;j<17;j++)
  207.         {
  208.                 gotoxy(35,j);
  209.                 printf("|       |");
  210.         }
  211.         gotoxy(28,11);
  212.         printf("-----------------------");
  213.         gotoxy(28,14);
  214.         printf("-----------------------");

  215.         for(j=1;j<10;j++)
  216.         {
  217.                 if(board[j] == 3)
  218.                         put_X_O('X',j);
  219.                 else
  220.                 if(board[j] == 5)
  221.                         put_X_O('O',j);
  222.         }
  223. }

  224. void put_X_O(char ch,int pos)
  225. {
  226.         int m;
  227.         int x = 31, y = 10;

  228.         m = pos;

  229.         if(m > 3)
  230.         {
  231.                 while(m > 3)
  232.                 {
  233.                         y += 3;
  234.                         m -= 3;
  235.                 }
  236.         }
  237.         if(pos % 3 == 0)
  238.                 x += 16;
  239.         else
  240.         {
  241.                 pos %= 3;
  242.                 pos--;
  243.                 while(pos)
  244.                 {
  245.                         x+=8;
  246.                         pos--;
  247.                 }
  248.         }
  249.         gotoxy(x,y);
  250.         printf("%c",ch);
  251. }
Copy the Code
Reply

Use magic Report

Post time 29-9-2006 04:37 PM | Show all posts
Kebanyakan coding sememangnya meleret... leretannya kadang2 menyebabkan otak kita meleret-leret...hehehe.
Reply

Use magic Report

Follow Us
 Author| Post time 1-10-2006 09:56 AM | Show all posts
Originally posted by shahnazz at 29-9-2006 03:59 PM
no difference if you ask here or search thru the net.

hmmmmm... takpe yang ni saya bagi free. next time try usaha sendiri. :jeling:

try and copy/paste this code.


#include<stdio.h>
...



tq 4 da code...nak crk reference byk2 so leh la compare utk create sumthing new.....bukan la nak mintak pasal nak copy paste bulat2 n then trus antar but nak tgk camner dia wat jer...dh la lemah programming2 nie....

minat2 mmg minat tp susah laa nak xplain...
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

 

ADVERTISEMENT


Forum Hot Topic

 

ADVERTISEMENT


 


ADVERTISEMENT
Follow Us

ADVERTISEMENT


Mobile|Archiver|Mobile*default|About Us|CariDotMy

15-5-2024 08:00 AM GMT+8 , Processed in 0.175069 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

Quick Reply To Top Return to the list