c语言实现五子棋极大极小算法
时间: 2023-12-07 19:40:10 浏览: 51
以下是C语言实现五子棋极大极小算法的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 15
#define MAX_DEPTH 5
int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
int player; // 玩家,1表示黑棋,2表示白棋
// 初始化棋盘
void init_board() {
memset(board, 0, sizeof(board));
}
// 打印棋盘
void print_board() {
printf(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i);
}
printf("\n");
for (int i = 0; i < BOARD_SIZE; i++) {
printf("%d ", i);
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_win(int x, int y) {
int i, j, k;
int count;
// 横向
count = 1;
for (i = x - 1; i >= 0 && board[i][y] == board[x][y]; i--) {
count++;
}
for (i = x + 1; i < BOARD_SIZE && board[i][y] == board[x][y]; i++) {
count++;
}
if (count >= 5) {
return 1;
}
// 纵向
count = 1;
for (j = y - 1; j >= 0 && board[x][j] == board[x][y]; j--) {
count++;
}
for (j = y + 1; j < BOARD_SIZE && board[x][j] == board[x][y]; j++) {
count++;
}
if (count >= 5) {
return 1;
}
// 左上到右下
count = 1;
for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == board[x][y]; i--, j--) {
count++;
}
for (i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == board[x][y]; i++, j++) {
count++;
}
if (count >= 5) {
return 1;
}
// 右上到左下
count = 1;
for (i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == board[x][y]; i--, j++) {
count++;
}
for (i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == board[x][y]; i++, j--) {
count++;
}
if (count >= 5) {
return 1;
}
return 0;
}
// 评估函数
int evaluate() {
int i, j, k;
int score = 0;
// 横向
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE - 4; j++) {
int count1 = 0, count2 = 0;
for (k = 0; k < 5; k++) {
if (board[i][j + k] == 1) {
count1++;
} else if (board[i][j + k] == 2) {
count2++;
}
}
if (count1 == 0 && count2 > 0) {
score -= count2 * count2;
} else if (count2 == 0 && count1 > 0) {
score += count1 * count1;
}
}
}
// 纵向
for (j = 0; j < BOARD_SIZE; j++) {
for (i = 0; i < BOARD_SIZE - 4; i++) {
int count1 = 0, count2 = 0;
for (k = 0; k < 5; k++) {
if (board[i + k][j] == 1) {
count1++;
} else if (board[i + k][j] == 2) {
count2++;
}
}
if (count1 == 0 && count2 > 0) {
score -= count2 * count2;
} else if (count2 == 0 && count1 > 0) {
score += count1 * count1;
}
}
}
// 左上到右下
for (i = 0; i < BOARD_SIZE - 4; i++) {
for (j = 0; j < BOARD_SIZE - 4; j++) {
int count1 = 0, count2 = 0;
for (k = 0; k < 5; k++) {
if (board[i + k][j + k] == 1) {
count1++;
} else if (board[i + k][j + k] == 2) {
count2++;
}
}
if (count1 == 0 && count2 > 0) {
score -= count2 * count2;
} else if (count2 == 0 && count1 > 0) {
score += count1 * count1;
}
}
}
// 右上到左下
for (i = 0; i < BOARD_SIZE - 4; i++) {
for (j = 4; j < BOARD_SIZE; j++) {
int count1 = 0, count2 = 0;
for (k = 0; k < 5; k++) {
if (board[i + k][j - k] == 1) {
count1++;
} else if (board[i + k][j - k] == 2) {
count2++;
}
}
if (count1 == 0 && count2 > 0) {
score -= count2 * count2;
} else if (count2 == 0 && count1 > 0) {
score += count1 * count1;
}
}
}
return score;
}
// 极大极小搜索算法
int max_min(int depth, int alpha, int beta) {
int i, j, k;
int score;
int best_i = -1, best_j = -1;
if (depth == MAX_DEPTH) {
return evaluate();
}
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
board[i][j] = player;
player = 3 - player;
score = -max_min(depth + 1, -beta, -alpha);
player = 3 - player;
board[i][j] = 0;
if (score > alpha) {
alpha = score;
best_i = i;
best_j = j;
}
if (alpha >= beta) {
goto end;
}
}
}
}
end:
if (depth == 0) {
return best_i * BOARD_SIZE + best_j;
} else {
return alpha;
}
}
// 电脑下棋
void computer_move() {
int i, j;
int pos = max_min(0, -1000000, 1000000);
i = pos / BOARD_SIZE;
j = pos % BOARD_SIZE;
board[i][j] = 2;
}
int main() {
init_board();
print_board();
while (1) {
// 玩家下棋
int x, y;
printf("请输入坐标(x y): ");
scanf("%d %d", &x, &y);
if (board[x][y] != 0) {
printf("该位置已经有棋子了,请重新输入!\n");
continue;
}
board[x][y] = 1;
print_board();
if (is_win(x, y)) {
printf("你赢了!\n");
break;
}
// 电脑下棋
printf("电脑正在思考...\n");
computer_move();
print_board();
if (is_win(x, y)) {
printf("电脑赢了!\n");
break;
}
}
return 0;
}
```