From ca65348b1f6a69f07d542885ddaaeea224ea77d4 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 16 Feb 2021 13:41:20 +0100 Subject: [PATCH] converted AbstractPlayer to interface --- src/othello/Main.java | 16 ++++++++-------- src/othello/State.java | 24 ++++++++++++------------ src/othello/players/AbstractPlayer.java | 12 ------------ src/othello/players/NegamaxPlayer.java | 20 ++++++++++++++++++++ src/othello/players/Player.java | 13 +++++++++++++ src/othello/players/RandomPlayer.java | 5 +++-- 6 files changed, 56 insertions(+), 34 deletions(-) delete mode 100644 src/othello/players/AbstractPlayer.java create mode 100644 src/othello/players/NegamaxPlayer.java create mode 100644 src/othello/players/Player.java diff --git a/src/othello/Main.java b/src/othello/Main.java index b3d2a9d..b4f255c 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -2,30 +2,30 @@ package othello; import java.util.ArrayList; -import othello.players.AbstractPlayer; +import othello.players.Player; import othello.players.RandomPlayer; public class Main { public static void main(String[] args) { - AbstractPlayer p1 = new RandomPlayer(); - AbstractPlayer p2 = new RandomPlayer(); - AbstractPlayer[][] board = initialize(p1, p2); + Player p1 = new RandomPlayer(); + Player p2 = new RandomPlayer(); + Player[][] board = initialize(p1, p2); State game = new State(board, p1, p2); System.out.println("joueur 1: " + p1); System.out.println("joueur 2: " + p2); while(!game.isOver()) { - AbstractPlayer player = game.getCurrentPlayer(); + Player player = game.getCurrentPlayer(); ArrayList> moves = game.getMove(player); System.out.println(game.toString()); - game = game.play(player.play(moves)); + game = game.play(player.play(moves, game, player)); } System.out.println("C'est " + game.getWinner() + " qui a gagné"); } - public static AbstractPlayer[][] initialize(AbstractPlayer p1, AbstractPlayer p2){ - AbstractPlayer[][] board = new AbstractPlayer[7][7]; + 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; diff --git a/src/othello/State.java b/src/othello/State.java index 9cda82d..6cc05a5 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -2,18 +2,18 @@ package othello; import java.util.ArrayList; -import othello.players.AbstractPlayer; +import othello.players.Player; public class State { - private AbstractPlayer[][] board; - private AbstractPlayer player1; - private AbstractPlayer player2; - private AbstractPlayer currentPlayer; + private Player[][] board; + private Player player1; + private Player player2; + private Player currentPlayer; private int n1; private int n2; - public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2, int n1, int n2) { + public State(Player[][] board, Player p1, Player p2, int n1, int n2) { this.board = board; this.player1 = p1; this.player2 = p2; @@ -22,7 +22,7 @@ public class State { this.n2 = n2; } - public State(AbstractPlayer[][] board, AbstractPlayer p1, AbstractPlayer p2) { + public State(Player[][] board, Player p1, Player p2) { this(board, p1, p2, 2, 2); } @@ -32,7 +32,7 @@ public class State { return getMove(player1).isEmpty() && getMove(player2).isEmpty(); } - public ArrayList> getMove(AbstractPlayer player) { + public ArrayList> getMove(Player player) { // Pair ArrayList> moves = new ArrayList<>(); // Parcours du plateau de jeu @@ -64,11 +64,11 @@ public class State { return moves; } - public int getScore(AbstractPlayer player) { + public int getScore(Player player) { return player == player1 ? n1/(n1+n2) : n2/(n2+n1); } - public AbstractPlayer getWinner() { + public Player getWinner() { int scoreP1 = getScore(player1), scoreP2 = getScore(player2); if(scoreP1 > scoreP2) return player1; @@ -99,11 +99,11 @@ public class State { copy.switchPlayer(); return copy; } - public AbstractPlayer getCurrentPlayer() { + public Player getCurrentPlayer() { return currentPlayer; } - public void setCurrentPlayer(AbstractPlayer currentPlayer) { + public void setCurrentPlayer(Player currentPlayer) { this.currentPlayer = currentPlayer; } diff --git a/src/othello/players/AbstractPlayer.java b/src/othello/players/AbstractPlayer.java deleted file mode 100644 index 6dd5a53..0000000 --- a/src/othello/players/AbstractPlayer.java +++ /dev/null @@ -1,12 +0,0 @@ -package othello.players; - -import java.util.ArrayList; - -import othello.Pair; -import othello.Point; - -public abstract class AbstractPlayer { - - public abstract Pair play(ArrayList> moves); - -} diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java new file mode 100644 index 0000000..f49d694 --- /dev/null +++ b/src/othello/players/NegamaxPlayer.java @@ -0,0 +1,20 @@ +package othello.players; + +import othello.Pair; +import othello.Point; +import othello.State; + +import java.util.ArrayList; + +public class NegamaxPlayer implements Player { + + @Override + public Pair play(ArrayList> moves, State game, Player player) { + return null; + } + + public void negamax(State game, int depth, Player player) { + + } + +} diff --git a/src/othello/players/Player.java b/src/othello/players/Player.java new file mode 100644 index 0000000..e9880ee --- /dev/null +++ b/src/othello/players/Player.java @@ -0,0 +1,13 @@ +package othello.players; + +import java.util.ArrayList; + +import othello.Pair; +import othello.Point; +import othello.State; + +public interface Player { + + public Pair play(ArrayList> moves, State board, Player player); + +} diff --git a/src/othello/players/RandomPlayer.java b/src/othello/players/RandomPlayer.java index 3510b6c..88c8764 100644 --- a/src/othello/players/RandomPlayer.java +++ b/src/othello/players/RandomPlayer.java @@ -5,11 +5,12 @@ import java.util.Random; import othello.Pair; import othello.Point; +import othello.State; -public class RandomPlayer extends AbstractPlayer { +public class RandomPlayer implements Player { @Override - public Pair play(ArrayList> moves) { + public Pair play(ArrayList> moves, State game, Player player) { return moves.get(new Random().nextInt(moves.size())); }