From 68cf66b19c1caf841008ca6e3756d9c149b6df5b Mon Sep 17 00:00:00 2001 From: kerboul Date: Tue, 7 Apr 2026 14:42:57 +0200 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9menter=20DominosHeuristics=20et=20Min?= =?UTF-8?q?iMax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../java/games/dominos/DominosHeuristics.java | 14 +++--- .../iialib/games/algs/algorithms/MiniMax.java | 43 ++++++++++++++++++- 2 files changed, 46 insertions(+), 11 deletions(-) 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; + } }