This commit is contained in:
william 2023-03-25 19:28:12 -04:00
parent bd0fc7edfd
commit 02b49c9437
4 changed files with 120 additions and 118 deletions

View File

@ -4,21 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="AI"> <list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Fixes?">
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/IPawn.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingBoard.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingPawn.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingPushed.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingPusher.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Client.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MiniMax.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Pawn.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pawn.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingBoard.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Player.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Player.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/Pusher.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pusher.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" afterDir="false" />
</list> </list>
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" /> <list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -143,7 +133,8 @@
<workItem from="1679365557789" duration="21000" /> <workItem from="1679365557789" duration="21000" />
<workItem from="1679424430928" duration="11764000" /> <workItem from="1679424430928" duration="11764000" />
<workItem from="1679593788411" duration="10753000" /> <workItem from="1679593788411" duration="10753000" />
<workItem from="1679672719638" duration="7700000" /> <workItem from="1679672719638" duration="8340000" />
<workItem from="1679779362814" duration="5861000" />
</task> </task>
<task id="LOCAL-00001" summary="MiniMax"> <task id="LOCAL-00001" summary="MiniMax">
<created>1679263366439</created> <created>1679263366439</created>
@ -166,7 +157,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1679490100944</updated> <updated>1679490100944</updated>
</task> </task>
<option name="localTasksCounter" value="4" /> <task id="LOCAL-00004" summary="Fixes?">
<created>1679682974611</created>
<option name="number" value="00004" />
<option name="presentableId" value="LOCAL-00004" />
<option name="project" value="LOCAL" />
<updated>1679682974611</updated>
</task>
<option name="localTasksCounter" value="5" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@ -176,7 +174,8 @@
<MESSAGE value="MiniMax" /> <MESSAGE value="MiniMax" />
<MESSAGE value="Movement logic" /> <MESSAGE value="Movement logic" />
<MESSAGE value="Small fixes" /> <MESSAGE value="Small fixes" />
<option name="LAST_COMMIT_MESSAGE" value="Small fixes" /> <MESSAGE value="Fixes?" />
<option name="LAST_COMMIT_MESSAGE" value="Fixes?" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>

View File

@ -76,7 +76,7 @@ public class Client {
board.move(previousMove); board.move(previousMove);
} }
Thread.sleep(500); // Thread.sleep(500);
String nextMove = board.runNextMove(); String nextMove = board.runNextMove();
System.out.println("Prochain mouvement: " + nextMove); System.out.println("Prochain mouvement: " + nextMove);

View File

