makispaiktis

MinMax

Dec 10th, 2018 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class MinMaxPlayer extends HeuristicPlayer {
  4.     //BinaryTree <Node> tree = new BinaryTree <Node> ();
  5.     int nodeDepth;
  6.    
  7.     // Constructors
  8.     public MinMaxPlayer(int playerId, String name, int score, Board templateBoard, ArrayList<Integer[]> templatePath) {
  9.         super(playerId, name, score, templateBoard, templatePath);
  10.         nodeDepth = 4;
  11.     }
  12.    
  13.    
  14.     public double evaluate(int currentPos, int dice, int opponentPosition) {
  15.         int pointsBefore = score;
  16.         int finalPos = heuristicMove(currentPos, dice);
  17.         double weight;
  18.         /*if(opponentPos > currentPos) {
  19.             weight = (finalPos - currentPos) * 0.65 + (score - pointsBefore) * 0.35;
  20.         }*/
  21.        
  22.    
  23.         //weight = (finalPos - currentPos) * 0.3 + (score - pointsBefore) * 0.5 + (currentPos - opponentPosition) * 0.2;
  24.         weight = (finalPos - currentPos) * 0.65 + (score - pointsBefore) * 0.35;
  25.        
  26.         score = pointsBefore;
  27.         return weight;
  28.     }
  29.    
  30.     public void createMySubTree(Node parent, int depth, int currentPos, int opponentCurrentPos) {
  31.         int movements = 6;
  32.         double movesWeight[] = new double[6];
  33.         //double opponentMovesWeight[] = new double[6];
  34.         double max = -1000000;
  35.         double min = 1000000;
  36.         //double opponentMax = 0;
  37.         //double opponentMin = 0;
  38.        
  39.         int maxIndex = 0;
  40.         int minIndex = 0;
  41.        
  42.         for(int i = 0 ; i < movements; i++) {
  43.             movesWeight[i] = evaluate(currentPos, i + 1, opponentCurrentPos);
  44.             //System.out.println(movesWeight[i]);
  45.             if(movesWeight[i] > max) {
  46.                 max = movesWeight[i];
  47.                 maxIndex = i;
  48.                 //System.out.println("mphka!");
  49.                
  50.             }
  51.             if(movesWeight[i] < min) {
  52.                 min = movesWeight[i];
  53.                 minIndex = i;
  54.             }
  55.         }
  56.         Node lChild = new Node(parent, depth, parent.getBoard(), min, minIndex + 1);
  57.         Node rChild = new Node(parent, depth, parent.getBoard(), max, maxIndex + 1);
  58.         parent.children.add(lChild);
  59.         parent.children.add(rChild);
  60.        
  61.        
  62.        
  63.         /*// Create the children of lChild and rChild
  64.         for(int i=0; i<movements; i++) {
  65.             opponentMovesWeight[i] = evaluate(opponentCurrentPos, i+1);
  66.             if(opponentMovesWeight[i] > opponentMax) {
  67.                 opponentMax = opponentMovesWeight[i];
  68.             }
  69.            
  70.             if(opponentMovesWeight[i] < opponentMin) {
  71.                 opponentMin = opponentMovesWeight[i];
  72.             }
  73.         }
  74.        
  75.         Node lrChild = new Node(rChild, depth+1, rChild.getBoard(), opponentMin);
  76.         Node rrChild = new Node(rChild, depth+1, rChild.getBoard(), opponentMax);
  77.         rChild.children.add(lrChild);
  78.         rChild.children.add(rrChild);*/
  79.     }
  80.    
  81.     public void createOpponentSubTree(Node parent, int depth, int currentPos, int opponentCurrentPos, double parentEval, Player opponent) {
  82.         int movements = 6;
  83.         double movesWeight[] = new double[6];
  84.         double max = -100000;
  85.         double min = 100000;
  86.        
  87.         for(int i=0; i<movements; i++) {
  88.             movesWeight[i] = opponent.evaluate(opponentCurrentPos, i+1);
  89.             if(movesWeight[i] > max) {
  90.                 max = movesWeight[i];
  91.             }
  92.            
  93.             if(movesWeight[i] < min) {
  94.                 min = movesWeight[i];
  95.             }
  96.         }
  97.        
  98.         Node lChild = new Node(parent, depth, parent.getBoard(), min);
  99.         Node rChild = new Node(parent, depth, parent.getBoard(), max);
  100.         parent.children.add(lChild);
  101.         parent.children.add(rChild);
  102.     }
  103.    
  104.     public int chooseMinMaxMove(Node root) {
  105.         int indexI = 0;
  106.         //int indexJ = 0;
  107.         //int indexK = 0;
  108.         double max = root.children.get(0).children.get(0).children.get(0).getNodeEvaluation();
  109.         for(int i = 0; i < 2; i++) {
  110.             for(int j = 0; j < 2; j++) {
  111.                 for(int k = 0; k < 2; k++) {
  112.                     System.out.println(root.children.get(i).children.get(j).children.get(k).getNodeEvaluation());
  113.                     System.out.println(max);
  114.                     System.out.println(root.children.get(i).children.get(j).children.get(k).getNodeEvaluation() > max);
  115.                     if(root.children.get(i).children.get(j).children.get(k).getNodeEvaluation() >= max) {
  116.                         indexI = i;
  117.                         //indexJ = j;
  118.                         //indexK = k;
  119.                         max = root.children.get(i).children.get(j).children.get(k).getNodeEvaluation();
  120.                     }
  121.                 }
  122.             }
  123.         }
  124.         System.out.println("IndexI: " + indexI);
  125.         return indexI;
  126.     }
  127.    
  128.     public int getNextMinMaxMove(int currentPos, int opponentCurrentPos, Player opponent) {
  129.         int movements = 6;
  130.         double movesWeight[] = new double[6];
  131.         double max = 0;
  132.        
  133.        
  134.         for(int i=0; i<movements; i++) {
  135.             movesWeight[i] = opponent.evaluate(opponentCurrentPos, i+1);
  136.             if(movesWeight[i] > max) {
  137.                 max = movesWeight[i];
  138.             }
  139.         }
  140.        
  141.         // 0
  142.         Node root = new Node(0, board, max);
  143.        
  144.         // 1
  145.         createMySubTree(root, 1, playerTileId, opponent.getPlayerTileId());
  146.         // 2
  147.         for(int i = 0; i < 2; i++) {
  148.             createOpponentSubTree(root.children.get(i), 2, currentPos, opponentCurrentPos, root.children.get(i).getNodeEvaluation(), opponent);
  149.         }
  150.         // 3
  151.         for(int i = 0; i < 2; i++) {
  152.             for(int j = 0; j < 2; j++) {
  153.                 createMySubTree(root.children.get(i).children.get(j), 3, currentPos, opponentCurrentPos);
  154.             }
  155.         }
  156.        
  157.         int moveI = chooseMinMaxMove(root);
  158.         int perfectDice = root.children.get(moveI).getDice();
  159.        
  160.         //Appropriate typecasting from int[] to Integer[]
  161.         Integer temp[] = new Integer[5];
  162.         int tempMove[] = move(currentPos, perfectDice);
  163.         for(int i = 0; i < temp.length; i++) {
  164.             temp[i] = tempMove[i];
  165.         }
  166.         path.add(temp);
  167.         return tempMove[0];
  168.         //return path.get(path.size() - 1)[0];
  169.        
  170.     }
  171.        
  172.    
  173. }
Add Comment
Please, Sign In to add comment