用c语言写一个围棋游戏
时间: 2023-11-15 14:05:43 浏览: 98
好的,以下是一个简单的围棋游戏的C语言实现:
```c
#include <stdio.h>
#define EMPTY 0
#define BLACK 1
#define WHITE 2
#define BOARD_SIZE 19
int board[BOARD_SIZE][BOARD_SIZE] = {0};
void print_board() {
printf(" ");
for (int i = 0; i < BOARD_SIZE; ++i) {
printf("%c ", 'A' + i);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; ++i) {
printf("%2d ", i + 1);
for (int j = 0; j < BOARD_SIZE; ++j) {
if (board[i][j] == EMPTY)
printf(". ");
else if (board[i][j] == BLACK)
printf("X ");
else if (board[i][j] == WHITE)
printf("O ");
}
printf("\n");
}
}
int main() {
int x, y, player = BLACK;
while (1) {
print_board();
printf("%c's turn: ", player == BLACK ? 'X' : 'O');
scanf("%d %d", &x, &y);
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
printf("Invalid move!\n");
continue;
}
if (board[x][y] != EMPTY) {
printf("Invalid move!\n");
continue;
}
board[x][y] = player;
player = 3 - player;
}
return 0;
}
```
这个程序使用一个二维数组来表示棋盘,其中0表示空,1表示黑子,2表示白子。它提供了一个简单的命令行界面,玩家可以交替输入坐标来下棋,程序会判断是否有效并更新棋盘状态。
阅读全文