diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 937fb98..471e4ff 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,11 @@
-
+
-
+
+
+
@@ -141,7 +143,14 @@
1679348475071
-
+
+ 1679490100944
+
+
+
+ 1679490100944
+
+
@@ -150,6 +159,18 @@
-
+
+
+
+
+
+
+
+ file://$PROJECT_DIR$/src/main/java/laboratoire4/PusherBoard.java
+ 60
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/laboratoire4/Client.java b/src/main/java/laboratoire4/Client.java
index 8ee65da..2f486be 100644
--- a/src/main/java/laboratoire4/Client.java
+++ b/src/main/java/laboratoire4/Client.java
@@ -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);
diff --git a/src/main/java/laboratoire4/MiniMax.java b/src/main/java/laboratoire4/MiniMax.java
index 2dc1bad..f535e5e 100644
--- a/src/main/java/laboratoire4/MiniMax.java
+++ b/src/main/java/laboratoire4/MiniMax.java
@@ -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 pawns = max ?
board.getMaxPawns() :
board.getMinPawns();
+// Collections.shuffle(pawns);
List 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;
+ }
+
+ int direction = max ? 1 : -1;
+ return score * direction;
}
- private static int didWin(Pawn pawn) {
- int goal = pawn.getPlayer() == Player.RED ? 7 : 0;
-
- 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;
+ }
}
}
diff --git a/src/main/java/laboratoire4/PusherBoard.java b/src/main/java/laboratoire4/PusherBoard.java
index 33d3621..c2af373 100644
--- a/src/main/java/laboratoire4/PusherBoard.java
+++ b/src/main/java/laboratoire4/PusherBoard.java
@@ -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();
}