From a62e3f77baa043fa33b3a89072a8d0b3be3137dc Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 12 Feb 2021 09:57:05 +0100 Subject: [PATCH] fixed bugs and crash --- src/othello/Main.java | 2 +- src/othello/Point.java | 4 +++- src/othello/State.java | 25 ++++++++++++++----------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/othello/Main.java b/src/othello/Main.java index 25f5d87..b3d2a9d 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -19,7 +19,7 @@ public class Main { AbstractPlayer player = game.getCurrentPlayer(); ArrayList> moves = game.getMove(player); System.out.println(game.toString()); - game.play(player.play(moves)); + game = game.play(player.play(moves)); } System.out.println("C'est " + game.getWinner() + " qui a gagné"); } diff --git a/src/othello/Point.java b/src/othello/Point.java index 4ccfd50..905fb57 100644 --- a/src/othello/Point.java +++ b/src/othello/Point.java @@ -13,16 +13,18 @@ public class Point { public boolean isJump(Point other) { return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2) == 4; } + public int getX(){ return x; } + public int getY(){ return y; } @Override public String toString () { - return "("+x+","+y+")"; + return "(" + x + ", " + y + ")"; } } diff --git a/src/othello/State.java b/src/othello/State.java index e855be2..9cda82d 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -42,15 +42,17 @@ public class State { // Recherche autour du pion du joueur courant for (int deltaY = -1; deltaY < 2; deltaY++) { for (int deltaX = -1; deltaX < 2; deltaX++) { - // La position du pion trouv� est exclue + // La position du pion trouvée est exclue if (deltaY != 0 && deltaX != 0) { - // Si une place libre est trouv�e elle est ajout�e à la liste des coups + // Si une place libre est trouvée elle est ajoutée à la liste des coups try { if (this.board[y+deltaY][x+deltaX] == null) { moves.add(new Pair(new Point(y, x), new Point(y+deltaY, x+deltaX))); } else { - if(this.board[y+2*deltaY][x+2*deltaX] == null) - moves.add(new Pair(new Point(y, x), new Point(y+2*deltaY, x+2*deltaX))); + Point current = new Point(y, x); + Point other = new Point(y + 2 * deltaY, x + 2 * deltaX); + if(this.board[y+2*deltaY][x+2*deltaX] == null && current.isJump(other)) + moves.add(new Pair(current, other)); } } catch(ArrayIndexOutOfBoundsException ignored) {} } @@ -79,8 +81,8 @@ public class State { State copy = this.copy(); copy.board[pair.getLeft().getX()][pair.getLeft().getY()] = copy.getCurrentPlayer(); int increment = 0; - for(int i = -1; i<2;i++){ - for(int z = -1;z<2;z++){ + for(int i = -1; i < 2; i++){ + for(int z = -1; z < 2; z++){ try { if(copy.board[pair.getLeft().getX() + i][pair.getLeft().getY() + z] != copy.getCurrentPlayer()){ increment++; @@ -89,11 +91,11 @@ public class State { } catch (IndexOutOfBoundsException ignored) {} } } - if (currentPlayer == player1) + if (copy.currentPlayer == player1) copy.n1 += increment; else copy.n2 += increment; - + copy.switchPlayer(); return copy; } @@ -106,12 +108,13 @@ public class State { } public State copy () { - State copy = new State(this.board, this.player1, this.player2,this.n1,this.n2); - for (int i=0; i