This commit is contained in:
william 2023-03-22 10:15:27 -04:00
parent 39051a8fdd
commit 0b528baa8b
4 changed files with 76 additions and 29 deletions

View File

@ -4,9 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Movement logic">
<list default="true" id="41395b4b-3252-492c-a869-5f4dab107186" name="Changes" comment="Small 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/Pushed.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/Pushed.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/PusherBoard.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java" afterDir="false" />
</list>
<list id="98b8a79f-2f53-42bf-96da-7af322697a0d" name="Changes by acastonguay" comment="" />
<option name="SHOW_DIALOG" value="false" />
@ -141,7 +143,14 @@
<option name="project" value="LOCAL" />
<updated>1679348475071</updated>
</task>
<option name="localTasksCounter" value="3" />
<task id="LOCAL-00003" summary="Small fixes">
<created>1679490100944</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1679490100944</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -150,6 +159,18 @@
<component name="VcsManagerConfiguration">
<MESSAGE value="MiniMax" />
<MESSAGE value="Movement logic" />
<option name="LAST_COMMIT_MESSAGE" value="Movement logic" />
<MESSAGE value="Small fixes" />
<option name="LAST_COMMIT_MESSAGE" value="Small fixes" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java</url>
<line>60</line>
<option name="timeStamp" value="7" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project>

View File

@ -65,7 +65,7 @@ public class Client {
return player == Player.RED;
}
private void play() throws IOException {
private void play() throws IOException, InterruptedException {
byte[] aBuffer = new byte[16];
int size = input.available();
if (size > 0) {
@ -76,6 +76,8 @@ public class Client {
board.move(previousMove);
}
// Thread.sleep(1000);
String nextMove = board.runNextMove();
System.out.println("Prochain mouvement: " + nextMove);

View File

@ -1,15 +1,16 @@
package laboratoire4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class MiniMax {
private static final int MAX_DEPTH = 4;
private static final int MAX_DEPTH = 2;
private static Random random = new Random();
public static MiniMaxResult miniMax(PusherBoard board) {
return miniMax(board, true, 0, Integer.MIN_VALUE);
return miniMax(board, true, 0, 0);
}
private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) {
@ -17,6 +18,7 @@ public class MiniMax {
List<Pawn> pawns = max ?
board.getMaxPawns() :
board.getMinPawns();
// Collections.shuffle(pawns);
List<MiniMaxResult> results = new ArrayList<>();
@ -28,10 +30,12 @@ public class MiniMax {
if (pawn.isMoveValid(board, movement)) {
pawn.move(movement);
int score = depth < MAX_DEPTH ?
miniMax(board, !max, depth + 1, limScore).getScore() :
evaluate(pawn, board);
score *= pawn.getDirection();
int nextAlphaBeta = max ? Math.max(limScore, alphaBeta) : Math.min(limScore, alphaBeta);
int score = evaluate(pawn, board, max);
if (depth < MAX_DEPTH) {
score = miniMax(board, !max, depth + 1, nextAlphaBeta).getScore();
}
if ((max && score > limScore) ||
(!max && score < limScore)) {
@ -41,13 +45,13 @@ public class MiniMax {
MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
//elagage alphaBeta
if ((max && limScore >= alphaBeta) ||
(!max && limScore <= alphaBeta)) {
pawn.setRow(originalRow);
pawn.setCol(originalCol);
return result;
}
// if ((max && limScore > alphaBeta) ||
// (!max && limScore < alphaBeta)) {
// pawn.setRow(originalRow);
// pawn.setCol(originalCol);
//
// return result;
// }
results.add(result);
}
@ -62,29 +66,46 @@ public class MiniMax {
return results.get(index);
}
private static int evaluate(Pawn pawn, PusherBoard board) {
int score = didWin(pawn);
score = Math.max(score, didCapture(pawn, board));
private static int evaluate(Pawn pawn, PusherBoard board, boolean max) {
int score = didWin(pawn, max) + didCapture(pawn, board, max) + isPushed(pawn);
return score;
int homeRow = pawn.getPlayer() == Player.RED ? 0 : 7;
if (pawn.getRow() != homeRow) {
score += 50;
}
private static int didWin(Pawn pawn) {
int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
int direction = max ? 1 : -1;
return score * direction;
}
if (pawn.getRow() == goal) {
return Integer.MAX_VALUE;
private static int isPushed(Pawn pawn) {
if (pawn instanceof Pushed) {
return 5;
}
return 0;
}
private static int didCapture(Pawn pawn, PusherBoard board) {
private static int didWin(Pawn pawn, boolean max) {
int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
if (pawn.getRow() == goal) {
return max ? 1000 : 100000;
}
return 0;
}
private static int didCapture(Pawn pawn, PusherBoard board, boolean max) {
Pawn capturedPawn = board.getBoard()[pawn.getRow()][pawn.getCol()];
if (capturedPawn != null) {
if (capturedPawn.getPlayer() != pawn.getPlayer()) {
return 50;
if (max) {
return capturedPawn instanceof Pusher ? 100 : 50;
} else {
return capturedPawn instanceof Pusher ? 10000 : 5000;
}
}
}

View File

@ -53,8 +53,11 @@ public class PusherBoard {
Pawn pawn = result.getPawn();
String initialPosition = pawn.getPosition();
System.out.println(result.getScore());
move(pawn, result.getMovement());
return initialPosition + "-" + pawn.getPosition();
}