This commit is contained in:
parent
39051a8fdd
commit
0b528baa8b
|
@ -4,9 +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="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$/.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>
|
||||||
<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" />
|
||||||
|
@ -141,7 +143,14 @@
|
||||||
<option name="project" value="LOCAL" />
|
<option name="project" value="LOCAL" />
|
||||||
<updated>1679348475071</updated>
|
<updated>1679348475071</updated>
|
||||||
</task>
|
</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 />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TypeScriptGeneratedFilesManager">
|
<component name="TypeScriptGeneratedFilesManager">
|
||||||
|
@ -150,6 +159,18 @@
|
||||||
<component name="VcsManagerConfiguration">
|
<component name="VcsManagerConfiguration">
|
||||||
<MESSAGE value="MiniMax" />
|
<MESSAGE value="MiniMax" />
|
||||||
<MESSAGE value="Movement logic" />
|
<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>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -65,7 +65,7 @@ public class Client {
|
||||||
return player == Player.RED;
|
return player == Player.RED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void play() throws IOException {
|
private void play() throws IOException, InterruptedException {
|
||||||
byte[] aBuffer = new byte[16];
|
byte[] aBuffer = new byte[16];
|
||||||
int size = input.available();
|
int size = input.available();
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
|
@ -76,6 +76,8 @@ public class Client {
|
||||||
board.move(previousMove);
|
board.move(previousMove);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Thread.sleep(1000);
|
||||||
|
|
||||||
String nextMove = board.runNextMove();
|
String nextMove = board.runNextMove();
|
||||||
System.out.println("Prochain mouvement: " + nextMove);
|
System.out.println("Prochain mouvement: " + nextMove);
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
package laboratoire4;
|
package laboratoire4;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class MiniMax {
|
public class MiniMax {
|
||||||
private static final int MAX_DEPTH = 4;
|
private static final int MAX_DEPTH = 2;
|
||||||
private static Random random = new Random();
|
private static Random random = new Random();
|
||||||
|
|
||||||
public static MiniMaxResult miniMax(PusherBoard board) {
|
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) {
|
private static MiniMaxResult miniMax(PusherBoard board, boolean max, int depth, int alphaBeta) {
|
||||||
|
@ -17,6 +18,7 @@ public class MiniMax {
|
||||||
List<Pawn> pawns = max ?
|
List<Pawn> pawns = max ?
|
||||||
board.getMaxPawns() :
|
board.getMaxPawns() :
|
||||||
board.getMinPawns();
|
board.getMinPawns();
|
||||||
|
// Collections.shuffle(pawns);
|
||||||
|
|
||||||
List<MiniMaxResult> results = new ArrayList<>();
|
List<MiniMaxResult> results = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -28,10 +30,12 @@ public class MiniMax {
|
||||||
if (pawn.isMoveValid(board, movement)) {
|
if (pawn.isMoveValid(board, movement)) {
|
||||||
pawn.move(movement);
|
pawn.move(movement);
|
||||||
|
|
||||||
int score = depth < MAX_DEPTH ?
|
int nextAlphaBeta = max ? Math.max(limScore, alphaBeta) : Math.min(limScore, alphaBeta);
|
||||||
miniMax(board, !max, depth + 1, limScore).getScore() :
|
|
||||||
evaluate(pawn, board);
|
int score = evaluate(pawn, board, max);
|
||||||
score *= pawn.getDirection();
|
if (depth < MAX_DEPTH) {
|
||||||
|
score = miniMax(board, !max, depth + 1, nextAlphaBeta).getScore();
|
||||||
|
}
|
||||||
|
|
||||||
if ((max && score > limScore) ||
|
if ((max && score > limScore) ||
|
||||||
(!max && score < limScore)) {
|
(!max && score < limScore)) {
|
||||||
|
@ -41,13 +45,13 @@ public class MiniMax {
|
||||||
MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
|
MiniMaxResult result = new MiniMaxResult(limScore, pawn, movement);
|
||||||
|
|
||||||
//elagage alphaBeta
|
//elagage alphaBeta
|
||||||
if ((max && limScore >= alphaBeta) ||
|
// if ((max && limScore > alphaBeta) ||
|
||||||
(!max && limScore <= alphaBeta)) {
|
// (!max && limScore < alphaBeta)) {
|
||||||
pawn.setRow(originalRow);
|
// pawn.setRow(originalRow);
|
||||||
pawn.setCol(originalCol);
|
// pawn.setCol(originalCol);
|
||||||
|
//
|
||||||
return result;
|
// return result;
|
||||||
}
|
// }
|
||||||
|
|
||||||
results.add(result);
|
results.add(result);
|
||||||
}
|
}
|
||||||
|
@ -62,29 +66,46 @@ public class MiniMax {
|
||||||
return results.get(index);
|
return results.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int evaluate(Pawn pawn, PusherBoard board) {
|
private static int evaluate(Pawn pawn, PusherBoard board, boolean max) {
|
||||||
int score = didWin(pawn);
|
int score = didWin(pawn, max) + didCapture(pawn, board, max) + isPushed(pawn);
|
||||||
score = Math.max(score, didCapture(pawn, board));
|
|
||||||
|
|
||||||
return score;
|
int homeRow = pawn.getPlayer() == Player.RED ? 0 : 7;
|
||||||
|
if (pawn.getRow() != homeRow) {
|
||||||
|
score += 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int didWin(Pawn pawn) {
|
int direction = max ? 1 : -1;
|
||||||
int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
|
return score * direction;
|
||||||
|
}
|
||||||
|
|
||||||
if (pawn.getRow() == goal) {
|
private static int isPushed(Pawn pawn) {
|
||||||
return Integer.MAX_VALUE;
|
if (pawn instanceof Pushed) {
|
||||||
|
return 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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()];
|
Pawn capturedPawn = board.getBoard()[pawn.getRow()][pawn.getCol()];
|
||||||
|
|
||||||
if (capturedPawn != null) {
|
if (capturedPawn != null) {
|
||||||
if (capturedPawn.getPlayer() != pawn.getPlayer()) {
|
if (capturedPawn.getPlayer() != pawn.getPlayer()) {
|
||||||
return 50;
|
if (max) {
|
||||||
|
return capturedPawn instanceof Pusher ? 100 : 50;
|
||||||
|
} else {
|
||||||
|
return capturedPawn instanceof Pusher ? 10000 : 5000;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,8 +53,11 @@ public class PusherBoard {
|
||||||
Pawn pawn = result.getPawn();
|
Pawn pawn = result.getPawn();
|
||||||
String initialPosition = pawn.getPosition();
|
String initialPosition = pawn.getPosition();
|
||||||
|
|
||||||
|
System.out.println(result.getScore());
|
||||||
|
|
||||||
move(pawn, result.getMovement());
|
move(pawn, result.getMovement());
|
||||||
|
|
||||||
|
|
||||||
return initialPosition + "-" + pawn.getPosition();
|
return initialPosition + "-" + pawn.getPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue