六子棋博弈树C语言代码实现
时间: 2023-11-06 15:51:56 浏览: 143
六子棋博弈树的实现可以使用alpha-beta剪枝算法来提高搜索效率。以下是一个简单的C语言代码实现:
```c
#include <stdio.h>
#define ROW 6
#define COL 6
#define MAX_DEPTH 3
int board[ROW][COL] = {0}; // 棋盘
int max_score = 100; // 最大分数
int min_score = -100; // 最小分数
// 判断是否赢了
int check_win(int player) {
int i, j, count;
// 横向判断
for (i = 0; i < ROW; i++) {
count = 0;
for (j = 0; j < COL; j++) {
if (board[i][j] == player) {
count++;
if (count == 6) {
return 1;
}
} else {
count = 0;
}
}
}
// 纵向判断
for (j = 0; j < COL; j++) {
count = 0;
for (i = 0; i < ROW; i++) {
if (board[i][j] == player) {
count++;
if (count == 6) {
return 1;
}
} else {
count = 0;
}
}
}
// 斜向判断
for (i = 0; i < ROW - 5; i++) {
for (j = 0; j < COL - 5; j++) {
if (board[i][j] == player && board[i+1][j+1] == player && board[i+2][j+2] == player
&& board[i+3][j+3] == player && board[i+4][j+4] == player && board[i+5][j+5] == player) {
return 1;
}
if (board[i][j+5] == player && board[i+1][j+4] == player && board[i+2][j+3] == player
&& board[i+3][j+2] == player && board[i+4][j+1] == player && board[i+5][j] == player) {
return 1;
}
}
}
return 0;
}
// 评估当前局面分数
int evaluate(int player) {
if (check_win(player)) {
return max_score;
} else if (check_win(3-player)) {
return min_score;
} else {
// 计算当前局面分数
int i, j, score = 0;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == player) {
score++;
} else if (board[i][j] == 3-player) {
score--;
}
}
}
return score;
}
}
// alpha-beta剪枝搜索算法
int alpha_beta(int depth, int alpha, int beta, int player) {
if (depth == 0) {
return evaluate(player);
}
int i, j, score;
if (player == 1) {
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == 0) {
board[i][j] = player;
score = alpha_beta(depth-1, alpha, beta, 3-player);
board[i][j] = 0;
if (score > alpha) {
alpha = score;
}
if (alpha >= beta) {
return alpha;
}
}
}
}
return alpha;
} else {
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == 0) {
board[i][j] = player;
score = alpha_beta(depth-1, alpha, beta, 3-player);
board[i][j] = 0;
if (score < beta) {
beta = score;
}
if (alpha >= beta) {
return beta;
}
}
}
}
return beta;
}
}
// 电脑下棋
void computer_play(int player) {
int i, j, max_score = min_score, score;
int x, y;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == 0) {
board[i][j] = player;
score = alpha_beta(MAX_DEPTH, min_score, max_score, 3-player);
board[i][j] = 0;
if (score > max_score) {
max_score = score;
x = i;
y = j;
}
}
}
}
board[x][y] = player;
}
// 打印棋盘
void print_board() {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
printf("%c ", board[i][j] == 0 ? '-' : board[i][j] == 1 ? 'X' : 'O');
}
printf("\n");
}
printf("\n");
}
int main() {
int player = 1; // 玩家先手
int x, y;
while (1) {
if (player == 1) {
printf("请玩家输入下棋位置(x,y):");
scanf("%d,%d", &x, &y);
if (board[x][y] != 0) {
printf("该位置已有棋子,请重新输入!\n");
continue;
}
board[x][y] = player;
} else {
computer_play(player);
}
print_board();
if (check_win(player)) {
printf("恭喜玩家%d胜利!\n", player);
break;
}
if (player == 1) {
player = 2;
} else {
player = 1;
}
}
return 0;
}
```
以上代码实现了一个简单的六子棋博弈树,玩家可以与电脑进行对弈。其中alpha-beta剪枝算法可以大大提高搜索效率,使得电脑更加智能。
阅读全文