Implémenter DominosHeuristics et MiniMax
- hVertical/hHorizontal : différence de coups disponibles entre les deux rôles - MiniMax : bestMove + maxValue/minValue récursifs avec comptage nœuds/feuilles Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -62,9 +62,22 @@ public class MiniMax<Move extends IMove,Role extends IRole,Board extends IBoard<
|
||||
@Override
|
||||
public Move bestMove(Board board, Role playerRole) {
|
||||
System.out.println("[MiniMax]");
|
||||
nbNodes = 0;
|
||||
nbLeaves = 0;
|
||||
|
||||
Move bestMove = null;
|
||||
// TODO
|
||||
int bestValue = IHeuristic.MIN_VALUE;
|
||||
|
||||
for (Move move : board.possibleMoves(playerMaxRole)) {
|
||||
Board successor = board.play(move, playerMaxRole);
|
||||
int value = minValue(successor, 1);
|
||||
if (value > bestValue) {
|
||||
bestValue = value;
|
||||
bestMove = move;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Nodes=" + nbNodes + " Leaves=" + nbLeaves);
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
@@ -80,5 +93,31 @@ public class MiniMax<Move extends IMove,Role extends IRole,Board extends IBoard<
|
||||
* PRIVATE METHODS ===============
|
||||
*/
|
||||
|
||||
//TODO
|
||||
private int maxValue(Board board, int depth) {
|
||||
nbNodes++;
|
||||
if (board.isGameOver() || depth >= depthMax) {
|
||||
nbLeaves++;
|
||||
return h.eval(board, playerMaxRole);
|
||||
}
|
||||
int value = IHeuristic.MIN_VALUE;
|
||||
for (Move move : board.possibleMoves(playerMaxRole)) {
|
||||
Board successor = board.play(move, playerMaxRole);
|
||||
value = Math.max(value, minValue(successor, depth + 1));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private int minValue(Board board, int depth) {
|
||||
nbNodes++;
|
||||
if (board.isGameOver() || depth >= depthMax) {
|
||||
nbLeaves++;
|
||||
return h.eval(board, playerMaxRole);
|
||||
}
|
||||
int value = IHeuristic.MAX_VALUE;
|
||||
for (Move move : board.possibleMoves(playerMinRole)) {
|
||||
Board successor = board.play(move, playerMinRole);
|
||||
value = Math.min(value, maxValue(successor, depth + 1));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user