二人对弈的简易围棋C语言程序设计
时间: 2023-07-04 17:04:24 浏览: 68
以下是一个简易的围棋对弈程序设计,使用 C 语言编写:
```
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 9 // 棋盘大小
int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
void init_board() { // 初始化棋盘
for(int i = 0; i < BOARD_SIZE; i++) {
for(int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = 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] == 0) {
printf(". ");
} else if(board[i][j] == 1) {
printf("X ");
} else {
printf("O ");
}
}
printf("\n");
}
}
int is_valid_move(int x, int y, int player) { // 判断棋子是否落在空位上
if(x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
return 0;
}
if(board[x][y] != 0) {
return 0;
}
return 1;
}
int is_surrounded(int x, int y, int player) { // 判断棋子是否被围住
int visited[BOARD_SIZE][BOARD_SIZE] = {0}; // 记录是否访问过
int queue[BOARD_SIZE*BOARD_SIZE][2];
int front = 0, rear = 0;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
queue[rear][0] = x;
queue[rear][1] = y;
visited[x][y] = 1;
rear++;
while(front < rear) {
int cx = queue[front][0];
int cy = queue[front][1];
front++;
for(int i = 0; i < 4; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if(nx < 0 || nx >= BOARD_SIZE || ny < 0 || ny >= BOARD_SIZE) {
continue;
}
if(board[nx][ny] == 0) {
return 0;
}
if(board[nx][ny] == player && !visited[nx][ny]) {
visited[nx][ny] = 1;
queue[rear][0] = nx;
queue[rear][1] = ny;
rear++;
}
}
}
return 1;
}
int play(int x, int y, int player) { // 落子
if(!is_valid_move(x, y, player)) {
return 0;
}
board[x][y] = player;
if(is_surrounded(x, y, player)) { // 判断棋子是否被围住
board[x][y] = 0;
return 0;
}
return 1;
}
int main() {
init_board();
int player = 1; // 玩家1为黑子,玩家2为白子
while(1) {
print_board();
if(player == 1) {
printf("Player 1's turn (X): ");
} else {
printf("Player 2's turn (O): ");
}
char input[4];
scanf("%s", input); // 输入落子坐标
if(input[0] == 'q') { // 输入q退出游戏
break;
}
int x = input[1] - '1';
int y = input[0] - 'a';
if(play(x, y, player)) {
player = 3 - player; // 切换玩家
} else {
printf("Invalid move!\n");
}
}
return 0;
}
```
该程序使用二维数组 `board` 存储棋盘,其中 0 表示空位,1 表示黑子,2 表示白子。`init_board()` 函数用于初始化棋盘,`print_board()` 函数用于打印棋盘。`is_valid_move()` 函数用于判断棋子是否落在空位上,`is_surrounded()` 函数用于判断棋子是否被围住,`play()` 函数用于落子。程序通过不断循环让玩家输入落子坐标,并调用 `play()` 函数进行落子,如果落子无效则提示玩家重新输入坐标。玩家可以输入 q 退出游戏。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)