六子棋的alpha-beta剪枝java代码
时间: 2023-06-15 16:05:41 浏览: 137
下面是六子棋的alpha-beta剪枝Java代码:
```java
public class AlphaBetaPruning {
private static final int MAX_DEPTH = 5;
private static final int WIN_SCORE = 100000;
private static final int[][] DIRECTIONS = {{1, 0}, {0, 1}, {1, 1}, {1, -1}};
public int[] getBestMove(int[][] board) {
int bestScore = Integer.MIN_VALUE;
int[] bestMove = new int[2];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 0) {
board[i][j] = 1;
int score = alphaBeta(board, MAX_DEPTH, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
board[i][j] = 0;
if (score > bestScore) {
bestScore = score;
bestMove[0] = i;
bestMove[1] = j;
}
}
}
}
return bestMove;
}
private int alphaBeta(int[][] board, int depth, int alpha, int beta, boolean isMaxPlayer) {
int score = evaluate(board);
if (score == WIN_SCORE || score == -WIN_SCORE || depth == 0) {
return score;
}
if (isMaxPlayer) {
int maxScore = Integer.MIN_VALUE;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 0) {
board[i][j] = 1;
int currentScore = alphaBeta(board, depth - 1, alpha, beta, false);
board[i][j] = 0;
maxScore = Math.max(maxScore, currentScore);
alpha = Math.max(alpha, currentScore);
if (beta <= alpha) {
return maxScore;
}
}
}
}
return maxScore;
} else {
int minScore = Integer.MAX_VALUE;
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 0) {
board[i][j] = 2;
int currentScore = alphaBeta(board, depth - 1, alpha, beta, true);
board[i][j] = 0;
minScore = Math.min(minScore, currentScore);
beta = Math.min(beta, currentScore);
if (beta <= alpha) {
return minScore;
}
}
}
}
return minScore;
}
}
private int evaluate(int[][] board) {
int score = 0;
// Check rows
for (int i = 0; i < board.length; i++) {
score += evaluateLine(board[i]);
}
// Check columns
for (int j = 0; j < board[0].length; j++) {
int[] column = new int[board.length];
for (int i = 0; i < board.length; i++) {
column[i] = board[i][j];
}
score += evaluateLine(column);
}
// Check diagonals
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
for (int[] direction : DIRECTIONS) {
int x = i + direction[0] * 5;
int y = j + direction[1] * 5;
if (x >= 0 && x < board.length && y >= 0 && y < board[0].length) {
int[] diagonal = new int[6];
for (int k = 0; k < 6; k++) {
diagonal[k] = board[i + direction[0] * k][j + direction[1] * k];
}
score += evaluateLine(diagonal);
}
}
}
}
return score;
}
private int evaluateLine(int[] line) {
int humanCount = 0;
int computerCount = 0;
for (int i = 0; i < line.length; i++) {
if (line[i] == 1) {
humanCount++;
} else if (line[i] == 2) {
computerCount++;
}
}
if (humanCount == 0) {
return (int) Math.pow(10, computerCount);
} else if (computerCount == 0) {
return -(int) Math.pow(10, humanCount);
}
return 0;
}
}
```
这段代码使用了递归实现了alpha-beta剪枝算法,其中`getBestMove`方法返回最佳下棋位置,`alphaBeta`方法返回当前局面下的最大得分或最小得分,`evaluate`方法计算局面得分,`evaluateLine`方法计算一行、一列或一条对角线的得分。