Fixes?
This commit is contained in:
parent
bd0fc7edfd
commit
02b49c9437
|
@ -4,21 +4,11 @@
|
|||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="AI">
|
||||
<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" />
|
||||
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Fixes?">
|
||||
<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/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/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" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/MovingBoard.java" afterDir="false" />
|
||||
</list>
|
||||
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" />
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
|
@ -143,7 +133,8 @@
|
|||
<workItem from="1679365557789" duration="21000" />
|
||||
<workItem from="1679424430928" duration="11764000" />
|
||||
<workItem from="1679593788411" duration="10753000" />
|
||||
<workItem from="1679672719638" duration="7700000" />
|
||||
<workItem from="1679672719638" duration="8340000" />
|
||||
<workItem from="1679779362814" duration="5861000" />
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="MiniMax">
|
||||
<created>1679263366439</created>
|
||||
|
@ -166,7 +157,14 @@
|
|||
<option name="project" value="LOCAL" />
|
||||
<updated>1679490100944</updated>
|
||||
</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 />
|
||||
</component>
|
||||
<component name="TypeScriptGeneratedFilesManager">
|
||||
|
@ -176,7 +174,8 @@
|
|||
<MESSAGE value="MiniMax" />
|
||||
<MESSAGE value="Movement logic" />
|
||||
<MESSAGE value="Small fixes" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Small fixes" />
|
||||
<MESSAGE value="Fixes?" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="Fixes?" />
|
||||
</component>
|
||||
<component name="XDebuggerManager">
|
||||
<breakpoint-manager>
|
||||
|
|
|
@ -76,7 +76,7 @@ public class Client {
|
|||
board.move(previousMove);
|
||||
}
|
||||
|
||||
Thread.sleep(500);
|
||||
// Thread.sleep(500);
|
||||
|
||||
String nextMove = board.runNextMove();
|
||||
System.out.println("Prochain mouvement: " + nextMove);
|
||||
|
|
|
@ -1,144 +1,143 @@
|
|||
package laboratoire4;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class MiniMax {
|
||||
private static final int MAX_DEPTH = 1;
|
||||
private static final int MAX_DEPTH = 4;
|
||||
|
||||
public static MiniMaxResult miniMax(PusherBoard board) {
|
||||
long startMillis = System.currentTimeMillis();
|
||||
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);
|
||||
|
||||
MiniMaxResult maxResult = null;
|
||||
|
||||
for (Action action : actions) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
private static MiniMaxResult miniMax(MovingBoard game, boolean max, int depth, int alpha, int beta) {
|
||||
depth += 1;
|
||||
long endMillis = System.currentTimeMillis();
|
||||
System.out.printf("%d ms\n", (endMillis - startMillis));
|
||||
|
||||
if (max) {
|
||||
return max(game, depth, alpha, beta);
|
||||
return maxResult;
|
||||
}
|
||||
|
||||
return min(game, depth, alpha, beta);
|
||||
private static int max(MovingBoard game, int depth, int alpha, int beta) {
|
||||
if (depth >= MAX_DEPTH) {
|
||||
return evaluate(game);
|
||||
}
|
||||
|
||||
private static MiniMaxResult max(MovingBoard game, int depth, int alpha, int beta) {
|
||||
int alphaT = Integer.MIN_VALUE;
|
||||
MovingPawn lastPawn = null;
|
||||
Pawn.PawnMovement lastMovement = null;
|
||||
int maxScore = Integer.MIN_VALUE;
|
||||
|
||||
for (MovingPawn pawn : game.getMaxPawns()) {
|
||||
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) {
|
||||
if (!pawn.isMoveValid(game.getBoard(), movement)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
lastPawn = pawn;
|
||||
lastMovement = movement;
|
||||
for (Action action : getActions(game, true)) {
|
||||
MovingPawn pawn = action.getPawn();
|
||||
Pawn.PawnMovement movement = action.getMovement();
|
||||
|
||||
game.move(pawn, movement);
|
||||
|
||||
int score = evaluate(pawn, game, depth);
|
||||
if (depth < MAX_DEPTH) {
|
||||
score = miniMax(game, false, depth, Math.max(alpha, alphaT), beta).getScore();
|
||||
}
|
||||
|
||||
int score = min(game, depth + 1, Math.max(alpha, maxScore), beta);
|
||||
game.revertMove();
|
||||
|
||||
if (score > alphaT) {
|
||||
alphaT = score;
|
||||
if (score > maxScore) {
|
||||
maxScore = score;
|
||||
}
|
||||
|
||||
if (alphaT >= beta) {
|
||||
return new MiniMaxResult(alphaT, pawn.getRow(), pawn.getCol(), movement);
|
||||
}
|
||||
if (maxScore >= beta) {
|
||||
return maxScore;
|
||||
}
|
||||
}
|
||||
|
||||
if (lastPawn == null) {
|
||||
return new MiniMaxResult(alphaT, 0, 0, null);
|
||||
}
|
||||
return new MiniMaxResult(alphaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement);
|
||||
return maxScore;
|
||||
}
|
||||
|
||||
private static MiniMaxResult min(MovingBoard game, int depth, int alpha, int beta) {
|
||||
int betaT = Integer.MAX_VALUE;
|
||||
MovingPawn lastPawn = null;
|
||||
Pawn.PawnMovement lastMovement = null;
|
||||
|
||||
for (MovingPawn pawn : game.getMinPawns()) {
|
||||
for (Pawn.PawnMovement movement : Pawn.PawnMovement.values()) {
|
||||
if (!pawn.isMoveValid(game.getBoard(), movement)) {
|
||||
continue;
|
||||
private static int min(MovingBoard game, int depth, int alpha, int beta) {
|
||||
if (depth >= MAX_DEPTH) {
|
||||
return evaluate(game);
|
||||
}
|
||||
|
||||
lastPawn = pawn;
|
||||
lastMovement = movement;
|
||||
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 = evaluate(pawn, game, depth);
|
||||
if (depth < MAX_DEPTH) {
|
||||
score = miniMax(game, true, depth, alpha, Math.min(betaT, beta)).getScore();
|
||||
}
|
||||
|
||||
int score = max(game, depth + 1, alpha, Math.min(beta, minScore)) * -1;
|
||||
game.revertMove();
|
||||
|
||||
if (score < betaT) {
|
||||
betaT = score;
|
||||
if (score < minScore) {
|
||||
minScore = score;
|
||||
}
|
||||
|
||||
if (betaT <= beta) {
|
||||
return new MiniMaxResult(betaT, pawn.getRow(), pawn.getCol(), movement);
|
||||
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) {
|
||||
return new MiniMaxResult(betaT, 0, 0, null);
|
||||
}
|
||||
return new MiniMaxResult(betaT, lastPawn.getRow(), lastPawn.getCol(), lastMovement);
|
||||
Collections.shuffle(actions);
|
||||
return actions;
|
||||
}
|
||||
|
||||
private static int evaluate(IPawn pawn, MovingBoard game, int depth) {
|
||||
int score = didWin(pawn) + didCapture(pawn, game);
|
||||
private static int evaluate(MovingBoard board) {
|
||||
return evaluate(board, board.getMaxPawns()) - evaluate(board, board.getMinPawns());
|
||||
}
|
||||
|
||||
score += pawn.isPusher() ? 0 : 5;
|
||||
private static int evaluate(MovingBoard board, Collection<MovingPawn> pawns) {
|
||||
int score = pawns.size() * 5;
|
||||
|
||||
for (MovingPawn pawn : pawns) {
|
||||
int row = pawn.getRow();
|
||||
int col = pawn.getCol();
|
||||
int nextNextRow = row + pawn.getDirection() * 2;
|
||||
int goal = pawn.getPlayer().getGoal();
|
||||
|
||||
// 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;
|
||||
if (row == goal) {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
IPawn nearPawn = game.getBoard()[nextNextRow][nextCol];
|
||||
if (nearPawn != null && !nearPawn.isPusher() && nearPawn.getPlayer() == pawn.getPlayer()) {
|
||||
score += 5;
|
||||
}
|
||||
}
|
||||
score += Math.abs(goal - row);
|
||||
}
|
||||
|
||||
return score * pawn.getDirection();
|
||||
return score;
|
||||
}
|
||||
|
||||
private static int didWin(IPawn pawn) {
|
||||
if (pawn.getRow() == pawn.getPlayer().getGoal()) {
|
||||
return 100;
|
||||
static class Action {
|
||||
private final MovingPawn pawn;
|
||||
private final Pawn.PawnMovement movement;
|
||||
|
||||
public Action(MovingPawn pawn, Pawn.PawnMovement movement) {
|
||||
this.pawn = pawn;
|
||||
this.movement = movement;
|
||||
}
|
||||
|
||||
return 0;
|
||||
public MovingPawn getPawn() {
|
||||
return pawn;
|
||||
}
|
||||
|
||||
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;
|
||||
public Pawn.PawnMovement getMovement() {
|
||||
return movement;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static class MiniMaxResult {
|
||||
|
|
|
@ -49,6 +49,10 @@ public class MovingBoard {
|
|||
return player;
|
||||
}
|
||||
|
||||
public boolean hasCaptured() {
|
||||
return removedPawns.peek() != null;
|
||||
}
|
||||
|
||||
public Collection<MovingPawn> getMaxPawns() {
|
||||
return getPawns(p -> p.getPlayer() == player);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue