From 57c31435f029c149441ef6f24b6b6448b5b10084 Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Wed, 24 Feb 2021 11:27:50 +0100 Subject: [PATCH] Negamax --- src/othello/Main.java | 5 +++-- src/othello/State.java | 14 ++++++++------ src/othello/players/NegamaxPlayer.java | 8 ++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/othello/Main.java b/src/othello/Main.java index 3cfbef2..8c9a26b 100644 --- a/src/othello/Main.java +++ b/src/othello/Main.java @@ -7,8 +7,8 @@ public class Main { public static void main(String[] args) { - Player p1 = new NegamaxPlayer(4); - Player p2 = new NegamaxPlayer(4); + Player p1 = new NegamaxPlayer(3); + Player p2 = new NegamaxPlayer(2); Player[][] board = initialize(p1, p2); State game = new State(board, p1, p2); System.out.println("joueur 1: " + p1); @@ -22,6 +22,7 @@ public class Main { System.out.println(game.getWinner() + " a gagné la partie"); System.out.println(game.getScore(p1)); System.out.println(game.getScore(p2)); + } public static Player[][] initialize(Player p1, Player p2){ diff --git a/src/othello/State.java b/src/othello/State.java index 92de7d1..3fb841d 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -107,18 +107,19 @@ public class State { return currentPlayer; } - public void setCurrentPlayer(Player currentPlayer) { - this.currentPlayer = currentPlayer; + public void setCurrentPlayer(Player player) { + this.currentPlayer = player; } public State copy () { - State copy = new State(this.board, this.player1, this.player2); + State copy = new State(new Player[7][7], this.player1, this.player2); for (int i = 0; i < this.board.length; i++) { - System.arraycopy(this.board[i], 0, copy.board[i], 0, this.board.length); + System.arraycopy(this.board[i], 0, copy.board[i], 0, this.board[i].length); } - copy.setCurrentPlayer(this.currentPlayer); + copy.setCurrentPlayer(this.getCurrentPlayer()); copy.n1 = n1; copy.n2 = n2; + return copy; } @@ -150,7 +151,8 @@ public class State { public boolean equals(Object state) { boolean bool; bool = ( state instanceof State); - bool = bool && (this.getCurrentPlayer() == ((State) state).getCurrentPlayer()) && (this.player1 == ((State) state).player1) && (this.player2 == ((State) state).player2); + bool = bool && (this.getCurrentPlayer().equals(((State) state).getCurrentPlayer())); + bool = bool && (this.player1.equals(((State) state).player1)) && (this.player2.equals(((State) state).player2)); bool = bool && (this.n1 == ((State)state).n1)&& (this.n2 == ((State)state).n2); for (int i = 0; i < this.board.length; i++) { for (int y = 0; y < this.board.length; y++){ diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java index ac90df6..678fc22 100644 --- a/src/othello/players/NegamaxPlayer.java +++ b/src/othello/players/NegamaxPlayer.java @@ -43,8 +43,12 @@ public class NegamaxPlayer extends Player { if(winner == null) return 0; else if(winner == game.getCurrentPlayer()) - return 1; - return -1; + return -1; + return 1; } + @Override + public boolean equals(Object obj) { + return (obj instanceof NegamaxPlayer); + } }