我一直在尝试为一个简单的游戏实现一个 minMax 算法(稍后将尝试进行字母修剪)......我看过很多伪代码和教程,但我就是无法让它工作......
一点帮助将不胜感激:)
这是相关的类...(为清楚起见,删除了实现)
class Board { //Stores board state, Immutable
Board playMove(Move m); //generates new Board after playing "Move m"
List<Move> nextMoves(Move m); // generates all possible moves, previous move is required to decide the validity of the next moves
boolean isTerminal(); //board at terminal state?
}
class Move { //stores positions played and score gained from that move
}
这是我的最小-最大实现......有人可以指出我做错了什么吗?谢谢你。
private Move bestMove = null; // field variable
private int maxMove(Board board, Move prevMove, int myScore, int oppnScore) {
out("maxMove " + board );
if(board.isTerminal()) {
return myScore - oppnScore;
}
int mx = Integer.MIN_VALUE;
for(Move nxtMove: board.nextMoves(prevMove)) {
int k = minMove(board.playMove(nxtMove),
nxtMove,
myScore + nxtMove.score,
oppnScore);
if(k > mx) {
mx = k;
bestMove = nxtMove;
}
}
return mx;
}
private int minMove(Board board, Move prevMove, int myScore, int oppnScore) {
if(board.isTerminal()) {
return myScore - oppnScore;
}
out("minMove " + board );
int mn = Integer.MAX_VALUE;
for(Move nxtMove: board.nextMoves(prevMove)) {
int k = maxMove(board.playMove(nxtMove),
nxtMove,
myScore,
oppnScore + nxtMove.score);
if(k < mn) {
mn = k;
bestMove = nxtMove;
}
}
return mn;
}
编辑:游戏的简要说明如下,你面前有一定数量的不同面额的硬币。您和另一名玩家轮流从任一侧(左侧或右侧)取出一枚硬币。硬币的面额表示您为该动作得分。某些硬币具有特殊含义,例如选择 X 表示您将跳过一个回合,或者 Y 表示您将再获得一个回合。你的目标是比你的对手得分更多。