added arguments support + improved isOver + fixed State.play + refactored a bit Main and MainStats

This commit is contained in:
Quentin Legot 2021-02-26 11:54:26 +01:00
parent 3a92e315d0
commit 710298150e
6 changed files with 150 additions and 180 deletions

View File

@ -1,19 +1,20 @@
package othello;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import othello.players.AlphaBetaPlayer;
import othello.players.NegamaxPlayer;
import othello.players.Player;
public class Main {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
Player p1 = new AlphaBetaPlayer(4);
Player p2 = new AlphaBetaPlayer(4);
Player[][] board = initialize(p1, p2);
public static void main(String[] args) {
Player[] players = extractArgs(args);
Player p1 = players[0];
Player p2 = players[1];
Player[][] board = initializeBoard(p1, p2);
State game = new State(board, p1, p2);
System.out.println("joueur 1: " + p1);
System.out.println("joueur 2: " + p2);
@ -22,15 +23,38 @@ public class Main {
System.out.println(game.toString());
game = game.play(player.play(game));
}
System.out.println(game.toString());
System.out.println(game.getN1()+" "+ game.getN2());
System.out.println(game.getWinner() + " a gagné la partie");
System.out.println("Score joueur 1 -> " + game.getN1());
System.out.println("Score joueur 2 -> "+ game.getN2());
endGame(game);
}
public static Player[][] initialize(Player p1, Player p2){
public static Player[] extractArgs(String[] args) {
Player p1;
Player p2;
int depthP1 = 4;
int depthP2 = 4;
boolean useAlphaBeta = false;
try {
if(args.length >= 3) { // les paramètres > 3 sont ignorés
depthP1 = Integer.parseInt(args[0]);
depthP2 = Integer.parseInt(args[1]);
useAlphaBeta = Boolean.parseBoolean(args[2]);
}
} catch (NumberFormatException e) {
System.out.println("Les arguments de lancement ne pas corrects (pas des nombres entiers)");
System.out.println("Utilisation des paramètres par défaut ( 4 4 true )");
} finally {
if(useAlphaBeta) {
p1 = new AlphaBetaPlayer(depthP1);
p2 = new AlphaBetaPlayer(depthP2);
} else {
p1 = new NegamaxPlayer(depthP1);
p2 = new NegamaxPlayer(depthP2);
}
}
return new Player[]{p1, p2};
}
public static Player[][] initializeBoard(Player p1, Player p2){
Player[][] board = new Player[7][7];
board[0][0] = p2;
board[0][6] = p1;
@ -39,4 +63,12 @@ public class Main {
return board;
}
public static void endGame(State game) {
System.out.println(game.toString());
System.out.println(game.getN1()+" "+ game.getN2());
System.out.println(game.getWinner() + " a gagné la partie");
System.out.println("Score joueur 1 -> " + game.getN1());
System.out.println("Score joueur 2 -> "+ game.getN2());
}
}

View File

@ -1,8 +1,8 @@
package othello;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import othello.players.AlphaBetaPlayer;
import othello.players.Player;
@ -10,46 +10,32 @@ import othello.players.Player;
public class MainStats {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
Player p1 = new AlphaBetaPlayer(1);
Player p2 = new AlphaBetaPlayer(5);
Player[][] board = initialize(p1, p2);
public static void main(String[] args) throws IOException {
Player[] players = Main.extractArgs(args);
Player p1 = players[0];
Player p2 = players[1];
Player[][] board = Main.initializeBoard(p1, p2);
State game = new State(board, p1, p2);
System.out.println("joueur 1: " + p1);
System.out.println("joueur 2: " + p2);
int tour = 1; // Pour le rapport
PrintWriter writer = new PrintWriter("statsj1.txt", "UTF-8");
PrintWriter writer2 = new PrintWriter("statsj2.txt", "UTF-8");
PrintWriter writer = new PrintWriter("statsj1.txt", StandardCharsets.UTF_8);
PrintWriter writer2 = new PrintWriter("statsj2.txt", StandardCharsets.UTF_8);
while(!game.isOver()) {
Player player = game.getCurrentPlayer();
System.out.println(game.toString());
game = game.play(player.play(game));
if(tour%2 == 0) {
if(tour % 2 == 0)
writer2.println(player.getComplexity());
}
else {
else
writer.println(player.getComplexity());
}
tour++;
}
writer.close();
writer2.close();
System.out.println(game.toString());
System.out.println(game.getN1()+" "+ game.getN2());
System.out.println(game.getWinner() + " a gagné la partie");
System.out.println("Score joueur 1 -> " + game.getN1());
System.out.println("Score joueur 2 -> "+ game.getN2());
}
public static Player[][] initialize(Player p1, Player p2){
Player[][] board = new Player[7][7];
board[0][0] = p2;
board[0][6] = p1;
board[6][0] = p1;
board[6][6] = p2;
return board;
Main.endGame(game);
}
}

View File

@ -4,8 +4,8 @@ import othello.players.Player;
public class Point {
private int x;
private int y;
private final int x;
private final int y;
public Point(int y, int x) {
this.x = x;
@ -13,7 +13,8 @@ public class Point {
}
public boolean isJump(Point other, Player[][] board) {
return (board[(x+other.getX())/2][(y+other.getY())/2] != null);
double value = Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2);
return (value == 4 || value == 8) && board[(x+other.getX())/2][(y+other.getY())/2] != null;
}
public int getX(){

View File

@ -7,7 +7,7 @@ import java.util.List;
public class State {
public static List<State> previousSituations = new LinkedList<>();
public static List<Player[][]> previousSituations = new LinkedList<>();
private final Player[][] board;
private final Player player1;
@ -26,7 +26,7 @@ public class State {
}
public boolean isOver() {
return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty());
return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty()) || previousSituations.contains(this.board);
}
public LinkedList<Pair<Point, Point>> getMove(Player player) {
@ -61,9 +61,11 @@ public class State {
public int getScore(Player player) {
return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2));
}
public int getN1() {
return this.n1;
}
public int getN2() {
return this.n2;
}
@ -77,13 +79,15 @@ public class State {
}
public State play(Pair<Point,Point> move) {
if(previousSituations.size() == 10) // on ne garde que les 10 dernieres situations par soucis de perfs
previousSituations.remove(0);
previousSituations.add(board);
State copy = this.copy();
boolean isJump = move.getLeft().isJump(move.getRight(), copy.board);
copy.board[move.getRight().getY()][move.getRight().getX()] = copy.currentPlayer;
if (isJump) {
copy.board[move.getLeft().getY()][move.getLeft().getX()] = null;
copy.board[(move.getLeft().getY() + move.getRight().getY()) / 2][(move.getLeft().getX() + move.getRight().getX()) / 2] = copy.currentPlayer;
} else {
}
for (int i = -1; i < 2; i++) {
for (int z = -1; z < 2; z++) {
try {
@ -92,7 +96,7 @@ public class State {
} catch (IndexOutOfBoundsException ignored) {}
}
}
}
int ni = 0, nj = 0;
for (Player[] players : copy.board) {
@ -134,9 +138,6 @@ public class State {
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
}
/**
* TODO: display the current state of the board
*/
@Override
public String toString() {
StringBuilder str = new StringBuilder();
@ -154,6 +155,7 @@ public class State {
}
return str.toString();
}
@Override
public boolean equals(Object state) {
boolean bool;

View File

@ -1,67 +1,42 @@
6
16
29
44
66
90
41
55
74
92
111
142
168
195
221
249
287
327
362
393
424
461
494
527
563
607
638
671
706
134
157
194
215
234
263
298
352
382
401
415
430
452
480
517
561
605
647
688
717
737
766
797
827
756
769
790
820
838
854
886
915
940
969
992
1023
1053
1091
1125
1165
1196
1234
1274
1313
1348
1386
1424
1462
1501
1538
1579
1615
1654
1684
1710
1742
1767
1791
1810
1836
1856
1875
1895
1905
1914
1922
1928
864
878
891
904
914
927
934

View File

@ -1,67 +1,41 @@
6
16
28
37
46
56
65
75
86
107
121
133
149
172
194
206
234
254
272
295
319
338
357
378
409
435
465
495
60
74
91
103
129
150
166
201
226
245
257
269
307
355
402
438
466
497
511
523
551
577
605
630
531
537
553
576
595
624
640
654
681
664
673
679
690
701
709
735
757
785
800
825
836
849
867
875
891
905
921
936
951
961
983
1003
1020
1036
1047
1061
1075
1084
1098
1112
1120
1123
1128
1136
1139
1146
717
721
727