写了个井字游戏,大家可以给些意见
程序代码:
#include<stdio.h> int main() { char chess[3][3]={'1','2','3','4','5','6','7','8','9',}; int player1=0,player2=0,winner=0,column,line,turn=0; int check(char chess[3][3]); printf("Game start!\nplayer1 *\nplayer2 O\n\n"); printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]); while(1) { printf("player1's turn:"); scanf("%d",&player1); if(!(player1>=1)&&(player1<=9)) { printf("input error\n"); return 0; } turn++; column=(int)(player1-1)/3; line=(int)(player1-1)%3; chess[column][line]='*'; printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]); if(check(chess)==1) { printf("player1 is the winner\n"); return 1; } if(turn==9) { printf("onbody win\n"); return 1; } printf("player2's turn:"); scanf("%d",&player2); if(!(player2>=1)&&(player2<=9)) { printf("input error\n"); return 0; } column=(int)(player2-1)/3; line=(int)(player2-1)%3; chess[column][line]='O'; turn++; printf(" %c | %c | %c \n",chess[0][0],chess[0][1],chess[0][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[1][0],chess[1][1],chess[1][2]); printf("---+---+---\n"); printf(" %c | %c | %c \n",chess[2][0],chess[2][1],chess[2][2]); if(check(chess)==1) { printf("player2 is the winner\n"); return 1; } } } int check(char chess[3][3]) { for(int i=0;i<=2;i++) if(chess[i][0]==chess[i][1]&&chess[i][1]==chess[i][2]) return 1; else if(chess[0][i]==chess[1][i]&&chess[1][i]==chess[2][i]) return 1; if(chess[0][0]==chess[1][1]&&chess[1][1]==chess[2][2]) return 1; else if(chess[0][2]==chess[1][1]&&chess[1][1]==chess[2][0]) return 1; else return 0; }