六子棋建立博弈树α-β剪枝算法代码C语言
时间: 2023-10-05 22:08:51 浏览: 120
下面是六子棋建立博弈树α-β剪枝算法的代码,使用C语言实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define BOARD_SIZE 6
#define EMPTY 0
#define PLAYER1 1
#define PLAYER2 2
int board[BOARD_SIZE][BOARD_SIZE];
// 判断当前棋盘状态下是否有玩家获胜
int is_win(int player) {
int i, j, k;
// 横向检查
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
for (k = 0; k < 6; k++) {
if (board[i][j + k] != player) {
break;
}
}
if (k == 6) {
return 1;
}
}
}
// 竖向检查
for (i = 0; i < BOARD_SIZE - 5; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
for (k = 0; k < 6; k++) {
if (board[i + k][j] != player) {
break;
}
}
if (k == 6) {
return 1;
}
}
}
// 左上到右下斜线检查
for (i = 0; i < BOARD_SIZE - 5; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
for (k = 0; k < 6; k++) {
if (board[i + k][j + k] != player) {
break;
}
}
if (k == 6) {
return 1;
}
}
}
// 右上到左下斜线检查
for (i = 5; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
for (k = 0; k < 6; k++) {
if (board[i - k][j + k] != player) {
break;
}
}
if (k == 6) {
return 1;
}
}
}
return 0;
}
// 计算当前棋盘状态的得分
int evaluate_board() {
int i, j, k, player1_score, player2_score;
// 横向得分
player1_score = player2_score = 0;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
int player1_count = 0, player2_count = 0;
for (k = 0; k < 6; k++) {
if (board[i][j + k] == PLAYER1) {
player1_count++;
} else if (board[i][j + k] == PLAYER2) {
player2_count++;
}
}
if (player1_count > 0 && player2_count == 0) {
player1_score += 10;
} else if (player2_count > 0 && player1_count == 0) {
player2_score += 10;
}
}
}
// 竖向得分
for (i = 0; i < BOARD_SIZE - 5; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
int player1_count = 0, player2_count = 0;
for (k = 0; k < 6; k++) {
if (board[i + k][j] == PLAYER1) {
player1_count++;
} else if (board[i + k][j] == PLAYER2) {
player2_count++;
}
}
if (player1_count > 0 && player2_count == 0) {
player1_score += 10;
} else if (player2_count > 0 && player1_count == 0) {
player2_score += 10;
}
}
}
// 左上到右下斜线得分
for (i = 0; i < BOARD_SIZE - 5; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
int player1_count = 0, player2_count = 0;
for (k = 0; k < 6; k++) {
if (board[i + k][j + k] == PLAYER1) {
player1_count++;
} else if (board[i + k][j + k] == PLAYER2) {
player2_count++;
}
}
if (player1_count > 0 && player2_count == 0) {
player1_score += 10;
} else if (player2_count > 0 && player1_count == 0) {
player2_score += 10;
}
}
}
// 右上到左下斜线得分
for (i = 5; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE - 5; j++) {
int player1_count = 0, player2_count = 0;
for (k = 0; k < 6; k++) {
if (board[i - k][j + k] == PLAYER1) {
player1_count++;
} else if (board[i - k][j + k] == PLAYER2) {
player2_count++;
}
}
if (player1_count > 0 && player2_count == 0) {
player1_score += 10;
} else if (player2_count > 0 && player1_count == 0) {
player2_score += 10;
}
}
}
return player1_score - player2_score;
}
// 枚举所有空位,计算当前玩家的得分
int alpha_beta_search(int alpha, int beta, int depth, int player) {
int i, j, value, best_value;
if (is_win(PLAYER1)) {
return INT_MAX;
} else if (is_win(PLAYER2)) {
return INT_MIN;
} else if (depth == 0) {
return evaluate_board();
}
if (player == PLAYER1) {
best_value = INT_MIN;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = PLAYER1;
value = alpha_beta_search(alpha, beta, depth - 1, PLAYER2);
board[i][j] = EMPTY;
if (value > best_value) {
best_value = value;
}
if (best_value > alpha) {
alpha = best_value;
}
if (alpha >= beta) {
return best_value;
}
}
}
}
} else {
best_value = INT_MAX;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = PLAYER2;
value = alpha_beta_search(alpha, beta, depth - 1, PLAYER1);
board[i][j] = EMPTY;
if (value < best_value) {
best_value = value;
}
if (best_value < beta) {
beta = best_value;
}
if (alpha >= beta) {
return best_value;
}
}
}
}
}
return best_value;
}
// 根据当前玩家的得分,找到最佳落子位置
void find_best_move(int *x, int *y) {
int i, j, value, best_value;
*x = *y = -1;
best_value = INT_MIN;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = PLAYER1;
value = alpha_beta_search(INT_MIN, INT_MAX, 5, PLAYER2);
board[i][j] = EMPTY;
if (value > best_value) {
best_value = value;
*x = i;
*y = j;
}
}
}
}
}
int main() {
int i, j, x, y;
// 初始化棋盘
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
// 玩家和AI轮流下棋,直到有一方获胜或棋盘已满
while (!is_win(PLAYER1) && !is_win(PLAYER2)) {
// 玩家下棋
printf("Enter your move (row column): ");
scanf("%d %d", &x, &y);
if (board[x][y] != EMPTY) {
printf("Invalid move!\n");
continue;
}
board[x][y] = PLAYER2;
// AI下棋
find_best_move(&x, &y);
board[x][y] = PLAYER1;
// 输出当前棋盘状态
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
// 输出获胜者
if (is_win(PLAYER1)) {
printf("AI wins!\n");
} else if (is_win(PLAYER2)) {
printf("You win!\n");
} else {
printf("Draw!\n");
}
return 0;
}
```
注意,这只是一个简单的示例代码,可能存在一些问题和需要完善的地方。如果你想使用这个代码进行实际的游戏,需要进一步优化和改进。
阅读全文