# 简单五子棋的实现

### 1.初始化棋盘

``````char ret;
//数据存储在一个二维数组中,玩家下去*，电脑下去#.
char board[row][col] = { 0 };//最开始为全是空格
InitBoard(board,row,col);//初始化棋盘``````

``````void InitBoard(char board[row][col], int Row, int Col)
{
int i = 0;
int j = 0;
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
board[i][j] = ' ';
}
}
}``````

### 2.打印棋盘格式

``````char ret;
//数据存储在一个二维数组中,玩家下去*，电脑下去#.
char board[row][col] = { 0 };//最开始为全是空格
InitBoard(board,row,col);//初始化棋盘
DisplayBoard(board, row, col);//打印棋盘``````

``````void DisplayBoard(char board[row][col], int Row, int Col)
{
int i, j;
for (i = 0; i < Row; i++)
{
for (j = 0; j < Col; j++)
{
printf(" %c ", board[i][j]);
if (j < Col-1)
printf("|");
}
printf("n");
for (j = 0; j < Col; j++)
{
if (i < Row-1)
{
printf("---");
}
if (j < Col-1&&i<Row-1)
{
printf("|");
}
}
printf("n");
}
}``````

### 3.玩家走棋

``PlayMove(board, row, col);//玩家下棋``

``````}
void PlayMove(char board[row][col], int Row, int Col)
{
int x, y;
printf("玩家下棋：");
scanf("%d %d", &x, &y);//输入走棋坐标
while (1)
{
if (x >= 1 && x <= Row && y >= 1 && y <= Col)//判断是否越界
{
if (board[x - 1][y - 1] == ' ')//判断是否可以下棋
{
board[x - 1][y - 1] = '*';
break;//如果下棋成功则退出while循环
}
else
{
printf("输入错误，请重新输入n");
}
}
else
{
printf("输入错误，请重新输入n");
}
}
}``````

### 4.电脑走棋

``ComputerMove(board, row, col);``

``````void ComputerMove(char board[row][col], int Row, int Col)
{
printf("电脑下棋n");
while (1)
{
int x = rand() % Row + 1;//随机初始化x、y（1-5）
int y = rand() % Col + 1;
if (board[x - 1][y - 1] == ' ')//判断是否可以下棋
{
board[x - 1][y - 1] = '#';
break;//下棋成功，退出
}

}

}``````

1.玩家五字连在一起

2.电脑五字连在一起

3.棋盘占满，没有分出胜负

4.以上三者都不满足，继续下棋

### 5.判断棋盘

``````void game()
{
char ret;
//数据存储在一个二维数组中,玩家下去*，电脑下去#.
char board[row][col] = { 0 };//最开始为全是空格
InitBoard(board,row,col);//初始化棋盘
DisplayBoard(board, row, col);//打印棋盘
//下棋
while (1)
{
PlayMove(board, row, col);//玩家下棋
DisplayBoard(board, row, col);//打印
ret = is_win(board, row, col);//判断是否结束

ComputerMove(board, row, col); //电脑下棋
DisplayBoard(board, row, col);//打印
}

}``````

``````char is_win(char board[row][col], int Row, int Col)
{
int x, y;
//求行
for (x = 0; x < Row; x++)
{
if (board[x][0] == board[x][1]&&board[x][1] == board[x][2]&&board[x][2]==board[x][3]&&board[x][3]==board[x][4]&&board[x][0]!=' ')
{
return board[x][0];
}

}
//求列
for (y = 0; y < Col; y++)
{
if (board[0][y] == board[1][y] && board[1][y] == board[2][y] &&board[2][y]==board[3][y]&&board[3][y]==board[4][y]&&board[0][y] != ' ')
{
return board[0][y];
}

}
//求对角线
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[0][0] != ' ')
{
return board[0][0];
}
if (board[0][4] == board[1][3] && board[1][3] == board[2][2] && board[2][2] == board[3][1] && board[3][1] == board[4][0] && board[0][4] != ' ')
{
return board[0][4];
}
if (isfull(board,row,col))
{
return 'Q';
}
return 'c';
}``````

``````int isfull(char board[row][col], int Row, int Col)
{
int x, y;
for (x = 0; x < Row; x++)
{
for (y = 0; y < Col; y++)
{
if (board[x][y] == ' ');
{
return 0;
}
}
}
return 1;
}``````

``````void game()
{
char ret;
//数据存储在一个二维数组中,玩家下去*，电脑下去#.
char board[row][col] = { 0 };//最开始为全是空格
InitBoard(board,row,col);//初始化棋盘
DisplayBoard(board, row, col);//打印棋盘
//下棋
while (1)
{
PlayMove(board, row, col);//玩家下棋
DisplayBoard(board, row, col);//打印
ret = is_win(board, row, col);//判断是否结束
if (ret!='c')
{
break;
}
ComputerMove(board, row, col); //电脑下棋
DisplayBoard(board, row, col);//打印
if (ret != 'c')
{
break;
}
}
if (ret == '*')
{
printf("玩家赢n");
}
if (ret == '#')
{
printf("电脑赢n");
}
if (ret == 'Q')
{
printf("平局n");
}
}``````

THE END