请帮我写一个完整的五子棋的c语言代码
时间: 2023-06-27 15:06:10 浏览: 76
好的,以下是一个简单的五子棋的 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROWS 15
#define COLS 15
void draw_board(char board[ROWS][COLS]); // 绘制棋盘
int check_win(char board[ROWS][COLS], char c); // 判断是否获胜
void player_move(char board[ROWS][COLS], char c); // 玩家下棋
void computer_move(char board[ROWS][COLS], char c); // 电脑下棋
int main()
{
char board[ROWS][COLS] = {0}; // 棋盘
char player = 'X'; // 玩家执棋子
char computer = 'O'; // 电脑执棋子
int turn = 0; // 轮到谁下棋,0 表示玩家,1 表示电脑
while (1) {
draw_board(board);
if (turn == 0) {
player_move(board, player);
if (check_win(board, player)) {
printf("你赢了!\n");
break;
}
turn = 1;
} else {
computer_move(board, computer);
if (check_win(board, computer)) {
printf("电脑赢了!\n");
break;
}
turn = 0;
}
}
return 0;
}
void draw_board(char board[ROWS][COLS])
{
system("clear"); // 清屏
printf(" ");
for (int i = 0; i < COLS; i++) {
printf(" %d", i + 1);
}
printf("\n");
for (int i = 0; i < ROWS; i++) {
printf("%c", i + 'A');
for (int j = 0; j < COLS; j++) {
printf(" %c", board[i][j]);
}
printf("\n");
}
}
int check_win(char board[ROWS][COLS], char c)
{
// 检查行
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j <= COLS - 5; j++) {
if (board[i][j] == c && board[i][j+1] == c && board[i][j+2] == c && board[i][j+3] == c && board[i][j+4] == c) {
return 1;
}
}
}
// 检查列
for (int i = 0; i <= ROWS - 5; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == c && board[i+1][j] == c && board[i+2][j] == c && board[i+3][j] == c && board[i+4][j] == c) {
return 1;
}
}
}
// 检查主对角线
for (int i = 0; i <= ROWS - 5; i++) {
for (int j = 0; j <= COLS - 5; j++) {
if (board[i][j] == c && board[i+1][j+1] == c && board[i+2][j+2] == c && board[i+3][j+3] == c && board[i+4][j+4] == c) {
return 1;
}
}
}
// 检查副对角线
for (int i = 0; i <= ROWS - 5; i++) {
for (int j = COLS - 1; j >= 4; j--) {
if (board[i][j] == c && board[i+1][j-1] == c && board[i+2][j-2] == c && board[i+3][j-3] == c && board[i+4][j-4] == c) {
return 1;
}
}
}
return 0;
}
void player_move(char board[ROWS][COLS], char c)
{
char input[3];
int row, col;
while (1) {
printf("请输入你的落子位置(例如 A1):");
scanf("%s", input);
row = input[0] - 'A';
col = input[1] - '1';
if (row < 0 || row > ROWS - 1 || col < 0 || col > COLS - 1) {
printf("无效的位置,请重新输入!\n");
} else if (board[row][col] != 0) {
printf("该位置已经有棋子了,请重新输入!\n");
} else {
board[row][col] = c;
break;
}
}
}
void computer_move(char board[ROWS][COLS], char c)
{
int row, col;
// 简单的电脑下棋策略:随机选择一个空位置
while (1) {
row = rand() % ROWS;
col = rand() % COLS;
if (board[row][col] == 0) {
board[row][col] = c;
break;
}
}
}
```
这个代码实现了一个简单的五子棋游戏,玩家执黑,电脑执白,可以通过输入行列坐标来下棋。其中 `check_win()` 函数用来判断是否获胜,如果获胜就返回 1,否则返回 0。`player_move()` 和 `computer_move()` 函数分别实现了玩家和电脑下棋的逻辑,其中电脑的下棋策略是随机选择一个空位置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)