井字游戏
这是我刚写的井字游戏,没写注释,如果有意见或问题可以跟贴由于我用的是linux所以倒数第五行的清空缓冲区的那个函数在windows中不一定可用,注意一下。。。
程序代码:
#include<stdio.h> #include<ctype.h> int main(void) { int winner,step,row,column,turn; char a[3][3],newgame; printf(" 1 | 2 | 3 \n"); printf("---+---+---\n"); printf(" 4 | 5 | 6 \n"); printf("---+---+---\n"); printf(" 7 | 8 | 9 \n"); printf("游戏要开始了,先连成三个(横竖斜均可)的人胜利\n" "A是X,B是O\n\n"); while(1){ char a[3][3]={'1','2','3','4','5','6','7','8','9'}; turn=0;winner=0; do { turn++; printf("It's %c's turn.Please enter a number\n",turn%2?'A':'B'); scanf("%d",&step); step-=1; column=step%3; row=step/3; a[row][column]=turn%2?'X':'O'; printf(" %c | %c | %c \n",a[0][0],a[0][1],a[0][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",a[1][0],a[1][1],a[1][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",a[2][0],a[2][1],a[2][2]); if(a[0][0]==a[0][1]&&a[0][0]==a[0][2]) winner=turn%2?1:2; else if(a[1][0]==a[1][1]&&a[1][1]==a[1][2]) winner=turn%2?1:2; else if(a[2][0]==a[2][1]&&a[2][1]==a[2][2]) winner=turn%2?1:2; else if(a[0][0]==a[1][0]&&a[2][0]==a[1][0]) winner=turn%2?1:2; else if(a[0][1]==a[1][1]&&a[1][1]==a[2][1]) winner=turn%2?1:2; else if(a[0][2]==a[1][2]&&a[1][2]==a[2][2]) winner=turn%2?1:2; else if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]) winner=turn%2?1:2; else if(a[0][2]==a[1][1]&&a[1][1]==a[2][0]) winner=turn%2?1:2; if(turn==9) break; } while(winner==0); if(winner!=0) printf("winner is %c\n",winner==1?'A':'B'); else printf("和\n"); printf("wanna another game?\n"); setbuf(stdin,NULL); scanf("%c",&newgame); if(tolower(newgame)=='n') break; }}
[此贴子已经被作者于2016-7-21 07:44编辑过]