From 92a20850febfc950528317857f6ea36c396a5be9 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Tue, 23 Feb 2021 14:28:01 +0100 Subject: [PATCH] Changes to Negamax --- src/othello/Main.java | 2 +- src/othello/Point.java | 6 ++++-- src/othello/State.java | 22 +++++++++------------- src/othello/players/NegamaxPlayer.java | 26 +++++++++++--------------- src/othello/players/RandomPlayer.java | 4 ++-- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/src/othello/Main.java b/src/othello/Main.java index 70f9b8f..827b346 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -7,7 +7,7 @@ public class Main { public static void main(String[] args) { - Player p1 = new NegamaxPlayer(1); + Player p1 = new NegamaxPlayer(100); Player p2 = new NegamaxPlayer(100); Player[][] board = initialize(p1, p2); State game = new State(board, p1, p2); diff --git a/src/othello/Point.java b/src/othello/Point.java index e71128a..8763411 100644 --- a/src/othello/Point.java +++ b/src/othello/Point.java @@ -1,5 +1,7 @@ package othello; +import othello.players.Player; + public class Point { private int x; @@ -10,8 +12,8 @@ public class Point { this.y = y; } - public boolean isJump(Point other,State game) { - return (game.board[(x+other.getX())/2][(y+other.getY())/2] != null); + public boolean isJump(Point other, Player[][] board) { + return (board[(x+other.getX())/2][(y+other.getY())/2] != null); } public int getX(){ diff --git a/src/othello/State.java b/src/othello/State.java index 3d34e10..ce1fe27 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -9,12 +9,12 @@ public class State { public static List previousSituations = new LinkedList<>(); - public Player[][] board; - public Player player1; - public Player player2; - public Player currentPlayer; - public int n1; - public int n2; + private final Player[][] board; + private final Player player1; + private final Player player2; + private Player currentPlayer; + private int n1; + private int n2; public State(Player[][] board, Player p1, Player p2) { this.board = board; @@ -29,12 +29,8 @@ public class State { return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty()); } - public Player getPlayerById(int id) { - if(id == 1) - return player1; - else if(id == -1) - return player2; - throw new IllegalArgumentException("Invalid player id: " + id); + public Player[][] getBoard(){ + return this.board; } public LinkedList> getMove(Player player) { @@ -55,7 +51,7 @@ public class State { moves.add(new Pair<>(current, new Point(y + deltaY, x + deltaX))); } Point other = new Point(y + 2 * deltaY, x + 2 * deltaX); - if(this.board[other.getY()][other.getX()] == null && current.isJump(other,this)) + if(this.board[other.getY()][other.getX()] == null && current.isJump(other,getBoard())) moves.add(new Pair<>(current, other)); } catch(ArrayIndexOutOfBoundsException ignored) {} } diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java index 57a084d..e26e8e8 100644 --- a/src/othello/players/NegamaxPlayer.java +++ b/src/othello/players/NegamaxPlayer.java @@ -6,6 +6,7 @@ import othello.State; public class NegamaxPlayer extends Player { + public NegamaxPlayer(int depth) { super(depth); } @@ -15,33 +16,28 @@ public class NegamaxPlayer extends Player { int bestValue = Integer.MIN_VALUE; Pair bestMove = null; for(Pair move : game.getMove(game.getCurrentPlayer())) { - State nextState = game.play(move); - int value = -negamax(nextState,depth); - if(value > bestValue){ + State nextState = game.copy(); + nextState.play(move); + int value = -negamax(nextState, depth); + if (value > bestValue) { bestValue = value; bestMove = move; } } - System.out.println("Negamax à joué"); return bestMove; } private Integer negamax(State game, int depth) { if(depth == 0 || game.isOver()) { - int score1 = game.getScore(game.player1); - int score2 = game.getScore(game.player2); - if (game.getCurrentPlayer() == game.player1){ - return score1 >= score2 ? score1 : -score2; - } - else{ - return score1 >= score2 ? score2 : -score1; - } + return game.getScore(game.getCurrentPlayer()); } - int value = Integer.MIN_VALUE; + int bestValue = Integer.MIN_VALUE; for(Pair move : game.getMove(game.getCurrentPlayer())) { - value = Math.max(value, -negamax(game.play(move),depth-1)); + State nextState = game.copy(); + bestValue = Math.max(bestValue,-negamax(nextState.play(move),depth-1)); + } - return value; + return bestValue; } } diff --git a/src/othello/players/RandomPlayer.java b/src/othello/players/RandomPlayer.java index 4560640..0066621 100644 --- a/src/othello/players/RandomPlayer.java +++ b/src/othello/players/RandomPlayer.java @@ -11,8 +11,8 @@ public class RandomPlayer extends Player { Random random; - public RandomPlayer(int id) { - super(id); + public RandomPlayer(int depth) { + super(depth); random = new Random(); }