codeblock的C语言围棋代码,包含吃子和禁手
时间: 2024-05-06 08:18:54 浏览: 39
这是一个简单的 C 语言围棋代码,包含了落子、判断禁手和吃子的功能。该代码使用了二维数组来存储棋盘,使用了函数来实现各种功能。注意,这只是一个简单的示例代码,还有很多需要完善的地方。
```c
#include <stdio.h>
#include <stdlib.h>
#define BOARD_SIZE 15
#define EMPTY 0
#define BLACK 1
#define WHITE 2
int board[BOARD_SIZE][BOARD_SIZE];
/* 初始化棋盘 */
void init_board() {
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
}
/* 打印棋盘 */
void print_board() {
int i, j;
printf(" ");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i + 1);
}
printf("\n");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%c ", 'a' + i);
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == BLACK) {
printf("X ");
} else if (board[i][j] == WHITE) {
printf("O ");
} else {
printf("+ ");
}
}
printf("\n");
}
}
/* 判断一个位置是否在棋盘内 */
int is_valid_pos(int row, int col) {
return row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
}
/* 判断一个位置是否为空 */
int is_empty_pos(int row, int col) {
return board[row][col] == EMPTY;
}
/* 判断一个位置是否为禁手 */
int is_forbidden_hand(int player, int row, int col) {
int i, j, cnt = 0;
if (player == BLACK) {
/* 黑方禁手 */
/* 判断四个方向 */
if ((row > 1 && board[row-1][col] == BLACK && board[row-2][col] == BLACK) ||
(row < BOARD_SIZE-2 && board[row+1][col] == BLACK && board[row+2][col] == BLACK) ||
(col > 1 && board[row][col-1] == BLACK && board[row][col-2] == BLACK) ||
(col < BOARD_SIZE-2 && board[row][col+1] == BLACK && board[row][col+2] == BLACK)) {
return 1;
}
/* 判断两个斜方向 */
if ((row > 0 && row < BOARD_SIZE-1 && col > 0 && col < BOARD_SIZE-1) &&
((board[row-1][col-1] == BLACK && board[row+1][col+1] == BLACK) ||
(board[row-1][col+1] == BLACK && board[row+1][col-1] == BLACK))) {
return 1;
}
/* 判断是否形成五连 */
for (i = row-4; i <= row+4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[i][col] == BLACK) {
cnt++;
if (cnt >= 5) {
return 1;
}
} else {
cnt = 0;
}
}
cnt = 0;
for (j = col-4; j <= col+4; j++) {
if (j < 0 || j >= BOARD_SIZE) {
continue;
}
if (board[row][j] == BLACK) {
cnt++;
if (cnt >= 5) {
return 1;
}
} else {
cnt = 0;
}
}
} else {
/* 白方禁手 */
/* 判断是否形成五连 */
for (i = row-4; i <= row+4; i++) {
if (i < 0 || i >= BOARD_SIZE) {
continue;
}
if (board[i][col] == WHITE) {
cnt++;
if (cnt >= 5) {
return 1;
}
} else {
cnt = 0;
}
}
cnt = 0;
for (j = col-4; j <= col+4; j++) {
if (j < 0 || j >= BOARD_SIZE) {
continue;
}
if (board[row][j] == WHITE) {
cnt++;
if (cnt >= 5) {
return 1;
}
} else {
cnt = 0;
}
}
}
return 0;
}
/* 落子 */
int make_move(int player, int row, int col) {
int i, j;
if (!is_valid_pos(row, col) || !is_empty_pos(row, col)) {
return 0;
}
board[row][col] = player;
/* 判断是否吃掉对方的棋子 */
for (i = row-1; i <= row+1; i++) {
for (j = col-1; j <= col+1; j++) {
if (is_valid_pos(i, j) && !is_empty_pos(i, j) && board[i][j] != player) {
if (is_forbidden_hand(board[i][j], i, j)) {
/* 对方禁手 */
continue;
}
/* 吃掉对方棋子 */
board[i][j] = EMPTY;
}
}
}
/* 判断是否形成禁手 */
if (is_forbidden_hand(player, row, col)) {
/* 撤销落子 */
board[row][col] = EMPTY;
return 0;
}
return 1;
}
int main() {
int player = BLACK;
init_board();
while (1) {
print_board();
printf("Player %d, enter your move (e.g. c3): ", player);
char input[10];
scanf("%s", input);
int row = input[1] - '1';
int col = input[0] - 'a';
if (make_move(player, row, col)) {
player = 3 - player; /* 切换玩家 */
} else {
printf("Invalid move!\n");
}
}
return 0;
}
```
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.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)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)