c语言五子棋ai算法代码
时间: 2023-08-01 20:09:29 浏览: 154
五子棋的AI算法C语言
下面是一个简单的 C 语言五子棋 AI 算法代码示例,使用了极大极小值算法和 alpha-beta 剪枝优化:
```c
#include <stdio.h>
#include <stdbool.h>
#define BOARD_SIZE 15
// 棋盘
int board[BOARD_SIZE][BOARD_SIZE];
// 棋子颜色
#define EMPTY 0
#define BLACK 1
#define WHITE 2
// 赢法数组
int wins[BOARD_SIZE][BOARD_SIZE][572];
int win_count = 0;
// 落子得分数组
int score[BOARD_SIZE][BOARD_SIZE];
// 初始化赢法数组
void init_wins() {
// 横向赢法
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
for (int k = 0; k < 5; k++) {
wins[i][j + k][win_count] = 1;
}
win_count++;
}
}
// 竖向赢法
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
for (int k = 0; k < 5; k++) {
wins[j + k][i][win_count] = 1;
}
win_count++;
}
}
// 斜向赢法
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = 0; j < BOARD_SIZE - 4; j++) {
for (int k = 0; k < 5; k++) {
wins[i + k][j + k][win_count] = 1;
}
win_count++;
}
}
// 反斜向赢法
for (int i = 0; i < BOARD_SIZE - 4; i++) {
for (int j = BOARD_SIZE - 1; j >= 4; j--) {
for (int k = 0; k < 5; k++) {
wins[i + k][j - k][win_count] = 1;
}
win_count++;
}
}
}
// 初始化落子得分数组
void init_score() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
score[i][j] = 0;
}
}
}
// 计算某个位置的落子得分
int calculate_score(int x, int y, int color) {
int score = 0;
for (int i = 0; i < win_count; i++) {
if (wins[x][y][i]) {
if (board[x][y] == color) {
score += 10;
} else if (board[x][y] == EMPTY) {
if (wins[x][y][i] == 1) {
score += 1;
} else if (wins[x][y][i] == 2) {
score += 5;
} else if (wins[x][y][i] == 3) {
score += 100;
} else if (wins[x][y][i] == 4) {
score += 1000;
}
}
}
}
return score;
}
// 计算所有位置的落子得分
void calculate_all_score(int color) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
score[i][j] = calculate_score(i, j, color);
}
}
}
}
// 判断是否胜利
bool is_win(int x, int y, int color) {
for (int i = 0; i < win_count; i++) {
if (wins[x][y][i]) {
bool win = true;
for (int j = 0; j < 5; j++) {
if (board[x][y] != color) {
win = false;
break;
}
}
if (win) {
return true;
}
}
}
return false;
}
// 极大极小值算法
int alpha_beta(int depth, int alpha, int beta, int color) {
if (depth == 0) {
return 0;
}
calculate_all_score(color);
// 极大值节点
if (color == BLACK) {
int max_score = -1000000;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
if (is_win(i, j, BLACK)) {
return 1000000;
}
int s = score[i][j];
if (s > 0) {
board[i][j] = BLACK;
int v = alpha_beta(depth - 1, alpha, beta, WHITE);
board[i][j] = EMPTY;
if (v > max_score) {
max_score = v;
}
if (v > alpha) {
alpha = v;
}
if (alpha >= beta) {
return max_score;
}
}
}
}
}
return max_score;
}
// 极小值节点
else {
int min_score = 1000000;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
if (is_win(i, j, WHITE)) {
return -1000000;
}
int s = score[i][j];
if (s > 0) {
board[i][j] = WHITE;
int v = alpha_beta(depth - 1, alpha, beta, BLACK);
board[i][j] = EMPTY;
if (v < min_score) {
min_score = v;
}
if (v < beta) {
beta = v;
}
if (beta <= alpha) {
return min_score;
}
}
}
}
}
return min_score;
}
}
// AI 下棋
void ai_move(int *x, int *y) {
int max_score = -1000000;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == EMPTY) {
int s = score[i][j];
if (s > 0) {
board[i][j] = BLACK;
int v = alpha_beta(5, -1000000, 1000000, WHITE);
board[i][j] = EMPTY;
if (v > max_score) {
max_score = v;
*x = i;
*y = j;
}
}
}
}
}
}
int main() {
// 初始化棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = EMPTY;
}
}
// 初始化赢法数组
init_wins();
// 主循环
while (true) {
// AI 下棋
int x, y;
ai_move(&x, &y);
board[x][y] = BLACK;
printf("AI: (%d, %d)\n", x, y);
// 打印棋盘
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
printf("%c ", board[i][j] == EMPTY ? '+' : (board[i][j] == BLACK ? 'X' : 'O'));
}
printf("\n");
}
// 判断胜负
if (is_win(x, y, BLACK)) {
printf("AI win!\n");
break;
}
// 玩家下棋
printf("Enter your move (e.g. 7 7): ");
int px, py;
scanf("%d %d", &px, &py);
board[px][py] = WHITE;
// 判断胜负
if (is_win(px, py, WHITE)) {
printf("You win!\n");
break;
}
}
return 0;
}
```
注意,这是一个非常简单的示例代码,实际应用中可能需要更复杂和更高效的算法。
阅读全文