# 有没有大佬能看一下这串代码Bug出在哪，一输入2 2 就直接重新开始游戏了

c code 2023-05-04 16:48:59
``````#include<stdio.h>
#include<time.h>
#define row 5
#define col 5
{
printf("       五子棋       \n");
printf("   play:1  exit:0   \n");
printf("                    \n");
}

void Initboard(char board[row][col], int x, int y)
{
int i=0;
int j=0;
for (i = 0; i < row; i++)
{
for ( j = 0; j < col; j++)
{
board[i][j] = ' ';//这就是将数组的内容初始化成全是空格
}
}
}
void displayboard(char board[row][col], int x, int y)
{
int i=0;
int j=0;
int k=0;
for (i = 0; i < row; i++)
{
//1、打印一行数据
for ( j = 0; j < col; j++)
{
printf(" %c ", board[i][j]);
if (j < col - 1)//最后一列不需要再打印竖杠了
printf("|");
}
printf("\n");
if (i < row - 1)
{
for ( k = 0; k < col; k++)
{
printf("---");
if (k < col - 1)
printf("|");
}
printf("\n");
}
}
}
void playermove(char board[row][col], int x, int y)
{
int a;
int b;
printf("现在请玩家输入想要下的坐标：\n");
printf("输入示例:1 1\n");
while (1)
{
printf("请输入要下坐标\n");
scanf("%d%d", &a, &b);
if (a >= 1 && a <= row && b >= 1 && b <= col)//这样输入坐标才是有效的
{
if (board[a - 1][b - 1] == ' ')
{
board[a - 1][b - 1] = '\$';
break;
}
else
{
printf("该坐标已经下过了\n");
}
}
else
{
printf("坐标无效，请重新输入：\n");
}
}
}
void computermove(char board[row][col], int x, int y)
{
printf("现在轮到电脑走了\n");
int c;
int d;
while (1)
{
c = rand() % row;//模一个3就得到0、1、2
d = rand() % col;
if (board[c][d] == ' ')
{
board[c][d] = '#';
break;
}
}
}
int isfull(char board[row][col], int x, int y)//返回1，棋盘满了;返回0，棋盘没满
{
int e=0;
int f=0;
for (e = 0; e < row; e++)
{
for (f = 0; f < col; f++)
{
if (board[e][f] == ' ')
return 0;
}
}
return 1;
}
char whowin(char board[row][col], int x, int y)
{
int i=0;
int j=0;
//横三行
for ( i = 0; i < row; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][2] == board[i][3] && board[i][3] == board[i][4] && board[i][1] != ' ')//随便一个不等于空格就行
return board[i][1];//随便返回一个就行
}
//竖三列
for ( j = 0; j < col; j++)
{
if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[2][j] == board[3][j] && board[3][j] == board[4][j] && board[1][1] != ' ')
return board[1][j];
}
//两个对角线
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[2][2] == board[3][3] && board[3][3] == board[4][4] && board[1][1] != ' ')
return board[1][1];
if (board[4][0] == board[3][1] && board[3][1] == board[2][2] && board[1][3] == board[2][2] && board[1][3] == board[0][4] && board[2][2] != ' ')
return board[2][2];
//判断是否平局
if (1 == isfull(board, row, col))
{
return 'Q';
}
return 'C';
}
void game()
{
char ret = 0;
//数组--存放走出的棋盘的信息
char board[row][col] = { 0 };
//希望数组的初始内容全是空格
//初始化棋盘
Initboard(board, row, col);
//打印棋盘
displayboard(board, row, col);
//开始下棋
while (1)
{
//先让玩家下棋，再电脑下棋
playermove(board, row, col);
displayboard(board, row, col);//下完一次打印一次棋盘
//判断玩家是否赢了
ret = whowin(board, row, col);
if (ret != 'C')
{
break;
}
computermove(board, row, col);
displayboard(board, row, col);
//判断电脑是否赢了
ret = whowin(board, row, col);
if (ret != 'C')
{
break;
}
}
if (ret == '\$')
{
printf("Play Win!\n");
}
else if (ret == '#')
{
printf("Computer Win!\n");
}
else if (ret == 'Q')
{
printf("平局\n");
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("请输入：\n");
scanf("%d", &input);
switch (input)
{
case 1:
printf("游戏:五子棋\n");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}``````

...全文
155 2 打赏 收藏 转发到动态 举报

2 条回复

zara 2023-05-05
• 打赏
• 举报

whowin() 函数里列判断部分，最后的 board[1][1] != ' ' 有误，应该是 board[1][j] != ' '

ooolinux 2023-05-05
• 打赏
• 举报

————————————————————

64,701

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下