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