關於用C 整TIC TAC TOE
本帖最后由 GSL3012 于 2011-05-07 18:25 编辑
- #include <stdio.h>
- #include <time.h>
- #define MYTRUE 1
- #define MYFALSE 0
- int win(int data[4][4], int side)
- {
- int loop01, loop02,loop03;
- int win=MYTRUE;
- for(loop01=0; loop01<3; loop01++)
- {
- for(loop02=0, win=MYTRUE; loop02<3 && win; loop02++)
- if(data[loop01][loop02] != side)
- win=MYFALSE;
- if(win)
- return(win);
- for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
- if(data[loop02][loop01] != side)
- win=MYFALSE;
- if(win)
- return(win);
- for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
- if(data[loop02][loop02] != side)
- win=MYFALSE;
- if(win)
- return(win);
- for(loop02=0, win= MYTRUE; loop02<3 && win; loop02++)
- if(data[loop02-1][loop02+1] != side)
- win=MYFALSE;
- if(win)
- return(win);
- }
- return(win);
- }
- int main()
- {
- int data[4][4] = { {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1},};
- int move=0;
- int loop01, loop02,loop03;
- int x0, x1, y0, y1;
- char repeat;
- do{
- srand(time(0));
- while(MYTRUE)
- {
- printf("Please enter your move: ");
- scanf("%d%d", &x0, &y0);
- fflush(stdin);
- data[y0][x0]=0;
- if(win(data, 0))
- {
- printf("User won the game!!\n");
- break;
- }
- while(1)
- {
- x1 = rand()%3;
- y1 = rand()%3;
- if(data[y1][x1]==-1)
- break;
- }
- printf("Computer Move: %d %d\n", x1, y1);
- data[y1][x1]=1;
- if(win(data, 1))
- {
- printf("Computer won the game!!\n");
- break;
- }
- }
- //game over display TTT
- //printf("%d %d %d\n", data[0][0], data[0][1], data[0][2]);
- //printf("%d %d %d\n", data[1][0], data[1][1], data[1][2]);
- //printf("%d %d %d\n", data[2][0], data[2][1], data[2][2]);
- /*for(loop03=0;loop03<data[4][4];loop03++){
- if(loop03 != -1)
- printf("%d",1);}*/
- printf("Start another new game?");
- repeat=getchar();
- }while(repeat=='y'||repeat=='Y');
- }
复制代码請問大家..我狂REPEAT時...
之後打左Y,之後入了座標
但就說USER WIN了...為何會這樣?
我巳經用了FFLUSH(STDIN);清除KEYBOARD BUFFER
小弟是一名正在學C的新手
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
回复 1# GSL3012
刚好我也写过一次,不知道对你是否有参考价值
http://blog168.chinaunix.net/space.php?uid=23387202&do=blog&id=129522
还有fflush(stdin)是未定义行为
不好意思...我不太明白
請問如果要定義是如何定義的?
http://bbs.chinaunix.net/thread-2305110-14-1.html
133楼
本帖最后由 GSL3012 于 2011-05-16 02:37 编辑
請問如果寫電腦AI如何入手?,小弟不知道如何入手
使用FOR...不知如何CHECK USER何時WIN,可以提點一下小弟?
我巳解決不能重複的問題~