diff --git a/src/othello/Main.java b/src/othello/Main.java index 79f97f1..bd36cbc 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -8,8 +8,8 @@ public class Main { public static void main(String[] args) { - Player p1 = new RandomPlayer(1); - Player p2 = new NegamaxPlayer(-1); + Player p1 = new NegamaxPlayer(1); + Player p2 = new RandomPlayer(-1); Player[][] board = initialize(p1, p2); State game = new State(board, p1, p2); System.out.println("joueur 1: " + p1); @@ -17,7 +17,7 @@ public class Main { while(!game.isOver()) { Player player = game.getCurrentPlayer(); System.out.println(game.toString()); - game = game.play(player.play(game)); + game = game.play(player.play(game,100)); } System.out.println(game.toString()); System.out.println(game.getWinner() + " a gagné la partie"); diff --git a/src/othello/Point.java b/src/othello/Point.java index e0d2011..e71128a 100644 --- a/src/othello/Point.java +++ b/src/othello/Point.java @@ -9,10 +9,9 @@ public class Point { this.x = x; this.y = y; } - - public boolean isJump(Point other) { - double value = Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2); - return value == 4 || value == 8; + + public boolean isJump(Point other,State game) { + return (game.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 e18315d..3d34e10 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -9,12 +9,12 @@ public class State { public static List previousSituations = new LinkedList<>(); - private final Player[][] board; - private final Player player1; - private final Player player2; - private Player currentPlayer; - private int n1; - private int n2; + public Player[][] board; + public Player player1; + public Player player2; + public Player currentPlayer; + public int n1; + public int n2; public State(Player[][] board, Player p1, Player p2) { this.board = board; @@ -55,7 +55,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)) + if(this.board[other.getY()][other.getX()] == null && current.isJump(other,this)) moves.add(new Pair<>(current, other)); } catch(ArrayIndexOutOfBoundsException ignored) {} } @@ -85,20 +85,17 @@ public class State { for(int i = -1; i < 2; i++){ for(int z = -1; z < 2; z++){ try { - if(copy.board[move.getRight().getY() + i][move.getRight().getX() + z] != copy.getCurrentPlayer() - && !move.getLeft().isJump(move.getRight())){ copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.getCurrentPlayer(); - } } catch (IndexOutOfBoundsException ignored) {} } } int ni = 0, nj = 0; - for(int i = 0; i < board.length; i++) { - for(int j = 0; j < board[i].length; j++) { - if(board[i][j] == player1){ + for (Player[] players : board) { + for (Player player : players) { + if (player == player1) { ni++; } - if(board[i][j] == player2){ + else if (player == player2) { nj++; } } @@ -111,7 +108,7 @@ public class State { public Player getCurrentPlayer() { return currentPlayer; } - + public void setCurrentPlayer(Player currentPlayer) { this.currentPlayer = currentPlayer; } diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java index d80a8e9..786c29f 100644 --- a/src/othello/players/NegamaxPlayer.java +++ b/src/othello/players/NegamaxPlayer.java @@ -11,34 +11,35 @@ public class NegamaxPlayer extends Player { } @Override - public Pair play(State game) { - Integer bestValue = null; + public Pair play(State game,int depth) { + int bestValue = Integer.MIN_VALUE; Pair bestMove = null; for(Pair move : game.getMove(game.getCurrentPlayer())) { - int v = negamax(game.play(move),10,-id); - if(bestValue == null || v > bestValue) { - bestValue = v; + State nextState = game.play(move); + int value = -negamax(nextState,depth); + if(value > bestValue){ + bestValue = value; bestMove = move; } } + System.out.println("Negamax à joué"); return bestMove; } - private int negamax(State game, int depth, int id) { + private Integer negamax(State game, int depth) { if(depth == 0 || game.isOver()) { - if(game.getWinner() == game.getCurrentPlayer()) - return game.getWinner().getId(); - else if(game.getWinner() != null) - return game.getWinner().getId(); - return 0; - } - Integer value = null; - Player player = game.getPlayerById(id); - for(Pair move : game.getMove(player)) { - int v = negamax(game.play(move),10,-id); - if(value == null || v>value){ - value = v; + 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; + } + } + int value = Integer.MIN_VALUE; + for(Pair move : game.getMove(game.getCurrentPlayer())) { + value = Math.max(value, -negamax(game.play(move),depth-1)); } return value; } diff --git a/src/othello/players/Player.java b/src/othello/players/Player.java index f006216..7363360 100644 --- a/src/othello/players/Player.java +++ b/src/othello/players/Player.java @@ -8,14 +8,10 @@ public abstract class Player { protected final int id; - public int getId() { - return this.id; - } - public Player(int id) { this.id = id; } - public abstract Pair play(State board); + public abstract Pair play(State board,int depth); } diff --git a/src/othello/players/RandomPlayer.java b/src/othello/players/RandomPlayer.java index 4560640..78e9294 100644 --- a/src/othello/players/RandomPlayer.java +++ b/src/othello/players/RandomPlayer.java @@ -17,7 +17,7 @@ public class RandomPlayer extends Player { } @Override - public Pair play(State game) { + public Pair play(State game, int depth) { LinkedList> moves = game.getMove(this); return moves.get(random.nextInt(moves.size())); }