Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package checkers;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.LinkedList;
  6.  
  7. public class AlphaBeta extends MinMaxPlayer {
  8.  
  9.     protected AlphaBeta(String name, int maxDepth) {
  10.         super(name, maxDepth);
  11.     }
  12.    
  13.    
  14.       @Override
  15.       public String getMove(Board game, LinkedList<String> allMoves) {
  16.         color = game.getCurrentPlayer();
  17.         float max = Float.NEGATIVE_INFINITY;
  18.         String best = null;
  19.         for (String move : allMoves) {
  20.           Board test = new Board(game);
  21.           test.playMove(move);
  22.           NB_EVAL++;
  23.           float tmp = evalMin(test, MAX_DEPTH, 0);
  24.           if (tmp > max) {
  25.             max = tmp;
  26.             best = move;
  27.           }
  28.         } // for move in allMoves
  29.         return best;
  30.       }
  31.  
  32.       private float evalMin(Board board, int depthLeft, int minimum) {
  33.         float min = Float.POSITIVE_INFINITY;
  34.         LinkedList<String> allMoves = board.getAllMoves();
  35.  
  36.         if (allMoves.isEmpty() || depthLeft==0) {
  37.           int score = board.getBalance();
  38.           if (color == Piece.White)
  39.             return score;
  40.           else
  41.             return -score;
  42.         }
  43.         for (String move : allMoves) {
  44.           Board test = new Board(board);
  45.           test.playMove(move);
  46.           NB_EVAL++;
  47.           float tmp = evalMin(test, depthLeft-1, minimum);
  48.           if(tmp< minimum){
  49.               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
  50.           }
  51.           if (tmp > min) {
  52.             min = tmp;
  53.           }
  54.         } // for move in allMoves
  55.         return min;
  56.       }
  57.  
  58.       private float evalMax(Board board, int depthLeft, int maximum) {
  59.         float max = Float.NEGATIVE_INFINITY;
  60.         LinkedList<String> allMoves = board.getAllMoves();
  61.         if (allMoves.isEmpty() || depthLeft==0) {
  62.           int score = board.getBalance();
  63.           if (color == Piece.White)
  64.             return score;
  65.           else
  66.             return -score;
  67.         }
  68.         for (String move : allMoves) {
  69.           Board test = new Board(board);
  70.           test.playMove(move);
  71.           NB_EVAL++;
  72.           float tmp = evalMin(test, depthLeft-1, maximum);
  73.           if(tmp > maximum){
  74.               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
  75.           }
  76.           if (tmp < max) {
  77.             max = tmp;
  78.           }
  79.         } // for move in allMoves
  80.         return max;
  81.       }
  82.  
  83.       @Override
  84.       public int getNbEvals() {
  85.         return NB_EVAL;
  86.       }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement