NegamaxPlayer fixed

This commit is contained in:
Arthur 2021-02-22 21:17:10 +01:00
parent 4389ceae8a
commit c7f1d52352
6 changed files with 39 additions and 46 deletions

View File

@ -8,8 +8,8 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Player p1 = new RandomPlayer(1); Player p1 = new NegamaxPlayer(1);
Player p2 = new NegamaxPlayer(-1); Player p2 = new RandomPlayer(-1);
Player[][] board = initialize(p1, p2); Player[][] board = initialize(p1, p2);
State game = new State(board, p1, p2); State game = new State(board, p1, p2);
System.out.println("joueur 1: " + p1); System.out.println("joueur 1: " + p1);
@ -17,7 +17,7 @@ public class Main {
while(!game.isOver()) { while(!game.isOver()) {
Player player = game.getCurrentPlayer(); Player player = game.getCurrentPlayer();
System.out.println(game.toString()); 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.toString());
System.out.println(game.getWinner() + " a gagné la partie"); System.out.println(game.getWinner() + " a gagné la partie");

View File

@ -10,9 +10,8 @@ public class Point {
this.y = y; this.y = y;
} }
public boolean isJump(Point other) { public boolean isJump(Point other,State game) {
double value = Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2); return (game.board[(x+other.getX())/2][(y+other.getY())/2] != null);
return value == 4 || value == 8;
} }
public int getX(){ public int getX(){

View File

@ -9,12 +9,12 @@ public class State {
public static List<State> previousSituations = new LinkedList<>(); public static List<State> previousSituations = new LinkedList<>();
private final Player[][] board; public Player[][] board;
private final Player player1; public Player player1;
private final Player player2; public Player player2;
private Player currentPlayer; public Player currentPlayer;
private int n1; public int n1;
private int n2; public int n2;
public State(Player[][] board, Player p1, Player p2) { public State(Player[][] board, Player p1, Player p2) {
this.board = board; this.board = board;
@ -55,7 +55,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)) if(this.board[other.getY()][other.getX()] == null && current.isJump(other,this))
moves.add(new Pair<>(current, other)); moves.add(new Pair<>(current, other));
} catch(ArrayIndexOutOfBoundsException ignored) {} } catch(ArrayIndexOutOfBoundsException ignored) {}
} }
@ -85,20 +85,17 @@ public class State {
for(int i = -1; i < 2; i++){ for(int i = -1; i < 2; i++){
for(int z = -1; z < 2; z++){ for(int z = -1; z < 2; z++){
try { 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(); copy.board[move.getRight().getY() + i][move.getRight().getX() + z] = copy.getCurrentPlayer();
}
} catch (IndexOutOfBoundsException ignored) {} } catch (IndexOutOfBoundsException ignored) {}
} }
} }
int ni = 0, nj = 0; int ni = 0, nj = 0;
for(int i = 0; i < board.length; i++) { for (Player[] players : board) {
for(int j = 0; j < board[i].length; j++) { for (Player player : players) {
if(board[i][j] == player1){ if (player == player1) {
ni++; ni++;
} }
if(board[i][j] == player2){ else if (player == player2) {
nj++; nj++;
} }
} }

View File

@ -11,35 +11,36 @@ public class NegamaxPlayer extends Player {
} }
@Override @Override
public Pair<Point, Point> play(State game) { public Pair<Point, Point> play(State game,int depth) {
Integer bestValue = null; 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())) {
int v = negamax(game.play(move),10,-id); State nextState = game.play(move);
if(bestValue == null || v > bestValue) { int value = -negamax(nextState,depth);
bestValue = v; if(value > bestValue){
bestValue = value;
bestMove = move; bestMove = move;
} }
} }
System.out.println("Negamax à joué");
return bestMove; return bestMove;
} }
private int negamax(State game, int depth, int id) { private Integer negamax(State game, int depth) {
if(depth == 0 || game.isOver()) { if(depth == 0 || game.isOver()) {
if(game.getWinner() == game.getCurrentPlayer()) int score1 = game.getScore(game.player1);
return game.getWinner().getId(); int score2 = game.getScore(game.player2);
else if(game.getWinner() != null) if (game.getCurrentPlayer() == game.player1){
return game.getWinner().getId(); return score1 >= score2 ? score1 : -score2;
return 0;
} }
Integer value = null; else{
Player player = game.getPlayerById(id); return score1 >= score2 ? score2 : -score1;
for(Pair<Point, Point> move : game.getMove(player)) {
int v = negamax(game.play(move),10,-id);
if(value == null || v>value){
value = v;
} }
} }
int value = Integer.MIN_VALUE;
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
value = Math.max(value, -negamax(game.play(move),depth-1));
}
return value; return value;
} }

View File

@ -8,14 +8,10 @@ public abstract class Player {
protected final int id; protected final int id;
public int getId() {
return this.id;
}
public Player(int id) { public Player(int id) {
this.id = id; this.id = id;
} }
public abstract Pair<Point, Point> play(State board); public abstract Pair<Point, Point> play(State board,int depth);
} }

View File

@ -17,7 +17,7 @@ public class RandomPlayer extends Player {
} }
@Override @Override
public Pair<Point, Point> play(State game) { public Pair<Point, Point> play(State game, int depth) {
LinkedList<Pair<Point, Point>> moves = game.getMove(this); LinkedList<Pair<Point, Point>> moves = game.getMove(this);
return moves.get(random.nextInt(moves.size())); return moves.get(random.nextInt(moves.size()));
} }