我尝试在Java中编写一个用于连接四游戏的MinMax程序,但该程序也应该适用于其他游戏。但是,我遇到了一个问题,我几天无法通过。节点的值设置不正确。我分享我负责生成树的代码片段。MinMax - 生成游戏树
也许你会注意到我犯了一个错误。
如果有人能帮助我,我会很高兴。
public Node generateTree(Board board, int depth) {
Node rootNode = new Node(board);
generateSubtree(rootNode, depth);
minMax(rootNode, depth);
return rootNode;
}
private void generateSubtree(Node subRootNode, int depth) {
Board board = subRootNode.getBoard();
if (depth == 0) {
subRootNode.setValue(board.evaluateBoard());
return;
}
for (Move move : board.generateMoves()) {
Board tempBoard = board.makeMove(move);
Node tempNode = new Node(tempBoard);
subRootNode.addChild(tempNode);
generateSubtree(tempNode, depth - 1);
}
}
public void minMax(Node rootNode, int depth) {
maxMove(rootNode, depth);
}
public int maxMove(Node node, int depth) {
if (depth == 0) {
return node.getValue();
}
int bestValue = Integer.MIN_VALUE;
for (Node childNode : node.getChildren()) {
int tempValue = minMove(childNode, depth - 1);
childNode.setValue(tempValue);
if (tempValue > bestValue) {
bestValue = tempValue;
}
}
return bestValue;
}
public int minMove(Node node, int depth) {
if (depth == 0) {
return node.getValue();
}
int bestValue = Integer.MAX_VALUE;
for (Node childNode : node.getChildren()) {
int tempValue = maxMove(childNode, depth - 1);
childNode.setValue(tempValue);
if (tempValue < bestValue) {
bestValue = tempValue;
}
}
return bestValue;
}
局类是板状态的表示。
移动类执行移动(整数[0-8]为井字游戏,[0-6]为连接四)。
节点类拥有移动和价值移动是多好。并且,拥有所有的孩子。
在我用这个方法这样的代码:
Node newNode = minmax.generateTree(board, depth, board.getPlayer());
Move newMove = new TicTacToeMove(board.getPlayer(), newNode.getBestMove().getMove(), depth);
board = board.makeMove(newMove);
当很明显,鉴于此举是败着(或获奖),我没有收到这个举动。
如果您可以包含您输入内容的示例,您认为结果应该是什么以及它是什么,那将会很好。 – Mark
嗨, **董事会**类是董事会国家的代表。 **移动**类执行移动(tictactoe的整数[0-8],连接四的[0-6])。 **节点**类拥有移动和价值移动是多好。还拥有所有的孩子。 在代码中,我使用这样的方法: 'Node newNode = minmax.generateTree(board,depth,board.getPlayer()); 移动newMove = new TicTacToeMove(board.getPlayer(),newNode.getBestMove()。getMove(),depth); board = board.makeMove(newMove);' 而且当其明显的举动是一个失败的举动(或胜利)时,我没有收到这个举动。 – Xing92
什么是“我没有收到此举”。意思?这是一款网络游戏吗?输赢是不是发送给你?你的意思是它没有放在电路板上?你的树没有获得新的条目吗? – Mark