diff --git a/src/main/java/games/dominos/DominosHeuristics.java b/src/main/java/games/dominos/DominosHeuristics.java index 9ea8e96..47d57c6 100644 --- a/src/main/java/games/dominos/DominosHeuristics.java +++ b/src/main/java/games/dominos/DominosHeuristics.java @@ -4,19 +4,15 @@ import iialib.games.algs.IHeuristic; public class DominosHeuristics { + // Avantage du joueur VERTICAL : nombre de coups verticaux disponibles moins coups horizontaux public static IHeuristic hVertical = (board,role) -> { - /* TODO */ - - - return 0; // TODO + return board.nbVerticalMoves() - board.nbHorizontalMoves(); }; - + + // Avantage du joueur HORIZONTAL : nombre de coups horizontaux disponibles moins coups verticaux public static IHeuristic hHorizontal = (board,role) -> { - /* TODO */ - - - return 0; // TODO + return board.nbHorizontalMoves() - board.nbVerticalMoves(); }; } diff --git a/src/main/java/iialib/games/algs/algorithms/MiniMax.java b/src/main/java/iialib/games/algs/algorithms/MiniMax.java index c634305..97c5876 100644 --- a/src/main/java/iialib/games/algs/algorithms/MiniMax.java +++ b/src/main/java/iialib/games/algs/algorithms/MiniMax.java @@ -62,9 +62,22 @@ public class MiniMax bestValue) { + bestValue = value; + bestMove = move; + } + } + + System.out.println("Nodes=" + nbNodes + " Leaves=" + nbLeaves); return bestMove; } @@ -80,5 +93,31 @@ public class MiniMax= 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; + } }