#include
#include
#define N 9
void playGame(char arr[N]);//Play the game.
char is_win(char a);//Judge the winner.
int is_full(char arr[N]);//Judge if the players fill the chessboard.
//Play the game.
void playGame(char arr[N])
{
int p;
int q;
int b;
char a;
do
{
printf("%c %c %c\n",arr[0],arr[1],arr[2]);
printf("%c %c %c\n",arr[3],arr[4],arr[5]);
printf("%c %c %c\n",arr[6],arr[7],arr[8]);//Output the chessboard.
printf("Player X, please enter the number in the display (1-9)\nwhere you wish your mark to be placed.\n");
scanf("%d",&p);//Player X.
q=p-1;
arr[q]='X';
b=is_full(arr);
a=is_win(a);
if(a!=2) // ‘ ’:No one wins.
break;//Break if there is a winner.
system("cls");//Clear the screem.
printf("%c %c %c\n",arr[0],arr[1],arr[2]);
printf("%c %c %c\n",arr[3],arr[4],arr[5]);
printf("%c %c %c\n",arr[6],arr[7],arr[8]);
printf("Player O, please enter the number in the display (1-9)\nwhere you wish your mark to be placed.\n");
scanf("%d",&p);
q=p-1;
arr[q]='O';
b=is_full(arr);
a=is_win(a);
if(a!=2) // ‘ ’:没人赢
break;
system("cls");
}
while(a!=2);
}
//Judge the winner.
char is_win(char a)
{
char arr[N];
//Judge and return rows.
int b;
b=is_full(arr);
if(arr[0]==arr[1]&&arr[1]==arr[2])
a=arr[0];
else if(arr[3]==arr[4]&&arr[4]==arr[5])
a=arr[3];
else if(arr[6]==arr[7]&&arr[7]==arr[8])
a=arr[6];
//Judge and return columns.
else if(arr[0]==arr[3]&&arr[3]==arr[6])
a=arr[0];
else if(arr[1]==arr[4]&&arr[4]==arr[7])
a=arr[1];
else if(arr[2]==arr[5]&&arr[5]==arr[8])
a=arr[2];
//Judge and return diagonals.
else if(arr[0]==arr[4]&&arr[4]==arr[8])
a=arr[0];
else if(arr[2]==arr[4]&&arr[4]==arr[6])
a=arr[2];
else if(b==2)//Judge if it is draw if it is, return ‘q'.
a=1;
else
a=2;
return a;
}
//Judge if the players fill the chessboard.
int is_full(char arr[N])
{
int b;
int i;
for(i=0;i<9;i++)
{
if(arr[i]==1||arr[i]==2||arr[i]==3||arr[i]==4||arr[i]==5||arr[i]==6||arr[i]==7||arr[i]==8||arr[i]==9)//判断是否有空格
b=1;//The chessboard is not full, b= 1.
else
b=2;//The chessboard is full.
}
return b;//The chessboard is full, return 0.
}
int main()
{
printf("Tic-Tac-Toe\n");
char arr[N];
char a;
//initialize array prior to playing
for ( int i = 0; i < 9; i++)
arr[i] = (char) ( '1' + i );
arr[9] = '\0';
playGame(arr);
if(a==1)
printf("The game ended in a draw.");//Judge the winner.
else
printf("The winner is player %c.",a);//Judge the winner.
return 0;
}
为什么a不被识别运行出来不能循环呢