From ac9ddcab27480286e87d109965578b3ea668721b Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 12 Feb 2021 09:18:03 +0100 Subject: [PATCH] converted board from int to AbstractPlayer --- .gitignore | 4 +++- src/othello/Main.java | 26 ++++++++++++++++---------- src/othello/State.java | 37 +++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index e357234..66c5a93 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ .project .classpath .metadata/ -.settings \ No newline at end of file +.settings +.idea +*.iml diff --git a/src/othello/Main.java b/src/othello/Main.java index 1dfbe0a..04b21be 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -2,24 +2,30 @@ package othello; import java.util.ArrayList; +import othello.players.AbstractPlayer; +import othello.players.RandomPlayer; + public class Main { public static void main(String[] args) { - int p1 = 1, p2 = 2; - int[][] board = initialize(p1, p2); + AbstractPlayer p1 = new RandomPlayer(); + AbstractPlayer p2 = new RandomPlayer(); + AbstractPlayer[][] board = initialize(p1, p2); State game = new State(board, p1, p2); - board[1][5]=game.getCurrentPlayer(); - /*while(!game.isOver()) { - int player = game.getCurrentPlayer(); + // board[1][5]=game.getCurrentPlayer(); + while(!game.isOver()) { + AbstractPlayer player = game.getCurrentPlayer(); ArrayList> moves = game.getMove(player); - } */ - ArrayList> moves = game.getMove(game.getCurrentPlayer()); - System.out.println(moves.toString()); + game.toString(); + player.play(moves); + } + // ArrayList> moves = game.getMove(game.getCurrentPlayer()); + // System.out.println(moves.toString()); } - public static int[][] initialize(int p1, int p2){ - int[][] board = new int[7][7]; + public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){ + AbstractPlayer[][] board = new AbstractPlayer[7][7]; board[0][0] = p2; board[0][6] = p1; board[6][0] = p1; diff --git a/src/othello/State.java b/src/othello/State.java index 128ab9a..bca19ae 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -2,16 +2,18 @@ package othello; import java.util.ArrayList; +import othello.players.AbstractPlayer; + public class State { - private int[][] board; - private int player1; - private int player2; - private int currentPlayer; + private AbstractPlayer[][] board; + private AbstractPlayer player1; + private AbstractPlayer player2; + private AbstractPlayer currentPlayer; private int n1; private int n2; - public State(int[][] board, int p1, int p2, int n1, int n2) { + public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2, int n1, int n2) { this.board = board; this.player1 = p1; this.player2 = p2; @@ -20,7 +22,7 @@ public class State { this.n2 = n2; } - public State(int[][] board, int p1, int p2) { + public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2) { this(board, p1, p2, 2, 2); } @@ -30,7 +32,7 @@ public class State { return getMove(player1).isEmpty() && getMove(player2).isEmpty(); } - public ArrayList> getMove(int player) { + public ArrayList> getMove(AbstractPlayer player) { // Pair ArrayList> moves = new ArrayList<>(); // Parcours du plateau de jeu @@ -44,11 +46,10 @@ public class State { 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) { + if (this.board[y+deltaY][x+deltaX] == null) { moves.add(new Pair(new Point(y, x), new Point(y+deltaY, x+deltaX))); - } - if(this.board[y+deltaY][x+deltaX]!=0){ - if(this.board[y+2*deltaY][x+2*deltaX] == 0) + } 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))); } } catch(ArrayIndexOutOfBoundsException ignored) {} @@ -61,7 +62,7 @@ public class State { return moves; } - public int getScore(int player) { + public int getScore(AbstractPlayer player) { return currentPlayer == player1 ? n1/(n1+n2) : n2/(n2+n1); } @@ -87,11 +88,11 @@ public class State { copy.switchPlayer(); return copy; } - public int getCurrentPlayer() { + public AbstractPlayer getCurrentPlayer() { return currentPlayer; } - public void setCurrentPlayer(int currentPlayer) { + public void setCurrentPlayer(AbstractPlayer currentPlayer) { this.currentPlayer = currentPlayer; } @@ -109,4 +110,12 @@ public class State { setCurrentPlayer(getCurrentPlayer() == this.player1 ? player2 : player1); } + /** + * TODO: display the current state of the board + */ + @Override + public String toString() { + return null; + } + }