Changes to Negamax
This commit is contained in:
parent
d41a3c4584
commit
92a20850fe
@ -7,7 +7,7 @@ public class Main {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Player p1 = new NegamaxPlayer(1);
|
Player p1 = new NegamaxPlayer(100);
|
||||||
Player p2 = new NegamaxPlayer(100);
|
Player p2 = new NegamaxPlayer(100);
|
||||||
Player[][] board = initialize(p1, p2);
|
Player[][] board = initialize(p1, p2);
|
||||||
State game = new State(board, p1, p2);
|
State game = new State(board, p1, p2);
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package othello;
|
package othello;
|
||||||
|
|
||||||
|
import othello.players.Player;
|
||||||
|
|
||||||
public class Point {
|
public class Point {
|
||||||
|
|
||||||
private int x;
|
private int x;
|
||||||
@ -10,8 +12,8 @@ public class Point {
|
|||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isJump(Point other,State game) {
|
public boolean isJump(Point other, Player[][] board) {
|
||||||
return (game.board[(x+other.getX())/2][(y+other.getY())/2] != null);
|
return (board[(x+other.getX())/2][(y+other.getY())/2] != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX(){
|
public int getX(){
|
||||||
|
@ -9,12 +9,12 @@ public class State {
|
|||||||
|
|
||||||
public static List<State> previousSituations = new LinkedList<>();
|
public static List<State> previousSituations = new LinkedList<>();
|
||||||
|
|
||||||
public Player[][] board;
|
private final Player[][] board;
|
||||||
public Player player1;
|
private final Player player1;
|
||||||
public Player player2;
|
private final Player player2;
|
||||||
public Player currentPlayer;
|
private Player currentPlayer;
|
||||||
public int n1;
|
private int n1;
|
||||||
public int n2;
|
private int n2;
|
||||||
|
|
||||||
public State(Player[][] board, Player p1, Player p2) {
|
public State(Player[][] board, Player p1, Player p2) {
|
||||||
this.board = board;
|
this.board = board;
|
||||||
@ -29,12 +29,8 @@ public class State {
|
|||||||
return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty());
|
return n1 == 0 || n2 == 0 || (getMove(player1).isEmpty() && getMove(player2).isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player getPlayerById(int id) {
|
public Player[][] getBoard(){
|
||||||
if(id == 1)
|
return this.board;
|
||||||
return player1;
|
|
||||||
else if(id == -1)
|
|
||||||
return player2;
|
|
||||||
throw new IllegalArgumentException("Invalid player id: " + id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LinkedList<Pair<Point, Point>> getMove(Player player) {
|
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)));
|
moves.add(new Pair<>(current, new Point(y + deltaY, x + deltaX)));
|
||||||
}
|
}
|
||||||
Point other = new Point(y + 2 * deltaY, x + 2 * 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));
|
moves.add(new Pair<>(current, other));
|
||||||
} catch(ArrayIndexOutOfBoundsException ignored) {}
|
} catch(ArrayIndexOutOfBoundsException ignored) {}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import othello.State;
|
|||||||
|
|
||||||
public class NegamaxPlayer extends Player {
|
public class NegamaxPlayer extends Player {
|
||||||
|
|
||||||
|
|
||||||
public NegamaxPlayer(int depth) {
|
public NegamaxPlayer(int depth) {
|
||||||
super(depth);
|
super(depth);
|
||||||
}
|
}
|
||||||
@ -15,33 +16,28 @@ public class NegamaxPlayer extends Player {
|
|||||||
int bestValue = Integer.MIN_VALUE;
|
int bestValue = Integer.MIN_VALUE;
|
||||||
Pair<Point, Point> bestMove = null;
|
Pair<Point, Point> bestMove = null;
|
||||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
||||||
State nextState = game.play(move);
|
State nextState = game.copy();
|
||||||
int value = -negamax(nextState,depth);
|
nextState.play(move);
|
||||||
if(value > bestValue){
|
int value = -negamax(nextState, depth);
|
||||||
|
if (value > bestValue) {
|
||||||
bestValue = value;
|
bestValue = value;
|
||||||
bestMove = move;
|
bestMove = move;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("Negamax à joué");
|
|
||||||
return bestMove;
|
return bestMove;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer negamax(State game, int depth) {
|
private Integer negamax(State game, int depth) {
|
||||||
if(depth == 0 || game.isOver()) {
|
if(depth == 0 || game.isOver()) {
|
||||||
int score1 = game.getScore(game.player1);
|
return game.getScore(game.getCurrentPlayer());
|
||||||
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;
|
int bestValue = Integer.MIN_VALUE;
|
||||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ public class RandomPlayer extends Player {
|
|||||||
|
|
||||||
Random random;
|
Random random;
|
||||||
|
|
||||||
public RandomPlayer(int id) {
|
public RandomPlayer(int depth) {
|
||||||
super(id);
|
super(depth);
|
||||||
random = new Random();
|
random = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user