AlphaBeta implemented
This commit is contained in:
parent
095c3eaab2
commit
3f58fd52ed
@ -1,14 +1,14 @@
|
||||
package othello;
|
||||
|
||||
import othello.players.NegamaxPlayer;
|
||||
import othello.players.Player;
|
||||
import othello.players.RandomPlayer;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Player p1 = new RandomPlayer(10);
|
||||
Player p2 = new RandomPlayer(10);
|
||||
Player p1 = new NegamaxPlayer(4);
|
||||
Player p2 = new NegamaxPlayer(4);
|
||||
Player[][] board = initialize(p1, p2);
|
||||
State game = new State(board, p1, p2);
|
||||
System.out.println("joueur 1: " + p1);
|
||||
|
@ -59,7 +59,7 @@ public class State {
|
||||
}
|
||||
|
||||
public int getScore(Player player) {
|
||||
return player == player1 ? n1 : n2;
|
||||
return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2));
|
||||
}
|
||||
|
||||
public Player getWinner() {
|
||||
@ -73,15 +73,18 @@ public class State {
|
||||
|
||||
public State play(Pair<Point,Point> move) {
|
||||
State copy = this.copy();
|
||||
copy.board[move.getRight().getY()][move.getRight().getX()] = copy.getCurrentPlayer();
|
||||
if(move.getLeft().isJump(move.getRight(),copy.board)){
|
||||
copy.board[move.getLeft().getY()][move.getLeft().getX()] = null;
|
||||
}
|
||||
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.getCurrentPlayer();
|
||||
}catch (IndexOutOfBoundsException ignored) {}
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int ni = 0, nj = 0;
|
||||
|
@ -6,7 +6,6 @@ import othello.State;
|
||||
|
||||
public class NegamaxPlayer extends Player {
|
||||
|
||||
|
||||
public NegamaxPlayer(int depth) {
|
||||
super(depth);
|
||||
}
|
||||
@ -16,9 +15,8 @@ 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.copy();
|
||||
nextState.play(move);
|
||||
int value = -negamax(nextState, depth);
|
||||
State nextState = game.play(move);
|
||||
int value = -negamax(nextState, this.depth,Integer.MIN_VALUE,Integer.MAX_VALUE);
|
||||
if (value > bestValue) {
|
||||
bestValue = value;
|
||||
bestMove = move;
|
||||
@ -27,17 +25,29 @@ public class NegamaxPlayer extends Player {
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private Integer negamax(State game, int depth) {
|
||||
if(depth == 0 || game.isOver()) {
|
||||
return game.getScore(game.getCurrentPlayer());
|
||||
private int negamax(State state, int depth,int alpha,int beta) {
|
||||
if(depth == 0 || state.isOver()) {
|
||||
return evaluate(state);
|
||||
}
|
||||
int bestValue = Integer.MIN_VALUE;
|
||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
||||
State nextState = game.copy();
|
||||
bestValue = Math.max(bestValue,-negamax(nextState.play(move),depth-1));
|
||||
|
||||
else{
|
||||
int m = Integer.MIN_VALUE;
|
||||
for (Pair<Point, Point> move : state.getMove(state.getCurrentPlayer())) {
|
||||
State nextState = state.play(move);
|
||||
m= Math.max(m,-negamax(nextState,depth-1,alpha,beta));
|
||||
alpha = Math.max(alpha, m);
|
||||
if(alpha >= beta)
|
||||
break;
|
||||
}
|
||||
return m;
|
||||
}
|
||||
return bestValue;
|
||||
}
|
||||
private int evaluate(State game){
|
||||
Player winner = game.getWinner();
|
||||
if(winner == null)
|
||||
return 0;
|
||||
else if(winner == game.getCurrentPlayer())
|
||||
return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user