@ -1,144 +1,143 @@
package laboratoire4; package laboratoire4;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class MiniMax { public class MiniMax {
private static final int MAX_DEPTH = 1; private static final int MAX_DEPTH = 4;
public static MiniMaxResult miniMax(PusherBoard board) { public static MiniMaxResult miniMax(PusherBoard board) {
long startMillis = System.currentTimeMillis();
MovingBoard game = new MovingBoard(board.getBoard(), board.getPlayer()); MovingBoard game = new MovingBoard(board.getBoard(), board.getPlayer());
return miniMax(game, true, 0, Integer.MIN_VALUE, Integer.MAX_VALUE); Collection<Action> actions = getActions(game, true);
}
private static MiniMaxResult miniMax(MovingBoard game, boolean max, int depth, int alpha, int beta) { MiniMaxResult maxResult = null;
depth += 1;
if (max) { for (Action action : actions) {
return max(game, depth, alpha, beta); int score = min(game, 0, Integer.MIN_VALUE, Integer.MAX_VALUE);
if (maxResult == null || score > maxResult.getScore()) {
MovingPawn pawn = action.getPawn();
maxResult = new MiniMaxResult(score, pawn.getRow(), pawn.getCol(), action.getMovement());
}
} }
return min(game, depth, alpha, beta); long endMillis = System.currentTimeMillis();
System.out.printf("%d ms\n", (endMillis - startMillis));
return maxResult;
} }
private static MiniMaxResult max(MovingBoard game, int depth, int alpha, int beta) { private static int max(MovingBoard game, int depth, int alpha, int beta) {
int alphaT = Integer.MIN_VALUE; if (depth >= MAX_DEPTH) {
MovingPawn lastPawn = null; return evaluate(game);
Pawn.PawnMovement lastMovement = null; }
for (MovingPawn pawn : game.getMaxPawns()) { int maxScore = Integer.MIN_VALUE;
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) {
if (!pawn.isMoveValid(game.getBoard(), movement)) {
continue;
}
lastPawn = pawn; for (Action action : getActions(game, true)) {
lastMovement = movement; MovingPawn pawn = action.getPawn();
Pawn.PawnMovement movement = action.getMovement();
game.move(pawn, movement); game.move(pawn, movement);
int score = min(game, depth + 1, Math.max(alpha, maxScore), beta);
game.revertMove();
int score = evaluate(pawn, game, depth); if (score > maxScore) {
if (depth < MAX_DEPTH) { maxScore = score;
score = miniMax(game, false, depth, Math.max(alpha, alphaT), beta).getScore(); }
}
game.revertMove(); if (maxScore >= beta) {
return maxScore;
}
}
if (score > alphaT) { return maxScore;
alphaT = score; }
}
if (alphaT >= beta) { private static int min(MovingBoard game, int depth, int alpha, int beta) {
return new MiniMaxResult(alphaT, pawn.getRow(), pawn.getCol(), movement); if (depth >= MAX_DEPTH) {
return evaluate(game);
}
int minScore = Integer.MAX_VALUE;
for (Action action : getActions(game, false)) {
MovingPawn pawn = action.getPawn();
Pawn.PawnMovement movement = action.getMovement();
game.move(pawn, movement);
int score = max(game, depth + 1, alpha, Math.min(beta, minScore)) * -1;
game.revertMove();
if (score < minScore) {
minScore = score;
}
if (minScore <= alpha) {
return minScore;
}
}
return minScore;
}
private static Collection<Action> getActions(MovingBoard game, boolean max) {
List<Action> actions = new ArrayList<>();
Collection<MovingPawn> pawns = max ? game.getMaxPawns() : game.getMinPawns();
Pawn.PawnMovement[] movements = Pawn.PawnMovement.values();
for (MovingPawn pawn : pawns) {
for (Pawn.PawnMovement movement : movements) {
if (pawn.isMoveValid(game.getBoard(), movement)) {
actions.add(new Action(pawn, movement));
} }
} }
} }
if (lastPawn == null) { Collections.shuffle(actions);
return new MiniMaxResult(alphaT, 0, 0, null); return actions;
}
return new MiniMaxResult(alphaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement);
} }
private static MiniMaxResult min(MovingBoard game, int depth, int alpha, int beta) { private static int evaluate(MovingBoard board) {
int betaT = Integer.MAX_VALUE; return evaluate(board, board.getMaxPawns()) - evaluate(board, board.getMinPawns());
MovingPawn lastPawn = null; }
Pawn.PawnMovement lastMovement = null;
for (MovingPawn pawn : game.getMinPawns()) { private static int evaluate(MovingBoard board, Collection<MovingPawn> pawns) {
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) { int score = pawns.size() * 5;
if (!pawn.isMoveValid(game.getBoard(), movement)) {
continue;
}
lastPawn = pawn; for (MovingPawn pawn : pawns) {
lastMovement = movement; int row = pawn.getRow();
int goal = pawn.getPlayer().getGoal();
game.move(pawn, movement); if (row == goal) {
return Integer.MAX_VALUE;
int score = evaluate(pawn, game, depth);
if (depth < MAX_DEPTH) {
score = miniMax(game, true, depth, alpha, Math.min(betaT, beta)).getScore();
}
game.revertMove();
if (score < betaT) {
betaT = score;
}
if (betaT <= beta) {
return new MiniMaxResult(betaT, pawn.getRow(), pawn.getCol(), movement);
}
} }
score += Math.abs(goal - row);
} }
if (lastPawn == null) { return score;
return new MiniMaxResult(betaT, 0, 0, null);
}
return new MiniMaxResult(betaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement);
} }
private static int evaluate(IPawn pawn, MovingBoard game, int depth) { static class Action {
int score = didWin(pawn) + didCapture(pawn, game); private final MovingPawn pawn;
private final Pawn.PawnMovement movement;
score += pawn.isPusher() ? 0 : 5; public Action(MovingPawn pawn, Pawn.PawnMovement movement) {
this.pawn = pawn;
int row = pawn.getRow(); this.movement = movement;
int col = pawn.getCol();
int nextNextRow = row + pawn.getDirection() * 2;
// Attire nos pusher vers nos pushed
if (pawn.isPusher() && nextNextRow >= 0 && nextNextRow <= 7) {
for (Pawn.PawnMovement move : Pawn.PawnMovement.values()) {
int nextCol = col + move.getMove();
if (nextCol < 0 || nextCol > 7) {
continue;
}
IPawn nearPawn = game.getBoard()[nextNextRow][nextCol];
if (nearPawn != null && !nearPawn.isPusher() && nearPawn.getPlayer() == pawn.getPlayer()) {
score += 5;
}
}
} }
return score * pawn.getDirection(); public MovingPawn getPawn() {
} return pawn;
private static int didWin(IPawn pawn) {
if (pawn.getRow() == pawn.getPlayer().getGoal()) {
return 100;
} }
return 0; public Pawn.PawnMovement getMovement() {
} return movement;
private static int didCapture(IPawn pawn, MovingBoard game) {
IPawn capturedPawn = game.getBoard()[pawn.getRow()][pawn.getCol()];
if (capturedPawn != null && capturedPawn.getPlayer() != game.getPlayer()) {
return 500;
} }
return 0;
} }
static class MiniMaxResult { static class MiniMaxResult {

View File

@ -49,6 +49,10 @@ public class MovingBoard {
return player; return player;
} }
public boolean hasCaptured() {
return removedPawns.peek() != null;
}
public Collection<MovingPawn> getMaxPawns() { public Collection<MovingPawn> getMaxPawns() {
return getPawns(p -> p.getPlayer() == player); return getPawns(p -> p.getPlayer() == player);
} }