Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package checkers;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.LinkedList;
- public class AlphaBeta extends MinMaxPlayer {
- protected AlphaBeta(String name, int maxDepth) {
- super(name, maxDepth);
- }
- @Override
- public String getMove(Board game, LinkedList<String> allMoves) {
- color = game.getCurrentPlayer();
- float max = Float.NEGATIVE_INFINITY;
- String best = null;
- for (String move : allMoves) {
- Board test = new Board(game);
- test.playMove(move);
- NB_EVAL++;
- float tmp = evalMin(test, MAX_DEPTH, 0);
- if (tmp > max) {
- max = tmp;
- best = move;
- }
- } // for move in allMoves
- return best;
- }
- private float evalMin(Board board, int depthLeft, int minimum) {
- float min = Float.POSITIVE_INFINITY;
- LinkedList<String> allMoves = board.getAllMoves();
- if (allMoves.isEmpty() || depthLeft==0) {
- int score = board.getBalance();
- if (color == Piece.White)
- return score;
- else
- return -score;
- }
- for (String move : allMoves) {
- Board test = new Board(board);
- test.playMove(move);
- NB_EVAL++;
- float tmp = evalMin(test, depthLeft-1, minimum);
- if(tmp< minimum){
- return tmp; //Si on trouve une valeur plus petite que celle deja évalué, on la retourne et on ne regarde pas les noeuds frères
- }
- if (tmp > min) {
- min = tmp;
- }
- } // for move in allMoves
- return min;
- }
- private float evalMax(Board board, int depthLeft, int maximum) {
- float max = Float.NEGATIVE_INFINITY;
- LinkedList<String> allMoves = board.getAllMoves();
- if (allMoves.isEmpty() || depthLeft==0) {
- int score = board.getBalance();
- if (color == Piece.White)
- return score;
- else
- return -score;
- }
- for (String move : allMoves) {
- Board test = new Board(board);
- test.playMove(move);
- NB_EVAL++;
- float tmp = evalMin(test, depthLeft-1, maximum);
- if(tmp > maximum){
- return tmp; //Si on trouve une valeur plus grande que celle deja évalué, on la retourne et on ne regarde pas les noeuds frères
- }
- if (tmp < max) {
- max = tmp;
- }
- } // for move in allMoves
- return max;
- }
- @Override
- public int getNbEvals() {
- return NB_EVAL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement