C语言实现建立博弈树并用α-β剪枝的六子棋代码
时间: 2023-10-26 10:28:11 浏览: 129
基于α-β剪枝的一字棋源代码
4星 · 用户满意度95%
以下是C语言实现建立博弈树并用α-β剪枝的六子棋代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_DEPTH 6
#define BOARD_SIZE 15
#define PLAYER1 1
#define PLAYER2 2
#define EMPTY 0
int board[BOARD_SIZE][BOARD_SIZE];
int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
int evaluate(int player) {
int i, j, k, count, score = 0;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player) {
// 水平方向
count = 1;
for (k = 1; k < 6 && j + k < BOARD_SIZE && board[i][j + k] == player; k++) {
count++;
}
for (k = 1; k < 6 && j - k >= 0 && board[i][j - k] == player; k++) {
count++;
}
score += count * count;
// 竖直方向
count = 1;
for (k = 1; k < 6 && i + k < BOARD_SIZE && board[i + k][j] == player; k++) {
count++;
}
for (k = 1; k < 6 && i - k >= 0 && board[i - k][j] == player; k++) {
count++;
}
score += count * count;
// 对角线方向
count = 1;
for (k = 1; k < 6 && i + k < BOARD_SIZE && j + k < BOARD_SIZE && board[i + k][j + k] == player; k++) {
count++;
}
for (k = 1; k < 6 && i - k >= 0 && j - k >= 0 && board[i - k][j - k] == player; k++) {
count++;
}
score += count * count;
// 反对角线方向
count = 1;
for (k = 1; k < 6 && i + k < BOARD_SIZE && j - k >= 0 && board[i + k][j - k] == player; k++) {
count++;
}
for (k = 1; k < 6 && i - k >= 0 && j + k < BOARD_SIZE && board[i - k][j + k] == player; k++) {
count++;
}
score += count * count;
}
}
}
return score;
}
int alphabeta(int depth, int alpha, int beta, int player) {
if (depth == 0) {
return evaluate(player);
}
int i, j, k, score, best_score, opponent = player == PLAYER1 ? PLAYER2 : PLAYER1;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = player;
score = alphabeta(depth - 1, alpha, beta, opponent);
board[i][j] = EMPTY;
if (player == PLAYER1) {
best_score = max(alpha, score);
if (best_score >= beta) {
return best_score;
}
alpha = best_score;
} else {
best_score = min(beta, score);
if (best_score <= alpha) {
return best_score;
}
beta = best_score;
}
}
}
}
return player == PLAYER1 ? alpha : beta;
}
void computer_move() {
int i, j, k, score, best_i = -1, best_j = -1, best_score = -1;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = PLAYER2;
score = alphabeta(MAX_DEPTH, -1000000, 1000000, PLAYER1);
board[i][j] = EMPTY;
if (score > best_score) {
best_score = score;
best_i = i;
best_j = j;
}
}
}
}
board[best_i][best_j] = PLAYER2;
printf("Computer move: (%d, %d)\n", best_i + 1, best_j + 1);
}
void print_board() {
int i, j;
printf(" ");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%c ", 'a' + i);
}
printf("\n");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%2d", i + 1);
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == PLAYER1) {
printf("●");
} else if (board[i][j] == PLAYER2) {
printf("○");
} else {
printf("╋");
}
printf(" ");
}
printf("%d\n", i + 1);
}
printf(" ");
for (i = 0; i < BOARD_SIZE; i++) {
printf("%c ", 'a' + i);
}
printf("\n");
}
int main() {
int i, j, player = PLAYER1;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
while (1) {
print_board();
if (player == PLAYER1) {
printf("Your move: ");
scanf("%d %d", &i, &j);
if (i < 1 || i > BOARD_SIZE || j < 1 || j > BOARD_SIZE || board[i - 1][j - 1] != EMPTY) {
printf("Invalid move.\n");
continue;
}
board[i - 1][j - 1] = PLAYER1;
} else {
computer_move();
}
if (evaluate(player) >= 1000000) {
print_board();
printf("Player %d wins!\n", player);
break;
}
player = player == PLAYER1 ? PLAYER2 : PLAYER1;
}
return 0;
}
```
这个代码实现了一个简单的六子棋游戏,玩家执黑棋,电脑执白棋。玩家可以通过在终端输入坐标来下棋,电脑则使用博弈树和α-β剪枝算法来计算最优的下棋位置。evaluate函数用于评估一个玩家在当前棋局中的得分,alphabeta函数用于搜索博弈树并剪枝。需要注意的是,这个代码没有进行任何优化,因此可能会比较慢,特别是在搜索深度较大的情况下。
阅读全文