From d4437f8b87253c84c25c52780ee42daa53eb00df Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 10 Feb 2021 12:01:51 +0100 Subject: [PATCH] added generic Pair and Point + fixed getMove --- src/othello/Main.java | 9 ++--- src/othello/Pair.java | 51 ++++++++++++++++++++++++ src/othello/Point.java | 17 ++++++++ src/othello/State.java | 56 ++++++++++++--------------- src/othello/players/RandomPlayer.java | 5 +++ 5 files changed, 102 insertions(+), 36 deletions(-) create mode 100644 src/othello/Pair.java create mode 100644 src/othello/Point.java create mode 100644 src/othello/players/RandomPlayer.java diff --git a/src/othello/Main.java b/src/othello/Main.java index e98c534..7257b85 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -1,6 +1,5 @@ package othello; -import java.awt.Point; import java.util.ArrayList; public class Main { @@ -10,11 +9,11 @@ public class Main { int p1 = 1, p2 = 2; int[][] board = initialize(p1, p2); State game = new State(board, p1, p2); - //while(!game.isOver()) { + while(!game.isOver()) { int player = game.getCurrentPlayer(); - - //} - ArrayList a = new ArrayList(); + ArrayList> moves = game.getMove(player); + } + ArrayList a = new ArrayList<>(); a.add(new Point(3,3)); System.out.println(game.getMove(p1).toString()); //System.out.println(a.get(0).toString()); diff --git a/src/othello/Pair.java b/src/othello/Pair.java new file mode 100644 index 0000000..b748afd --- /dev/null +++ b/src/othello/Pair.java @@ -0,0 +1,51 @@ +package othello; + +/** + * tuple containing 2 unknown type elements + * + * @param left + * @param right + */ +public class Pair { + + private final U left; + private final K right; + + public Pair(U left, K right) { + this.left = left; + this.right = right; + } + + public U getLeft() { + return left; + } + + public K getRight() { + return right; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final Pair other = (Pair) obj; + return this.left.equals(other.getLeft()) && this.left.equals(other.getRight()); + } + + @Override + public int hashCode() { + return 31 + left.hashCode() * right.hashCode(); + } + + @Override + public String toString() { + return "(" + left + ", " + right + ")"; + } +} diff --git a/src/othello/Point.java b/src/othello/Point.java new file mode 100644 index 0000000..4e0b11a --- /dev/null +++ b/src/othello/Point.java @@ -0,0 +1,17 @@ +package othello; + +public class Point { + + public int x; + public int y; + + public Point(int x, int y) { + this.x = x; + this.y = y; + } + + public boolean isJump(Point other) { + return Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2) == 4; + } + +} diff --git a/src/othello/State.java b/src/othello/State.java index 5e84cda..a15f3c2 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -1,6 +1,5 @@ package othello; -import java.awt.Point; import java.util.ArrayList; public class State { @@ -26,45 +25,40 @@ public class State { } public boolean isOver() { - - return false; + if(n1 == 0 || n2 == 0) + return true; + return getMove(player1).isEmpty() && getMove(player2).isEmpty(); } - public ArrayList getMove(int player) { - ArrayList moves = new ArrayList(); - - // Clonage + public ArrayList> getMove(int player) { + // Pair + ArrayList> moves = new ArrayList<>(); // Parcours du plateau de jeu - for (int i=0; i= 0) && (i+k < 7 )) && ((j+l >= 0) && (j+l < 7 )) && (this.board[i+k][j+l]==0)) { - moves.add(new Point(i+k, j+l)); - } + for (int deltaY = -2; deltaY < 3; deltaY++) { + for (int deltaX = -2; deltaX < 3; deltaX++) { + // La position du pion trouv� est exclue + if (deltaY != 0 && deltaX != 0) { + // Si une place libre est trouv�e elle est ajout�e à la liste des coups + try { + if ((this.board[y+deltaY][x+deltaX]==0)) { + moves.add(new Pair(new Point(y, x), new Point(y+deltaY, x+deltaX))); + } + } catch(ArrayIndexOutOfBoundsException ignored) {} } } } } } } - - // Saut - - return moves; } public int getScore(int player) { - if (currentPlayer == 1) - return n1/(n1+n2); - else - return n2/(n2+n1); + return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1); } public State play(int x, int y) { @@ -75,15 +69,15 @@ public class State { for(int z = -1;z<2;z++){ try { copy.board[x+i][y+z] = copy.getCurrentPlayer(); - increment+=1; + increment++; } catch (IndexOutOfBoundsException ignored) {} } } - if (currentPlayer == 1){ + if (currentPlayer == player1) copy.n1 += increment; - }else{ + else copy.n2 += increment; - } + copy.switchPlayer(); return copy; } @@ -105,7 +99,7 @@ public class State { return copy; } - public void switchPlayer () { + public void switchPlayer() { setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1); } diff --git a/src/othello/players/RandomPlayer.java b/src/othello/players/RandomPlayer.java new file mode 100644 index 0000000..1196add --- /dev/null +++ b/src/othello/players/RandomPlayer.java @@ -0,0 +1,5 @@ +package othello.players; + +public class RandomPlayer { + +}