C语言实现六子棋使用α-β剪枝算法的代码
时间: 2023-06-27 17:05:35 浏览: 145
以下是使用α-β剪枝算法实现的C语言六子棋代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 6
#define MAX_DEPTH 5
typedef struct {
int row;
int col;
} Move;
int board[BOARD_SIZE][BOARD_SIZE];
int player = 1; // 1 for player 1, -1 for player 2
int maxDepth = MAX_DEPTH;
int evaluate() {
int score = 0;
int i, j;
// check rows
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
int openEnds = 0;
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
// check columns
for (j = 0; j < BOARD_SIZE; j++) {
int count = 0;
int openEnds = 0;
for (i = 0; i < BOARD_SIZE; i++) {
if (board[i][j] == player) {
count++;
} else if (board[i][j] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
// check diagonals
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
int openEnds = 0;
for (j = 0; j < BOARD_SIZE - i; j++) {
if (board[i+j][j] == player) {
count++;
} else if (board[i+j][j] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
for (j = 1; j < BOARD_SIZE; j++) {
int count = 0;
int openEnds = 0;
for (i = 0; i < BOARD_SIZE - j; i++) {
if (board[i][j+i] == player) {
count++;
} else if (board[i][j+i] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
int openEnds = 0;
for (j = 0; j <= i; j++) {
if (board[i-j][j] == player) {
count++;
} else if (board[i-j][j] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
for (j = 1; j < BOARD_SIZE; j++) {
int count = 0;
int openEnds = 0;
for (i = BOARD_SIZE - 1; i >= j; i--) {
if (board[i][j+(BOARD_SIZE-1-i)] == player) {
count++;
} else if (board[i][j+(BOARD_SIZE-1-i)] == -player) {
if (count == 0) {
openEnds++;
} else {
score += count * count;
count = 0;
}
} else if (count > 0) {
openEnds++;
count = 0;
}
}
if (count > 0) {
score += count * count;
}
if (openEnds == 2) {
score += 1;
}
}
return score;
}
int isGameOver() {
int i, j;
// check rows
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
// check columns
for (j = 0; j < BOARD_SIZE; j++) {
int count = 0;
for (i = 0; i < BOARD_SIZE; i++) {
if (board[i][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
// check diagonals
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (j = 0; j < BOARD_SIZE - i; j++) {
if (board[i+j][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
for (j = 1; j < BOARD_SIZE; j++) {
int count = 0;
for (i = 0; i < BOARD_SIZE - j; i++) {
if (board[i][j+i] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
for (i = 0; i < BOARD_SIZE; i++) {
int count = 0;
for (j = 0; j <= i; j++) {
if (board[i-j][j] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
for (j = 1; j < BOARD_SIZE; j++) {
int count = 0;
for (i = BOARD_SIZE - 1; i >= j; i--) {
if (board[i][j+(BOARD_SIZE-1-i)] == player) {
count++;
} else {
count = 0;
}
if (count == 6) {
return 1;
}
}
}
// check for tie
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
return 0;
}
}
}
return 1;
}
int minimax(int depth, int alpha, int beta) {
int i, j, score, bestScore;
Move move;
bestScore = -player*10000;
if (depth == maxDepth || isGameOver()) {
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;
move.row = i;
move.col = j;
score = -minimax(depth+1, -beta, -alpha);
board[i][j] = 0;
if (score > bestScore) {
bestScore = score;
if (depth == 0) {
printf("Best move: row=%d col=%d score=%d\n", i, j, score);
}
}
if (score > alpha) {
alpha = score;
}
if (alpha >= beta) {
return alpha;
}
}
}
}
return bestScore;
}
void makeMove() {
int i, j, score, bestScore;
Move move, bestMove;
bestScore = -player*10000;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 0) {
board[i][j] = player;
move.row = i;
move.col = j;
score = -minimax(1, -player*10000, player*10000);
board[i][j] = 0;
if (score > bestScore) {
bestScore = score;
bestMove = move;
}
}
}
}
board[bestMove.row][bestMove.col] = player;
printf("Player %d moved: row=%d col=%d\n", player, bestMove.row, bestMove.col);
}
void printBoard() {
int i, j;
for (i = 0; i < BOARD_SIZE; i++) {
for (j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == 1) {
printf("X ");
} else if (board[i][j] == -1) {
printf("O ");
} else {
printf("- ");
}
}
printf("\n");
}
}
int main() {
int i, j;
memset(board, 0, sizeof(board));
printf("Starting game...\n");
while (!isGameOver()) {
printBoard();
if (player == 1) {
makeMove();
player = -1;
} else {
printf("Enter row and column for player 2: ");
scanf("%d %d", &i, &j);
board[i][j] = -1;
player = 1;
}
}
printBoard();
if (evaluate() > 0) {
printf("Player 1 wins!\n");
} else if (evaluate() < 0) {
printf("Player 2 wins!\n");
} else {
printf("Tie game!\n");
}
return 0;
}
```
该代码使用了α-β剪枝算法来搜索最优解,其中evaluate()函数用于评估当前局面的得分,isGameOver()函数用于判断游戏是否结束,minimax()函数用于实现α-β剪枝算法,makeMove()函数用于让电脑进行移动,printBoard()函数用于打印当前棋盘。该代码实现了一个简单的人机对战六子棋游戏,可以通过调整maxDepth来控制搜索深度,从而影响电脑的水平。
阅读全文
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)