#include <stdio.h>
#include <stdbool.h>
int main()
{
char qipan[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
int step = 0;
int player = 0;
int pos = 0;
bool win = false;
int i , j;
//绘制棋盘
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[0][0],qipan[0][1],qipan[0][2]);
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[1][0],qipan[1][1],qipan[1][2]);
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[2][0],qipan[2][1],qipan[2][2]);
printf("|-------------|");
while(step<9)
{
printf("\n请选手%d落子:",step%2+1);
scanf("%d",&pos);
pos = pos - 1;
//判断落子是否合理
if(qipan[pos/3][pos%3]==' ')
{
qipan[pos/3][pos%3] = step%2 ? 'O' : 'X';
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[0][0],qipan[0][1],qipan[0][2]);
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[1][0],qipan[1][1],qipan[1][2]);
printf("|-------------|\n");
printf("| %c || %c || %c |\n", qipan[2][0],qipan[2][1],qipan[2][2]);
printf("|-------------|");
}
else
{
printf("落子位置错误!");
continue;
}
//判断是否胜利
for(i=0;i<3;i++)
{
if(qipan[i][0]==qipan[i][1]&&qipan[i][1]==qipan[i][2]&&qipan[i][0]!= ' ')
win = true;
if(qipan[0][i]==qipan[1][i]&&qipan[1][i]==qipan[2][i]&&qipan[0][i]!= ' ')
win = true;
}
if(qipan[0][0]==qipan[1][1]&&qipan[1][1]==qipan[2][2]&&qipan[0][0]!= ' ')
win = true;
if(qipan[0][2]==qipan[1][1]&&qipan[1][1]==qipan[2][0]&&qipan[0][2]!= ' ')
win = true;
if (win)
{
printf("\n选手%d胜利\n",step%2+1);
system("pause");
return 0;
}
else
step++;
}
printf("\n平局\n");
system("pause");
return 0;
}
C井字棋
最新推荐文章于 2023-12-21 21:34:45 发布