diff --git a/src/othello/Main.java b/src/othello/Main.java index 8c9a26b..cd4177b 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -1,14 +1,14 @@ package othello; -import othello.players.NegamaxPlayer; +import othello.players.AlphaBetaPlayer; import othello.players.Player; public class Main { public static void main(String[] args) { - Player p1 = new NegamaxPlayer(3); - Player p2 = new NegamaxPlayer(2); + Player p1 = new AlphaBetaPlayer(5); + Player p2 = new AlphaBetaPlayer(5); Player[][] board = initialize(p1, p2); State game = new State(board, p1, p2); System.out.println("joueur 1: " + p1); @@ -19,9 +19,10 @@ public class Main { game = game.play(player.play(game)); } System.out.println(game.toString()); + System.out.println(game.getN1()+" "+ game.getN2()); System.out.println(game.getWinner() + " a gagné la partie"); - System.out.println(game.getScore(p1)); - System.out.println(game.getScore(p2)); + System.out.println("Score joueur 1 -> " + game.getN1()); + System.out.println("Score joueur 2 -> "+ game.getN2()); } diff --git a/src/othello/State.java b/src/othello/State.java index 3fb841d..2122170 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -61,12 +61,17 @@ public class State { public int getScore(Player player) { return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2)); } + public int getN1(){ + return this.n1; + } + public int getN2(){ + return this.n2; + } public Player getWinner() { - int scoreP1 = getScore(player1), scoreP2 = getScore(player2); - if(scoreP1 > scoreP2) + if(this.n1 > this.n2) return player1; - else if(scoreP2 > scoreP1) + else if(this.n2 > this.n1) return player2; return null; } diff --git a/src/othello/players/AlphaBetaPlayer.java b/src/othello/players/AlphaBetaPlayer.java new file mode 100644 index 0000000..f0bde1a --- /dev/null +++ b/src/othello/players/AlphaBetaPlayer.java @@ -0,0 +1,50 @@ +package othello.players; + +import othello.Pair; +import othello.Point; +import othello.State; + +public class AlphaBetaPlayer extends Player{ + + public AlphaBetaPlayer(int depth) { + super(depth); + } + + @Override + public Pair play(State game) { + int bestValue = Integer.MIN_VALUE; + Pair bestMove = null; + for(Pair move : game.getMove(game.getCurrentPlayer())) { + State nextState = game.play(move); + int value = -alphabeta(nextState, this.depth,Integer.MIN_VALUE,Integer.MAX_VALUE); + if (value > bestValue) { + bestValue = value; + bestMove = move; + } + } + return bestMove; + } + + private int alphabeta(State state, int depth,int alpha,int beta) { + if(depth == 0 || state.isOver()) { + return evaluate(state); + } + else{ + for (Pair move : state.getMove(state.getCurrentPlayer())) { + State nextState = state.play(move); + alpha = Math.max(alpha,-alphabeta(nextState,depth-1,-beta,-alpha)); + if(alpha >= beta) + return alpha; + } + return alpha; + } + } + private int evaluate(State game){ + Player winner = game.getWinner(); + if(winner == null) + return 0; + else if(winner == game.getCurrentPlayer()) + return -1; + return 1; + } +}