用C语言写一个井字棋游戏
时间: 2023-05-23 12:04:28 浏览: 114
A:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义井字棋格子内容类型
enum GridContent {
GridContentEmpty,
GridContentCross,
GridContentCircle
};
// 构建井字棋棋盘
typedef struct {
enum GridContent content[3][3];
} TicTacToeBoard;
// 初始化井字棋棋盘
void init_board(TicTacToeBoard *board) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board->content[i][j] = GridContentEmpty;
}
}
}
// 输出井字棋棋盘
void print_board(TicTacToeBoard *board) {
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board->content[i][j] == GridContentEmpty) {
printf(" ");
} else if (board->content[i][j] == GridContentCross) {
printf("X");
} else if (board->content[i][j] == GridContentCircle) {
printf("O");
}
if (j != 2) {
printf("|");
}
}
printf("\n");
if (i != 2) {
printf("-+-+-\n");
}
}
}
// 判断是否有一方获胜
int check_win(TicTacToeBoard *board, enum GridContent content) {
// 判断行
for (int i = 0; i < 3; ++i) {
if (board->content[i][0] == content && board->content[i][1] == content && board->content[i][2] == content) {
return 1;
}
}
// 判断列
for (int i = 0; i < 3; ++i) {
if (board->content[0][i] == content && board->content[1][i] == content && board->content[2][i] == content) {
return 1;
}
}
// 判断对角线
if (board->content[0][0] == content && board->content[1][1] == content && board->content[2][2] == content) {
return 1;
}
if (board->content[0][2] == content && board->content[1][1] == content && board->content[2][0] == content) {
return 1;
}
return 0;
}
// 执行游戏主循环
void run_game() {
TicTacToeBoard board;
init_board(&board);
enum GridContent player1_content = GridContentCross;
enum GridContent player2_content = GridContentCircle;
enum GridContent cur_content = player1_content;
while (1) {
// 输出当前棋盘
printf("\n");
print_board(&board);
// 让玩家输入下一步位置
int row, col;
printf("\nPlayer %s's turn (row, col): ", (cur_content == player1_content ? "1" : "2"));
scanf("%d %d", &row, &col);
// 检查输入是否合法
if (row < 1 || row > 3 || col < 1 || col > 3) {
printf("Invalid input! Please enter the position as row col, where row and col are integers between 1 and 3.\n");
continue;
}
if (board.content[row - 1][col - 1] != GridContentEmpty) {
printf("The position has already been taken! Please choose another one.\n");
continue;
}
// 将指定位置标记为当前玩家的棋子
board.content[row - 1][col - 1] = cur_content;
// 检查是否有一方获胜
if (check_win(&board, cur_content)) {
printf("\nPlayer %s wins!\n", (cur_content == player1_content ? "1" : "2"));
print_board(&board);
break;
}
// 如果棋盘已满,宣布平局
int is_tie = 1;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (board.content[i][j] == GridContentEmpty) {
is_tie = 0;
break;
}
}
if (!is_tie) {
break;
}
}
if (is_tie) {
printf("\nThe game is a tie!\n");
print_board(&board);
break;
}
// 切换到下一个玩家
cur_content = (cur_content == player1_content ? player2_content : player1_content);
}
}
int main() {
run_game();
return 0;
}
```
这个程序使用了结构体和枚举类型来构建、表示井字棋棋盘和格子内容。它包括了下列功能:
- 初始化棋盘
- 输出棋盘
- 判断是否有一方获胜
- 让玩家输入下一步位置,并检查输入是否合法
- 更新棋盘状态,并在必要时宣布获胜或平局
- 切换到下一个玩家
运行程序即可体验井字棋游戏的玩法!
阅读全文