【发布时间】:2021-08-29 06:47:44
【问题描述】:
我试图让 Alpha-beta 修剪工作,但与我的 Minimax 函数相比,它给了我完全错误的动作。这是我现在运行良好的 Minimax 函数。
float Minimax(char[,] _board, int depth, bool isMax) {
if (depth == 0 || isFull(_board)) {
EvaluateBoard(_board);
}
if (isMax) {
float bestScore = -Mathf.Infinity;
float score = -Mathf.Infinity;
for (int i = 0; i < 7; i++) {
char[,] board = (char[, ]) _board.Clone();
if (Play(board, i, false)) {
score = Minimax(board, depth - 1, false);
bestScore = Mathf.Max(score, bestScore);
}
}
return bestScore;
} else {
float bestScore = Mathf.Infinity;
float score = Mathf.Infinity;
for (int i = 0; i < 7; i++) {
char[,] board = (char[, ]) _board.Clone();
if (Play(board, i, true)) {
score = Minimax(board, depth - 1, true);
bestScore = Mathf.Min(score, bestScore);
}
}
return bestScore;
}
}
这是我的 Alphabeta 修剪函数
float ABPruning(char[,] _board, int depth, float alpha, float beta, bool isMax) {
if (depth == 0 || isFull(_board)) {
return EvaluateBoard(_board);
}
if (isMax) {
float bestScore = -Mathf.Infinity;
for (int i = 0; i < 7; i++) {
char[, ] board = (char[,]) _board.Clone();
if (Play(board, i, false)) {
bestScore = ABPruning(board, depth - 1, alpha, beta, false);
alpha = Mathf.Max(alpha, bestScore);
if (beta<=alpha) {
return bestScore;
}
}
}
return bestScore;
} else {
float bestScore = Mathf.Infinity;
for (int i = 0; i < 7; i++) {
char[, ] board = (char[,]) _board.Clone();
if (Play(board, i, true)) {
bestScore = ABPruning(board, depth - 1, alpha, beta, true);
beta = Mathf.Min(beta, bestScore);
if (beta<=alpha) {
break;
}
}
}
return bestScore;
}
}
两者都使用相同的评估,不确定这里有什么问题。感谢您的帮助。
【问题讨论】:
标签: c# algorithm minimax alpha-beta-pruning