diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bb4456a..64436b1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,9 +6,7 @@ - - diff --git a/src/main/java/laboratoire4/Client.java b/src/main/java/laboratoire4/Client.java index b76468a..623b983 100644 --- a/src/main/java/laboratoire4/Client.java +++ b/src/main/java/laboratoire4/Client.java @@ -40,7 +40,6 @@ public class Client { if (cmd == '5') { stopGame(); - break; } } } catch (IOException e) { diff --git a/src/main/java/laboratoire4/MiniMax.java b/src/main/java/laboratoire4/MiniMax.java index 115ae1f..9226743 100644 --- a/src/main/java/laboratoire4/MiniMax.java +++ b/src/main/java/laboratoire4/MiniMax.java @@ -109,19 +109,89 @@ public class MiniMax { int score = pawns.size() * 5; for (MovingPawn pawn : pawns) { - int row = pawn.getRow(); - int goal = pawn.getPlayer().getGoal(); - - if (row == goal) { - return Integer.MAX_VALUE; - } - - score += Math.abs(goal - row); + score += evaluate(board, pawn); } return score; } + private static int evaluate(MovingBoard game, MovingPawn pawn) { + if (pawn.getRow() == pawn.getPlayer().getGoal()) { + return Integer.MAX_VALUE; + } + + // Favorise les pièces qui peuvent gagner au prochain tour + if (pawn.getRow() + pawn.getDirection() == pawn.getPlayer().getGoal()) { + return Integer.MAX_VALUE / 2; + } + + int score = 0; + score += attackScore(game, pawn); + + // Favorise les attaques de notre côté + if ((pawn.getPlayer().getGoal() - pawn.getRow()) / 2f < 4) { + score *= 2; + } + + score += getValidMoveCount(game, pawn) * 50; + + // Favorise les pushed + if (!pawn.isPusher()) { + score += 50; + } + + // Favorise les pièces qui n'ont pas encore bougées + if (pawn.getRow() == (pawn.getPlayer() == Player.RED ? 0 : 7)) { + score += 50; + } + + return score; + } + + private static int attackScore(MovingBoard game, MovingPawn pawn) { + int score = 0; + if (nextToOtherPlayer(game, pawn, -1)) { + score += 100; + } + if (nextToOtherPlayer(game, pawn, 1)) { + score += 100; + } + return score; + } + + private static boolean nextToOtherPlayer(MovingBoard game, MovingPawn pawn, int colOffset) { + int row = pawn.getRow() + pawn.getDirection(); + int col = pawn.getCol() + colOffset; + + if (col < 0 || col > 7) { + return false; + } + + MovingPawn dest = game.getBoard()[row][col]; + return dest != null && dest.getPlayer() != pawn.getPlayer(); + } + + private static int getValidMoveCount(MovingBoard game, MovingPawn pawn) { + int moveCount = 0; + + // Diagonale gauche + if (pawn.getCol() > 0) { + moveCount++; + } + + // Il n'y a pas de pièce adverse devant + if (!nextToOtherPlayer(game, pawn, 0)) { + moveCount++; + } + + // Diagonale droite + if (pawn.getCol() < 7) { + moveCount++; + } + + return moveCount; + } + static class Action { private final MovingPawn pawn; private final Pawn.PawnMovement movement;