Changes to Negamax

This commit is contained in:
Arthur 2021-02-23 14:28:01 +01:00
parent d41a3c4584
commit 92a20850fe
5 changed files with 27 additions and 33 deletions

View File

@ -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);

View File

@ -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(){

View File

@ -9,12 +9,12 @@ public class State {
public static List<State> 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<Pair<Point, Point>> 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) {}
}

View File

@ -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<Point, Point> bestMove = null;
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
State nextState = game.play(move);
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;
return game.getScore(game.getCurrentPlayer());
}
else{
return score1 >= score2 ? score2 : -score1;
}
}
int value = Integer.MIN_VALUE;
int bestValue = Integer.MIN_VALUE;
for(Pair<Point, Point> 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;
}
}

View File

@ -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();
}