Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. public static int MAX_VALUE(int depth,boardState gameBoard,String typeOfHeurestic,int parentAlpha,int parentBeta){
  2.  
  3.         int currentAlpha = parentAlpha;
  4.         int currentBeta = parentBeta;
  5.  
  6.         if(depth==TERMINAL_DEPTH){
  7.             gameBoard.calculateHeuresticValue(currentPlayer,typeOfHeurestic);
  8.             int retVal = gameBoard.getHeuresticValue();
  9.             return retVal;
  10.         }
  11.  
  12.         boolean endGame = endGameCondition(gameBoard);
  13.  
  14.         if(endGame){
  15.             gameBoard.calculateHeuresticValue(currentPlayer,typeOfHeurestic);
  16.             int retVal = gameBoard.getHeuresticValue();
  17.             return retVal;
  18.         }
  19.  
  20.         ArrayList<int[]> movesList = gameBoard.getSetOfValidMoves(currentPlayer);
  21.  
  22.         int v = Integer.MIN_VALUE;
  23.  
  24.         for(int i=0;i<movesList.size();i++){
  25.             int[] element = movesList.get(i);
  26.             boardState newBoard = new boardState(8,gameBoard.copyBoard());
  27.             newBoard.placeCoin(element[0],element[1],currentPlayer);
  28.             int valueRecursion = MIN_VALUE(depth+1,newBoard,typeOfHeurestic,currentAlpha,currentBeta);
  29.             v= Integer.max(v,valueRecursion);
  30.             if(v>=currentBeta)return v;
  31.             currentAlpha = Integer.max(currentAlpha,v);
  32.         }
  33.  
  34.         return v;
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.     }
  42.  
  43.     public static int MIN_VALUE(int depth,boardState gameBoard,String typeOfHeurestic,int parentAlpha,int parentBeta){
  44.  
  45.         int currentAlpha = parentAlpha;
  46.         int currentBeta = parentBeta;
  47.  
  48.  
  49.         if(depth==TERMINAL_DEPTH){
  50.             gameBoard.calculateHeuresticValue(currentPlayer,typeOfHeurestic);
  51.             int retVal = gameBoard.getHeuresticValue();
  52.             return retVal;
  53.         }
  54.  
  55.         boolean endGame = endGameCondition(gameBoard);
  56.  
  57.         if(endGame){
  58.             gameBoard.calculateHeuresticValue(currentPlayer,typeOfHeurestic);
  59.             int retVal = gameBoard.getHeuresticValue();
  60.             return retVal;
  61.         }
  62.  
  63.         ArrayList<int[]> movesList = gameBoard.getSetOfValidMoves(oppositePlayer);
  64.  
  65.         int v = Integer.MAX_VALUE;
  66.  
  67.         for(int i=0;i<movesList.size();i++){
  68.             int[] element = movesList.get(i);
  69.             boardState newBoard = new boardState(8,gameBoard.copyBoard());
  70.             newBoard.placeCoin(element[0],element[1],oppositePlayer);
  71.             int valueRecursion = MAX_VALUE(depth+1,newBoard,typeOfHeurestic,currentAlpha,currentBeta);
  72.             v= Integer.min(v,valueRecursion);
  73.             if(v<=currentAlpha)return v;
  74.             currentBeta = Integer.min(currentBeta,v);
  75.         }
  76.  
  77.         return v;
  78.  
  79.     }
  80.  
  81.  
  82.     public static int[] miniMaxAlgorithm(int depth,boardState gameBoard,String typeOfHeurestic){
  83.  
  84.         int rootAlpha = Integer.MIN_VALUE;
  85.         int rootBeta = Integer.MAX_VALUE;
  86.  
  87.         ArrayList<int[]> movesList = gameBoard.getSetOfValidMoves(currentPlayer);
  88.         int v = Integer.MIN_VALUE;
  89.         int bestMoveIndex = 0;
  90.  
  91.  
  92.         for(int i=0;i<movesList.size();i++){
  93.             int[] element = movesList.get(i);
  94.             boardState newBoard = new boardState(8,gameBoard.copyBoard());
  95.             newBoard.placeCoin(element[0],element[1],currentPlayer);
  96.             int retVal = MIN_VALUE(depth+1,newBoard,typeOfHeurestic,rootAlpha,rootBeta);
  97.             v = Integer.max(v,retVal);
  98.             if(v>=rootAlpha){
  99.                 rootAlpha=v;
  100.                 bestMoveIndex=i;
  101.             }
  102.         }
  103.         return movesList.get(bestMoveIndex);
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement