AlphaBetaPlayer Implemented and score fixed.
This commit is contained in:
parent
57c31435f0
commit
ecf642de67
@ -1,14 +1,14 @@
|
||||
package othello;
|
||||
|
||||
import othello.players.NegamaxPlayer;
|
||||
import othello.players.AlphaBetaPlayer;
|
||||
import othello.players.Player;
|
||||
|
||||
public class Main {
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Player p1 = new NegamaxPlayer(3);
|
||||
Player p2 = new NegamaxPlayer(2);
|
||||
Player p1 = new AlphaBetaPlayer(5);
|
||||
Player p2 = new AlphaBetaPlayer(5);
|
||||
Player[][] board = initialize(p1, p2);
|
||||
State game = new State(board, p1, p2);
|
||||
System.out.println("joueur 1: " + p1);
|
||||
@ -19,9 +19,10 @@ public class Main {
|
||||
game = game.play(player.play(game));
|
||||
}
|
||||
System.out.println(game.toString());
|
||||
System.out.println(game.getN1()+" "+ game.getN2());
|
||||
System.out.println(game.getWinner() + " a gagné la partie");
|
||||
System.out.println(game.getScore(p1));
|
||||
System.out.println(game.getScore(p2));
|
||||
System.out.println("Score joueur 1 -> " + game.getN1());
|
||||
System.out.println("Score joueur 2 -> "+ game.getN2());
|
||||
|
||||
}
|
||||
|
||||
|
@ -61,12 +61,17 @@ public class State {
|
||||
public int getScore(Player player) {
|
||||
return player == player1 ? (n1/(n1+n2)) : (n2/(n1+n2));
|
||||
}
|
||||
public int getN1(){
|
||||
return this.n1;
|
||||
}
|
||||
public int getN2(){
|
||||
return this.n2;
|
||||
}
|
||||
|
||||
public Player getWinner() {
|
||||
int scoreP1 = getScore(player1), scoreP2 = getScore(player2);
|
||||
if(scoreP1 > scoreP2)
|
||||
if(this.n1 > this.n2)
|
||||
return player1;
|
||||
else if(scoreP2 > scoreP1)
|
||||
else if(this.n2 > this.n1)
|
||||
return player2;
|
||||
return null;
|
||||
}
|
||||
|
50
src/othello/players/AlphaBetaPlayer.java
Normal file
50
src/othello/players/AlphaBetaPlayer.java
Normal file
@ -0,0 +1,50 @@
|
||||
package othello.players;
|
||||
|
||||
import othello.Pair;
|
||||
import othello.Point;
|
||||
import othello.State;
|
||||
|
||||
public class AlphaBetaPlayer extends Player{
|
||||
|
||||
public AlphaBetaPlayer(int depth) {
|
||||
super(depth);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Point, Point> play(State game) {
|
||||
int bestValue = Integer.MIN_VALUE;
|
||||
Pair<Point, Point> bestMove = null;
|
||||
for(Pair<Point, Point> move : game.getMove(game.getCurrentPlayer())) {
|
||||
State nextState = game.play(move);
|
||||
int value = -alphabeta(nextState, this.depth,Integer.MIN_VALUE,Integer.MAX_VALUE);
|
||||
if (value > bestValue) {
|
||||
bestValue = value;
|
||||
bestMove = move;
|
||||
}
|
||||
}
|
||||
return bestMove;
|
||||
}
|
||||
|
||||
private int alphabeta(State state, int depth,int alpha,int beta) {
|
||||
if(depth == 0 || state.isOver()) {
|
||||
return evaluate(state);
|
||||
}
|
||||
else{
|
||||
for (Pair<Point, Point> move : state.getMove(state.getCurrentPlayer())) {
|
||||
State nextState = state.play(move);
|
||||
alpha = Math.max(alpha,-alphabeta(nextState,depth-1,-beta,-alpha));
|
||||
if(alpha >= beta)
|
||||
return alpha;
|
||||
}
|
||||
return alpha;
|
||||
}
|
||||
}
|
||||
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