c语言五子棋游戏
时间: 2023-07-09 22:22:11 浏览: 120
好的,我可以为您提供一个简单的C语言五子棋游戏的代码示例,如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROWS 15
#define COLS 15
void init_board(char board[][COLS], int rows, int cols); // 初始化棋盘
void display_board(char board[][COLS], int rows, int cols); // 显示棋盘
void player_move(char board[][COLS], int rows, int cols); // 玩家下棋
void computer_move(char board[][COLS], int rows, int cols); // 计算机下棋
char check_win(char board[][COLS], int rows, int cols); // 判断胜负
int main()
{
char board[ROWS][COLS];
init_board(board, ROWS, COLS);
display_board(board, ROWS, COLS);
char winner = 0;
while (1)
{
player_move(board, ROWS, COLS);
display_board(board, ROWS, COLS);
winner = check_win(board, ROWS, COLS);
if (winner)
break;
computer_move(board, ROWS, COLS);
display_board(board, ROWS, COLS);
winner = check_win(board, ROWS, COLS);
if (winner)
break;
}
if (winner == 'X')
printf("You win!\n");
else if (winner == 'O')
printf("Computer wins!\n");
else
printf("It's a tie!\n");
return 0;
}
void init_board(char board[][COLS], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = '+';
}
}
}
void display_board(char board[][COLS], int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void player_move(char board[][COLS], int rows, int cols)
{
int row, col;
printf("Please enter your move (row column): ");
scanf("%d %d", &row, &col);
while (row < 1 || row > ROWS || col < 1 || col > COLS || board[row-1][col-1] != '+')
{
printf("Invalid move. Please enter your move (row column): ");
scanf("%d %d", &row, &col);
}
board[row-1][col-1] = 'X';
}
void computer_move(char board[][COLS], int rows, int cols)
{
int row, col;
do
{
row = rand() % ROWS;
col = rand() % COLS;
} while (board[row][col] != '+');
board[row][col] = 'O';
}
char check_win(char board[][COLS], int rows, int cols)
{
// 检查行
for (int i = 0; i < rows; i++)
{
for (int j = 0; j <= cols - 5; j++)
{
if (board[i][j] == '+' ||
board[i][j] != board[i][j+1] ||
board[i][j+1] != board[i][j+2] ||
board[i][j+2] != board[i][j+3] ||
board[i][j+3] != board[i][j+4])
{
continue;
}
else
{
return board[i][j];
}
}
}
// 检查列
for (int j = 0; j < cols; j++)
{
for (int i = 0; i <= rows - 5; i++)
{
if (board[i][j] == '+' ||
board[i][j] != board[i+1][j] ||
board[i+1][j] != board[i+2][j] ||
board[i+2][j] != board[i+3][j] ||
board[i+3][j] != board[i+4][j])
{
continue;
}
else
{
return board[i][j];
}
}
}
// 检查左上-右下对角线
for (int i = 0; i <= rows - 5; i++)
{
for (int j = 0; j <= cols - 5; j++)
{
if (board[i][j] == '+' ||
board[i][j] != board[i+1][j+1] ||
board[i+1][j+1] != board[i+2][j+2] ||
board[i+2][j+2] != board[i+3][j+3] ||
board[i+3][j+3] != board[i+4][j+4])
{
continue;
}
else
{
return board[i][j];
}
}
}
// 检查右上-左下对角线
for (int i = 0; i <= rows - 5; i++)
{
for (int j = cols - 1; j >= 4; j--)
{
if (board[i][j] == '+' ||
board[i][j] != board[i+1][j-1] ||
board[i+1][j-1] != board[i+2][j-2] ||
board[i+2][j-2] != board[i+3][j-3] ||
board[i+3][j-3] != board[i+4][j-4])
{
continue;
}
else
{
return board[i][j];
}
}
}
// 没有胜者
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (board[i][j] == '+')
{
return 0;
}
}
}
// 平局
return 'T';
}
```
这个五子棋游戏使用了一个二维字符数组 `board` 来表示棋盘,其中 `+` 表示空位,`X` 表示玩家下的棋,`O` 表示计算机下的棋。玩家和计算机轮流下棋,直到出现胜者或棋盘填满。胜者是指在一行、一列或对角线上连续出现五个棋子的一方。
阅读全文