写出C语言五子棋的算法代码
时间: 2023-08-20 15:06:06 浏览: 211
以下是一个简单的五子棋AI算法的C语言代码示例,采用的是极大极小搜索算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define ROWS 15 // 棋盘行数
#define COLS 15 // 棋盘列数
#define BLACK 1 // 黑子
#define WHITE 2 // 白子
#define EMPTY 0 // 空格
int board[ROWS][COLS]; // 棋盘数组
int player; // 玩家(黑子或白子)
int ai; // AI(黑子或白子)
// 初始化棋盘
void init_board() {
int i, j;
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
board[i][j] = EMPTY;
}
}
}
// 打印棋盘
void print_board() {
int i, j;
printf(" ");
for (i = 0; i < COLS; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 0; i < ROWS; i++) {
printf("%d ", i);
for (j = 0; j < COLS; j++) {
if (board[i][j] == EMPTY) {
printf("+ ");
} else if (board[i][j] == BLACK) {
printf("@ ");
} else {
printf("O ");
}
}
printf("\n");
}
}
// 判断是否赢了
int win(int x, int y, int player) {
int i, j;
int count;
// 判断横向是否连成五子
count = 0;
for (j = y - 4; j <= y; j++) {
if (j < 0 || j > COLS - 5) {
continue;
}
if (board[x][j] == player &&
board[x][j+1] == player &&
board[x][j+2] == player &&
board[x][j+3] == player &&
board[x][j+4] == player) {
return 1;
}
}
// 判断纵向是否连成五子
count = 0;
for (i = x - 4; i <= x; i++) {
if (i < 0 || i > ROWS - 5) {
continue;
}
if (board[i][y] == player &&
board[i+1][y] == player &&
board[i+2][y] == player &&
board[i+3][y] == player &&
board[i+4][y] == player) {
return 1;
}
}
// 判断右上方是否连成五子
count = 0;
for (i = x + 4, j = y - 4; i >= 0 && i <= ROWS - 5 && j >= 0 && j <= COLS - 5; i--, 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) {
return 1;
}
}
// 判断右下方是否连成五子
count = 0;
for (i = x - 4, j = y - 4; i <= x && i >= 0 && j <= y && j >= 0; i++, 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) {
return 1;
}
}
return 0;
}
// 估值函数
int score(int x, int y, int player) {
int i, j;
int count;
int score = 0;
// 判断横向是否连成五子
count = 0;
for (j = y - 4; j <= y; j++) {
if (j < 0 || j > COLS - 5) {
continue;
}
for (i = x; i <= x + 4; i++) {
if (board[i][j] == player) {
count++;
}
}
if (count == 5) {
score += 100;
} else if (count == 4) {
score += 10;
}
count = 0;
}
// 判断纵向是否连成五子
count = 0;
for (i = x - 4; i <= x; i++) {
if (i < 0 || i > ROWS - 5) {
continue;
}
for (j = y; j <= y + 4; j++) {
if (board[i][j] == player) {
count++;
}
}
if (count == 5) {
score += 100;
} else if (count == 4) {
score += 10;
}
count = 0;
}
// 判断右上方是否连成五子
count = 0;
for (i = x + 4, j = y - 4; i >= 0 && i <= ROWS - 5 && j >= 0 && j <= COLS - 5; i--, j++) {
for (int k = 0; k < 5; k++) {
if (board[i-k][j+k] == player) {
count++;
}
}
if (count == 5) {
score += 100;
} else if (count == 4) {
score += 10;
}
count = 0;
}
// 判断右下方是否连成五子
count = 0;
for (i = x - 4, j = y - 4; i <= x && i >= 0 && j <= y && j >= 0; i++, j++) {
for (int k = 0; k < 5; k++) {
if (board[i+k][j+k] == player) {
count++;
}
}
if (count == 5) {
score += 100;
} else if (count == 4) {
score += 10;
}
count = 0;
}
return score;
}
// 极大极小搜索算法
int min_max_search(int depth, int alpha, int beta) {
int i, j;
int best_score = -99999;
int score;
int x, y;
if (depth == 0) {
return score;
}
// 极大层
if (player == ai) {
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = ai;
score = score(i, j, ai);
if (win(i, j, ai)) {
board[i][j] = EMPTY;
return 99999;
} else {
score += min_max_search(depth - 1, alpha, beta);
if (score > best_score) {
best_score = score;
x = i;
y = j;
}
board[i][j] = EMPTY;
if (best_score > alpha) {
alpha = best_score;
}
if (alpha >= beta) {
return best_score;
}
}
}
}
}
return best_score;
// 极小层
} else {
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = player;
score = score(i, j, player);
if (win(i, j, player)) {
board[i][j] = EMPTY;
return -99999;
} else {
score -= min_max_search(depth - 1, alpha, beta);
if (score < best_score) {
best_score = score;
x = i;
y = j;
}
board[i][j] = EMPTY;
if (best_score < beta) {
beta = best_score;
}
if (alpha >= beta) {
return best_score;
}
}
}
}
}
return best_score;
}
}
// AI下棋
void ai_play() {
int i, j;
int best_score = -99999;
int score;
int x, y;
// 极大极小搜索算法
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
if (board[i][j] == EMPTY) {
board[i][j] = ai;
score = score(i, j, ai);
if (win(i, j, ai)) {
board[i][j] = EMPTY;
printf("AI: (%d, %d)\n", j, i);
return;
} else {
score += min_max_search(2, -99999, 99999);
if (score > best_score) {
best_score = score;
x = i;
y = j;
}
board[i][j] = EMPTY;
}
}
}
}
// 下棋
board[x][y] = ai;
printf("AI: (%d, %d)\n", y, x);
}
// 玩家下棋
void player_play() {
int x, y;
printf("Player: ");
scanf("%d %d", &x, &y);
while (board[x][y] != EMPTY) {
printf("Invalid move, please try again.\n");
printf("Player: ");
scanf("%d %d", &x, &y);
}
board[x][y] = player;
}
int main() {
init_board();
printf("Please select a player (1:black, 2:white): ");
scanf("%d", &player);
while (player != BLACK && player != WHITE) {
printf("Invalid input, please try again.\n");
printf("Please select a player (1:black, 2:white): ");
scanf("%d", &player);
}
ai = (player == BLACK) ? WHITE : BLACK;
print_board();
while (1) {
if (player == BLACK) {
player_play();
print_board();
if (win(x, y, player)) {
printf("Player wins!\n");
break;
}
} else {
ai_play();
print_board();
if (win(x, y, ai)) {
printf("AI wins!\n");
break;
}
}
player = (player == BLACK) ? WHITE : BLACK;
}
return 0;
}
```
需要注意的是,以上代码只是一个简单的示例,实际上五子棋AI算法的实现要更加复杂和精细。
阅读全文