關於用C 整TIC TAC TOE

发布于 2022-10-15 07:38:37 字数 4684 浏览 16 评论 0

本帖最后由 GSL3012 于 2011-05-07 18:25 编辑

  1. #include <stdio.h>
  2. #include <time.h>
  3. #define MYTRUE 1
  4. #define MYFALSE 0
  5. int win(int data[4][4], int side)
  6. {
  7.   int loop01, loop02,loop03;
  8.   int win=MYTRUE;
  9.     for(loop01=0; loop01<3; loop01++)
  10.     {
  11.       for(loop02=0, win=MYTRUE; loop02<3 && win; loop02++)
  12.         if(data[loop01][loop02] != side)
  13.           win=MYFALSE;
  14.       if(win)
  15.         return(win);
  16.       for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
  17.         if(data[loop02][loop01] != side)
  18.           win=MYFALSE;
  19.       if(win)
  20.         return(win);
  21.                
  22.           for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
  23.         if(data[loop02][loop02] != side)
  24.           win=MYFALSE;
  25.       if(win)
  26.         return(win);
  27.                
  28.           for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
  29.         if(data[loop02-1][loop02+1] != side)
  30.           win=MYFALSE;
  31.       if(win)
  32.         return(win);
  33.     }
  34.     return(win);
  35. }
  36. int main()
  37. {
  38.   int data[4][4] = { {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1},};
  39.   int move=0;
  40.   int loop01, loop02,loop03;
  41.   int x0, x1, y0, y1;
  42.   char repeat;
  43. do{   
  44.   srand(time(0));
  45.   while(MYTRUE)
  46.   {
  47.     printf("Please enter your move: ");
  48.     scanf("%d%d", &x0, &y0);
  49.     fflush(stdin);
  50.     data[y0][x0]=0;
  51.     if(win(data, 0))
  52.     {
  53.       printf("User won the game!!\n");
  54.       break;
  55.     }
  56.     while(1)
  57.     {
  58.       x1 = rand()%3;
  59.       y1 = rand()%3;
  60.       if(data[y1][x1]==-1)
  61.         break;
  62.     }
  63.     printf("Computer Move: %d %d\n", x1, y1);
  64.     data[y1][x1]=1;
  65.     if(win(data, 1))
  66.     {
  67.       printf("Computer won the game!!\n");
  68.       break;
  69.     }
  70.   }
  71.         //game over display TTT
  72.          //printf("%d   %d   %d\n", data[0][0], data[0][1], data[0][2]);
  73.          //printf("%d   %d   %d\n", data[1][0], data[1][1], data[1][2]);
  74.          //printf("%d   %d   %d\n", data[2][0], data[2][1], data[2][2]);
  75.         /*for(loop03=0;loop03<data[4][4];loop03++){
  76.         if(loop03 != -1)
  77.                 printf("%d",1);}*/
  78.    
  79.    
  80.     printf("Start another new game?");
  81.         repeat=getchar();
  82. }while(repeat=='y'||repeat=='Y');
  83. }

复制代码請問大家..我狂REPEAT時...
之後打左Y,之後入了座標
但就說USER WIN了...為何會這樣?
我巳經用了FFLUSH(STDIN);清除KEYBOARD BUFFER
小弟是一名正在學C的新手

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红焚 2022-10-22 07:38:37

回复 1# GSL3012

    刚好我也写过一次,不知道对你是否有参考价值

    http://blog168.chinaunix.net/space.php?uid=23387202&do=blog&id=129522

    还有fflush(stdin)是未定义行为

葵雨 2022-10-22 07:38:37

不好意思...我不太明白
請問如果要定義是如何定義的?

故人爱我别走 2022-10-22 07:38:37

本帖最后由 GSL3012 于 2011-05-16 02:37 编辑

請問如果寫電腦AI如何入手?,小弟不知道如何入手
使用FOR...不知如何CHECK USER何時WIN,可以提點一下小弟?
我巳解決不能重複的問題~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文