From f673ed86fbb844e1fe04a03ab63ea986763b4fad Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 24 Feb 2021 14:58:09 +0100 Subject: [PATCH] fix State.play(move) + move and fix evaluate from Negamax and AlphaBeta to Player --- src/othello/State.java | 18 ++++++++++-------- src/othello/players/AlphaBetaPlayer.java | 8 -------- src/othello/players/NegamaxPlayer.java | 8 -------- src/othello/players/Player.java | 9 +++++++++ 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/othello/State.java b/src/othello/State.java index 2122170..a827b57 100644 --- a/src/othello/State.java +++ b/src/othello/State.java @@ -78,20 +78,22 @@ public class State { public State play(Pair move) { State copy = this.copy(); - if(move!=null) { - copy.board[move.getRight().getY()][move.getRight().getX()] = copy.currentPlayer; - if (move.getLeft().isJump(move.getRight(), copy.board)) { - copy.board[move.getLeft().getY()][move.getLeft().getX()] = null; - } + boolean isJump = move.getLeft().isJump(move.getRight(), copy.board); + copy.board[move.getRight().getY()][move.getRight().getX()] = copy.currentPlayer; + if (isJump) { + copy.board[move.getLeft().getY()][move.getLeft().getX()] = null; + copy.board[(move.getLeft().getY() + move.getRight().getY()) / 2][(move.getLeft().getX() + move.getRight().getX()) / 2] = copy.currentPlayer; + } else { for (int i = -1; i < 2; i++) { for (int z = -1; z < 2; z++) { try { - copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.currentPlayer; - } catch (IndexOutOfBoundsException ignored) { - } + if(copy.board[move.getRight().getY() + i][move.getRight().getX() + z] != null) + copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.currentPlayer; + } catch (IndexOutOfBoundsException ignored) {} } } } + int ni = 0, nj = 0; for (Player[] players : copy.board) { for (Player player : players) { diff --git a/src/othello/players/AlphaBetaPlayer.java b/src/othello/players/AlphaBetaPlayer.java index f0bde1a..e0b22b8 100644 --- a/src/othello/players/AlphaBetaPlayer.java +++ b/src/othello/players/AlphaBetaPlayer.java @@ -39,12 +39,4 @@ public class AlphaBetaPlayer extends Player{ 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; - } } diff --git a/src/othello/players/NegamaxPlayer.java b/src/othello/players/NegamaxPlayer.java index 678fc22..4913acd 100644 --- a/src/othello/players/NegamaxPlayer.java +++ b/src/othello/players/NegamaxPlayer.java @@ -38,14 +38,6 @@ public class NegamaxPlayer extends Player { return m; } } - private int evaluate(State game){ - Player winner = game.getWinner(); - if(winner == null) - return 0; - else if(winner == game.getCurrentPlayer()) - return -1; - return 1; - } @Override public boolean equals(Object obj) { diff --git a/src/othello/players/Player.java b/src/othello/players/Player.java index 63dc123..f7ee887 100644 --- a/src/othello/players/Player.java +++ b/src/othello/players/Player.java @@ -14,4 +14,13 @@ public abstract class Player { public abstract Pair play(State board); + protected int evaluate(State game){ + Player winner = game.getWinner(); + if(winner == null) + return 0; + else if(winner == game.getCurrentPlayer()) + return 1; + return -1; + } + }