added arguments support + improved isOver + fixed State.play + refactored a bit Main and MainStats
This commit is contained in:
parent
3a92e315d0
commit
710298150e
@ -1,19 +1,20 @@
|
|||||||
package othello;
|
package othello;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import othello.players.AlphaBetaPlayer;
|
import othello.players.AlphaBetaPlayer;
|
||||||
|
import othello.players.NegamaxPlayer;
|
||||||
import othello.players.Player;
|
import othello.players.Player;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
|
public static void main(String[] args) {
|
||||||
Player p1 = new AlphaBetaPlayer(4);
|
Player[] players = extractArgs(args);
|
||||||
Player p2 = new AlphaBetaPlayer(4);
|
Player p1 = players[0];
|
||||||
Player[][] board = initialize(p1, p2);
|
Player p2 = players[1];
|
||||||
|
Player[][] board = initializeBoard(p1, p2);
|
||||||
State game = new State(board, p1, p2);
|
State game = new State(board, p1, p2);
|
||||||
System.out.println("joueur 1: " + p1);
|
System.out.println("joueur 1: " + p1);
|
||||||
System.out.println("joueur 2: " + p2);
|
System.out.println("joueur 2: " + p2);
|
||||||
@ -22,15 +23,38 @@ public class Main {
|
|||||||
System.out.println(game.toString());
|
System.out.println(game.toString());
|
||||||
game = game.play(player.play(game));
|
game = game.play(player.play(game));
|
||||||
}
|
}
|
||||||
System.out.println(game.toString());
|
endGame(game);
|
||||||
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){
|
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];
|
Player[][] board = new Player[7][7];
|
||||||
board[0][0] = p2;
|
board[0][0] = p2;
|
||||||
board[0][6] = p1;
|
board[0][6] = p1;
|
||||||
@ -39,4 +63,12 @@ public class Main {
|
|||||||
return board;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package othello;
|
package othello;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import othello.players.AlphaBetaPlayer;
|
import othello.players.AlphaBetaPlayer;
|
||||||
import othello.players.Player;
|
import othello.players.Player;
|
||||||
@ -10,46 +10,32 @@ import othello.players.Player;
|
|||||||
public class MainStats {
|
public class MainStats {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
|
public static void main(String[] args) throws IOException {
|
||||||
Player p1 = new AlphaBetaPlayer(1);
|
Player[] players = Main.extractArgs(args);
|
||||||
Player p2 = new AlphaBetaPlayer(5);
|
Player p1 = players[0];
|
||||||
Player[][] board = initialize(p1, p2);
|
Player p2 = players[1];
|
||||||
|
Player[][] board = Main.initializeBoard(p1, p2);
|
||||||
State game = new State(board, p1, p2);
|
State game = new State(board, p1, p2);
|
||||||
System.out.println("joueur 1: " + p1);
|
System.out.println("joueur 1: " + p1);
|
||||||
System.out.println("joueur 2: " + p2);
|
System.out.println("joueur 2: " + p2);
|
||||||
int tour = 1; // Pour le rapport
|
int tour = 1; // Pour le rapport
|
||||||
PrintWriter writer = new PrintWriter("statsj1.txt", "UTF-8");
|
PrintWriter writer = new PrintWriter("statsj1.txt", StandardCharsets.UTF_8);
|
||||||
PrintWriter writer2 = new PrintWriter("statsj2.txt", "UTF-8");
|
PrintWriter writer2 = new PrintWriter("statsj2.txt", StandardCharsets.UTF_8);
|
||||||
|
|
||||||
while(!game.isOver()) {
|
while(!game.isOver()) {
|
||||||
Player player = game.getCurrentPlayer();
|
Player player = game.getCurrentPlayer();
|
||||||
System.out.println(game.toString());
|
System.out.println(game.toString());
|
||||||
game = game.play(player.play(game));
|
game = game.play(player.play(game));
|
||||||
if(tour%2 == 0) {
|
|
||||||
|
if(tour % 2 == 0)
|
||||||
writer2.println(player.getComplexity());
|
writer2.println(player.getComplexity());
|
||||||
}
|
else
|
||||||
else {
|
|
||||||
writer.println(player.getComplexity());
|
writer.println(player.getComplexity());
|
||||||
}
|
|
||||||
tour++;
|
tour++;
|
||||||
}
|
}
|
||||||
writer.close();
|
writer.close();
|
||||||
writer2.close();
|
writer2.close();
|
||||||
System.out.println(game.toString());
|
Main.endGame(game);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,8 +4,8 @@ import othello.players.Player;
|
|||||||
|
|
||||||
public class Point {
|
public class Point {
|
||||||
|
|
||||||
private int x;
|
private final int x;
|
||||||
private int y;
|
private final int y;
|
||||||
|
|
||||||
public Point(int y, int x) {
|
public Point(int y, int x) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@ -13,7 +13,8 @@ public class Point {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isJump(Point other, Player[][] board) {
|
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(){
|
public int getX(){
|
||||||
|
@ -7,7 +7,7 @@ import java.util.List;
|
|||||||
|
|
||||||
public class State {
|
public class State {
|
||||||
|
|
||||||
public static List<State> previousSituations = new LinkedList<>();
|
public static List<Player[][]> previousSituations = new LinkedList<>();
|
||||||
|
|
||||||
private final Player[][] board;
|
private final Player[][] board;
|
||||||
private final Player player1;
|
private final Player player1;
|
||||||
@ -26,7 +26,7 @@ public class State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOver() {
|
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) {
|
public LinkedList<Pair<Point, Point>> getMove(Player player) {
|
||||||
@ -61,9 +61,11 @@ public class State {
|
|||||||
public int getScore(Player player) {
|
public int getScore(Player player) {
|
||||||
return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2));
|
return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getN1() {
|
public int getN1() {
|
||||||
return this.n1;
|
return this.n1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getN2() {
|
public int getN2() {
|
||||||
return this.n2;
|
return this.n2;
|
||||||
}
|
}
|
||||||
@ -77,13 +79,15 @@ public class State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public State play(Pair<Point,Point> move) {
|
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();
|
State copy = this.copy();
|
||||||
boolean isJump = move.getLeft().isJump(move.getRight(), copy.board);
|
boolean isJump = move.getLeft().isJump(move.getRight(), copy.board);
|
||||||
copy.board[move.getRight().getY()][move.getRight().getX()] = copy.currentPlayer;
|
copy.board[move.getRight().getY()][move.getRight().getX()] = copy.currentPlayer;
|
||||||
if (isJump) {
|
if (isJump) {
|
||||||
copy.board[move.getLeft().getY()][move.getLeft().getX()] = null;
|
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 i = -1; i < 2; i++) {
|
||||||
for (int z = -1; z < 2; z++) {
|
for (int z = -1; z < 2; z++) {
|
||||||
try {
|
try {
|
||||||
@ -92,7 +96,7 @@ public class State {
|
|||||||
} catch (IndexOutOfBoundsException ignored) {}
|
} catch (IndexOutOfBoundsException ignored) {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int ni = 0, nj = 0;
|
int ni = 0, nj = 0;
|
||||||
for (Player[] players : copy.board) {
|
for (Player[] players : copy.board) {
|
||||||
@ -134,9 +138,6 @@ public class State {
|
|||||||
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
|
setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: display the current state of the board
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
@ -154,6 +155,7 @@ public class State {
|
|||||||
}
|
}
|
||||||
return str.toString();
|
return str.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object state) {
|
public boolean equals(Object state) {
|
||||||
boolean bool;
|
boolean bool;
|
||||||
|
97
statsj1.txt
97
statsj1.txt
@ -1,67 +1,42 @@
|
|||||||
6
|
6
|
||||||
16
|
16
|
||||||
29
|
29
|
||||||
44
|
41
|
||||||
66
|
55
|
||||||
90
|
74
|
||||||
|
92
|
||||||
111
|
111
|
||||||
142
|
134
|
||||||
168
|
157
|
||||||
195
|
194
|
||||||
221
|
215
|
||||||
249
|
234
|
||||||
287
|
263
|
||||||
327
|
298
|
||||||
362
|
352
|
||||||
393
|
382
|
||||||
424
|
401
|
||||||
461
|
415
|
||||||
494
|
430
|
||||||
527
|
452
|
||||||
563
|
480
|
||||||
607
|
517
|
||||||
638
|
561
|
||||||
671
|
605
|
||||||
706
|
647
|
||||||
|
688
|
||||||
|
717
|
||||||
737
|
737
|
||||||
766
|
756
|
||||||
797
|
769
|
||||||
827
|
790
|
||||||
|
820
|
||||||
|
838
|
||||||
854
|
854
|
||||||
886
|
864
|
||||||
915
|
878
|
||||||
940
|
891
|
||||||
969
|
904
|
||||||
992
|
914
|
||||||
1023
|
927
|
||||||
1053
|
934
|
||||||
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
|
|
||||||
|
94
statsj2.txt
94
statsj2.txt
@ -1,67 +1,41 @@
|
|||||||
6
|
6
|
||||||
16
|
16
|
||||||
28
|
28
|
||||||
37
|
|
||||||
46
|
46
|
||||||
56
|
60
|
||||||
65
|
74
|
||||||
75
|
91
|
||||||
86
|
103
|
||||||
107
|
129
|
||||||
121
|
150
|
||||||
133
|
166
|
||||||
149
|
201
|
||||||
172
|
226
|
||||||
194
|
245
|
||||||
206
|
257
|
||||||
234
|
269
|
||||||
254
|
307
|
||||||
272
|
355
|
||||||
295
|
402
|
||||||
319
|
438
|
||||||
338
|
466
|
||||||
357
|
497
|
||||||
378
|
511
|
||||||
409
|
|
||||||
435
|
|
||||||
465
|
|
||||||
495
|
|
||||||
523
|
523
|
||||||
551
|
531
|
||||||
577
|
537
|
||||||
605
|
553
|
||||||
630
|
576
|
||||||
|
595
|
||||||
|
624
|
||||||
|
640
|
||||||
654
|
654
|
||||||
681
|
664
|
||||||
|
673
|
||||||
|
679
|
||||||
|
690
|
||||||
|
701
|
||||||
709
|
709
|
||||||
735
|
717
|
||||||
757
|
721
|
||||||
785
|
727
|
||||||
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
|
|
||||||
|
Loading…
Reference in New Issue
Block a user