diff --git a/src/othello/Main.java b/src/othello/Main.java index cdd468c..1b14ff0 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -23,16 +23,26 @@ public class Main { System.out.println(game.toString()); game = game.play(player.play(game)); } - endGame(game); + endGame(game, p1, p2); } + /** + * The method will interpret the arguments and return a {@link Player} instance depending on the arguments given + * + * If arguments are missing or invalid, default value will be use {@code (4, 4, true)} + * @param args program arguments, given when user write the command to execute the program + * @return Player 1 and 2 instance + */ public static Player[] extractArgs(String[] args) { Player p1; Player p2; int depthP1 = 4; int depthP2 = 4; - boolean useAlphaBeta = false; + boolean useAlphaBeta = true; try { if(args.length >= 3) { // les paramètres > 3 sont ignorés depthP1 = Integer.parseInt(args[0]); @@ -63,12 +73,12 @@ public class Main { return board; } - public static void endGame(State game) { + public static void endGame(State game, Player p1, Player p2) { 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()); + System.out.println("Score joueur 1 -> " + game.getScore(p1)); + System.out.println("Score joueur 2 -> "+ game.getScore(p2)); } } diff --git a/src/othello/MainStats.java b/src/othello/MainStats.java index bfac4a7..f1663d8 100644 --- a/src/othello/MainStats.java +++ b/src/othello/MainStats.java @@ -35,7 +35,7 @@ public class MainStats { } writer.close(); writer2.close(); - Main.endGame(game); + Main.endGame(game, p1, p2); } } diff --git a/src/othello/Point.java b/src/othello/Point.java index d34b6a0..160eb59 100644 --- a/src/othello/Point.java +++ b/src/othello/Point.java @@ -12,6 +12,12 @@ public class Point { this.y = y; } + /** + * check if {@code other} point is considered as jump + * @param other arrival point + * @param board current {@link State} situation + * @return {@code true} if other is considered as a jump depending of {@code this}, {@code false} otherwise + */ public boolean isJump(Point other, Player[][] board) { 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; diff --git a/src/othello/State.java b/src/othello/State.java index 25e5a8c..523aebd 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -7,6 +7,11 @@ import java.util.List; public class State { + /** + * Contains previous situations of the {@link State#board}, if the game return in a situation which have already + * been played, the game ends. + * We only keep the 10 previous situations due to performances issues + */ public static List previousSituations = new LinkedList<>(); private final Player[][] board; @@ -26,9 +31,18 @@ public class State { } public boolean isOver() { - return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty()) || previousSituations.contains(this.board); + return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty()) + || previousSituations.contains(this.board); } + /** + * The method check every possible movement which can do {@code player}'s pawns and add the movement in a list when + * there is no-one on the {@link State#board board}. the left side of the {@link Pair tuple} contains the position + * where the pawn currently is and the right side the position where it can go by cloning itself or jumping over an + * other pawn + * @param player the player whose possible movements will be checked + * @return a {@link LinkedList list} containing every movement which can do {@code player} in this current situation + */ public LinkedList> getMove(Player player) { // Pair LinkedList> moves = new LinkedList<>(); @@ -78,6 +92,14 @@ public class State { return null; } + /** + * The method create a copy of itself and modify this copy depending on the {@code move} parameter, it'll clone or jump + * a pawn from the left side of {@code move} to the right side, switch current player and recalculate players' score + * @param move a {@link Pair tuple} containing 2 elements, + * the left side contains the starting point (where is the point) + * and the right side contains the point where it'll clone or jump + * @return a modified copy of the current situation + */ public State play(Pair move) { if(previousSituations.size() == 10) // on ne garde que les 10 dernieres situations par soucis de perfs previousSituations.remove(0); diff --git a/src/othello/players/AlphaBetaPlayer.java b/src/othello/players/AlphaBetaPlayer.java index d12a9b4..c9d780b 100644 --- a/src/othello/players/AlphaBetaPlayer.java +++ b/src/othello/players/AlphaBetaPlayer.java @@ -10,6 +10,13 @@ public class AlphaBetaPlayer extends Player{ super(depth); } + /** + * This method will find the best move to try to win the game by searching all possible movement given by + * {@link State#getMove(Player)} in the limit of {@link Player#depth} + * @see AlphaBetaPlayer#alphabeta(State, int, int, int) + * @see Player#play(State) + * @see NegamaxPlayer#play(State) + */ @Override public Pair play(State game) { int bestValue = Integer.MIN_VALUE; diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java index 82698ad..bad836c 100644 --- a/src/othello/players/NegamaxPlayer.java +++ b/src/othello/players/NegamaxPlayer.java @@ -10,6 +10,13 @@ public class NegamaxPlayer extends Player { super(depth); } + /** + * This method will find the best move to try to win the game by searching all possible movement given by + * {@link State#getMove(Player)} in the limit of {@link Player#depth} + * @see NegamaxPlayer#negamax(State, int) + * @see AlphaBetaPlayer#play(State) + * @see Player#play(State) + */ @Override public Pair play(State game) { int bestValue = Integer.MIN_VALUE; diff --git a/src/othello/players/Player.java b/src/othello/players/Player.java index 8d7fdaf..a02b131 100644 --- a/src/othello/players/Player.java +++ b/src/othello/players/Player.java @@ -18,6 +18,11 @@ public abstract class Player { return this.complexity; } + /** + * @param board current {@link State} situation + * @return a {@link Pair tuple} with on the left side the starting point of a pawn and on the right side the arrival + * point + */ public abstract Pair play(State board); protected int evaluate(State game){