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:
@@ -4,19 +4,15 @@ import iialib.games.algs.IHeuristic;
|
|||||||
|
|
||||||
public class DominosHeuristics {
|
public class DominosHeuristics {
|
||||||
|
|
||||||
|
// Avantage du joueur VERTICAL : nombre de coups verticaux disponibles moins coups horizontaux
|
||||||
public static IHeuristic<DominosBoard,DominosRole> hVertical = (board,role) -> {
|
public static IHeuristic<DominosBoard,DominosRole> hVertical = (board,role) -> {
|
||||||
/* TODO */
|
return board.nbVerticalMoves() - board.nbHorizontalMoves();
|
||||||
|
|
||||||
|
|
||||||
return 0; // TODO
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Avantage du joueur HORIZONTAL : nombre de coups horizontaux disponibles moins coups verticaux
|
||||||
public static IHeuristic<DominosBoard,DominosRole> hHorizontal = (board,role) -> {
|
public static IHeuristic<DominosBoard,DominosRole> hHorizontal = (board,role) -> {
|
||||||
/* TODO */
|
return board.nbHorizontalMoves() - board.nbVerticalMoves();
|
||||||
|
|
||||||
|
|
||||||
return 0; // TODO
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,9 +62,22 @@ public class MiniMax<Move extends IMove,Role extends IRole,Board extends IBoard<
|
|||||||
@Override
|
@Override
|
||||||
public Move bestMove(Board board, Role playerRole) {
|
public Move bestMove(Board board, Role playerRole) {
|
||||||
System.out.println("[MiniMax]");
|
System.out.println("[MiniMax]");
|
||||||
|
nbNodes = 0;
|
||||||
|
nbLeaves = 0;
|
||||||
|
|
||||||
Move bestMove = null;
|
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;
|
return bestMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,5 +93,31 @@ public class MiniMax<Move extends IMove,Role extends IRole,Board extends IBoard<
|
|||||||
* PRIVATE METHODS ===============
|
* 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