在写三子棋的过程中,在电脑下子的地方出现不明bug,电脑突然罢工,相关图片与代码如下;
while (1)
{
computermove(board, ROW, COL);//电脑下棋
displayboard(board, ROW, COL);//每走一次打印一次棋盘
playermove(board, ROW, COL);//玩家下棋
displayboard(board, ROW, COL);
}
void computermove(char board[ROW][COL], int row, int col)
{
printf("电脑走\n");
int x, y;
x = rand() % row;
y = rand() % col;
while (100)
{
if (board[x][y] == ' ')
{
board[x][y] = '+';
break;
}
}
}
void playermove(char board[ROW][COL], int row, int col)
{
int x, y;
while (1)
{
printf("玩家走请输入要下的坐标\n");
scanf_s("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) //判断棋子是否合理,并输入
{
if (board[x - 1][y - 1] == ' ')
{
board[x - 1][y - 1] = '*';
break;
}
else
printf("该坐标已经有子,请重新输入\n");
}
else
{
printf("坐标有误请重新输入\n");
}
}
}