NegamaxPlayer fixed
This commit is contained in:
parent
4389ceae8a
commit
c7f1d52352
@ -8,8 +8,8 @@ public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Player p1 = new RandomPlayer(1);
|
||||
Player p2 = new NegamaxPlayer(-1);
|
||||
Player p1 = new NegamaxPlayer(1);
|
||||
Player p2 = new RandomPlayer(-1);
|
||||
Player[][] board = initialize(p1, p2);
|
||||
State game = new State(board, p1, p2);
|
||||
System.out.println("joueur 1: " + p1);
|
||||
@ -17,7 +17,7 @@ public class Main {
|
||||
while(!game.isOver()) {
|
||||
Player player = game.getCurrentPlayer();
|
||||
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.getWinner() + " a gagné la partie");
|
||||
|
@ -10,9 +10,8 @@ public class Point {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public boolean isJump(Point other) {
|
||||
double value = Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2);
|
||||
return value == 4 || value == 8;
|
||||
public boolean isJump(Point other,State game) {
|
||||
return (game.board[(x+other.getX())/2][(y+other.getY())/2] != null);
|
||||
}
|
||||
|
||||
public int getX(){
|
||||
|
@ -9,12 +9,12 @@ public class State {
|
||||
|
||||
public static List<State> previousSituations = new LinkedList<>();
|
||||
|
||||
private final Player[][] board;
|
||||
private final Player player1;
|
||||
private final Player player2;
|
||||
private Player currentPlayer;
|
||||
private int n1;
|
||||
private int n2;
|
||||
public Player[][] board;
|
||||
public Player player1;
|
||||
public Player player2;
|
||||
public Player currentPlayer;
|
||||
public int n1;
|
||||
public int n2;
|
||||
|
||||
public State(Player[][] board, Player p1, Player p2) {
|
||||
this.board = board;
|
||||
@ -55,7 +55,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))
|
||||
if(this.board[other.getY()][other.getX()] == null && current.isJump(other,this))
|
||||
moves.add(new Pair<>(current, other));
|
||||
} catch(ArrayIndexOutOfBoundsException ignored) {}
|
||||
}
|
||||
@ -85,20 +85,17 @@ public class State {
|
||||
for(int i = -1; i < 2; i++){
|
||||
for(int z = -1; z < 2; z++){
|
||||
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();
|
||||
}
|
||||
} catch (IndexOutOfBoundsException ignored) {}
|
||||
}
|
||||
}
|
||||
int ni = 0, nj = 0;
|
||||
for(int i = 0; i < board.length; i++) {
|
||||
for(int j = 0; j < board[i].length; j++) {
|
||||
if(board[i][j] == player1){
|
||||
for (Player[] players : board) {
|
||||
for (Player player : players) {
|
||||
if (player == player1) {
|
||||
ni++;
|
||||
}
|
||||
if(board[i][j] == player2){
|
||||
else if (player == player2) {
|
||||
nj++;
|
||||
}
|
||||
}
|
||||
|
@ -11,34 +11,35 @@ public class NegamaxPlayer extends Player {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Point, Point> play(State game) {
|
||||
Integer bestValue = null;
|
||||
public Pair<Point, Point> play(State game,int depth) {
|
||||
int bestValue = Integer.MIN_VALUE;
|
||||
Pair<Point, Point> bestMove = null;
|
||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
||||
int v = negamax(game.play(move),10,-id);
|
||||
if(bestValue == null || v > bestValue) {
|
||||
bestValue = v;
|
||||
State nextState = game.play(move);
|
||||
int value = -negamax(nextState,depth);
|
||||
if(value > bestValue){
|
||||
bestValue = value;
|
||||
bestMove = move;
|
||||
}
|
||||
}
|
||||
System.out.println("Negamax à joué");
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private int negamax(State game, int depth, int id) {
|
||||
private Integer negamax(State game, int depth) {
|
||||
if(depth == 0 || game.isOver()) {
|
||||
if(game.getWinner() == game.getCurrentPlayer())
|
||||
return game.getWinner().getId();
|
||||
else if(game.getWinner() != null)
|
||||
return game.getWinner().getId();
|
||||
return 0;
|
||||
}
|
||||
Integer value = null;
|
||||
Player player = game.getPlayerById(id);
|
||||
for(Pair<Point, Point> move : game.getMove(player)) {
|
||||
int v = negamax(game.play(move),10,-id);
|
||||
if(value == null || v>value){
|
||||
value = v;
|
||||
int score1 = game.getScore(game.player1);
|
||||
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;
|
||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
||||
value = Math.max(value, -negamax(game.play(move),depth-1));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
@ -8,14 +8,10 @@ public abstract class Player {
|
||||
|
||||
protected final int id;
|
||||
|
||||
public int getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public Player(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public abstract Pair<Point, Point> play(State board);
|
||||
public abstract Pair<Point, Point> play(State board,int depth);
|
||||
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ public class RandomPlayer extends Player {
|
||||
}
|
||||
|
||||
@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);
|
||||
return moves.get(random.nextInt(moves.size()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user