Advertisement
Guest User

MyTools4

a guest
Mar 30th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. package sXXXXXXXXX.mytools;
  2.  
  3. import boardgame.Board;
  4. import boardgame.BoardState;
  5. import boardgame.Move;
  6. import boardgame.Player;
  7.  
  8. import omweso.CCBoardState;
  9. import omweso.CCBoard;
  10. import omweso.CCMove;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.Random;
  14.  
  15. public class MyTools4 {
  16.  
  17.     public static double getSomething(){
  18.         return Math.random();
  19.     }
  20.  
  21.     private static int getTotalSeeds(int pits[]){
  22.         int num_of_seeds = 0;
  23.         for(int i = 0;i<pits.length;i++){
  24.             num_of_seeds += pits[i];
  25.         }
  26.  
  27.         return num_of_seeds;
  28.     }
  29.  
  30.     private static int value_of_board(CCBoardState board){
  31.         int pits[][] = board.getBoard();
  32.         return getTotalSeeds(pits[0]) - getTotalSeeds(pits[1]);
  33.     }
  34.  
  35.     public static void printLegalMoves(ArrayList<CCMove> moves){
  36.         for(CCMove move: moves){
  37.             System.out.print(move.getPit() + " ");
  38.         }
  39.  
  40.         System.out.println();
  41.     }
  42.  
  43.     public static CCMove returnBestMove(CCBoardState board){
  44.  
  45.         System.out.println("\nStarts HERE");
  46.         int moves_left = 100;
  47.         ArrayList<CCMove> legal_moves = board.getLegalMoves();
  48.         //System.out.println(legal_moves.toString());
  49.         CCMove best_move = legal_moves.get(0);
  50.         int best_score = 0;
  51.         int current_score = best_score;
  52.         CCBoardState tmp_board;
  53.  
  54.         for(CCMove m : legal_moves){
  55.             tmp_board = (CCBoardState)board.clone();
  56.             //          if (!board.isLegal(m))
  57.             //              return best_move;
  58.             board.move(m);
  59.             current_score = getBestMove(tmp_board, moves_left);
  60.  
  61.             if(current_score > best_score){
  62.                 best_score = current_score;
  63.                 best_move = m;
  64.             }
  65.         }
  66.  
  67.         return best_move;
  68.     }
  69.  
  70.     private static int getBestMove(CCBoardState board, int moves_left){
  71.  
  72.         if(moves_left == 0){
  73.             ArrayList<CCMove> moves = board.getLegalMoves();
  74.             if(moves.size() == 0){
  75.                 System.out.println(board.getBoard().toString());
  76.                 return -1;
  77.             }else{
  78.                 int score = 0;
  79.                 for(CCMove move : moves){
  80.                     if(score < value_of_board(board)){
  81.                         score = value_of_board(board);
  82.                     }
  83.                 }
  84.                 return score;
  85.             }
  86.         }
  87.  
  88.         ArrayList<CCMove> legal_moves = board.getLegalMoves();
  89.  
  90.         if(legal_moves.size() == 0){
  91.             return -1;
  92.         }
  93.  
  94.         printLegalMoves(legal_moves);
  95.         int best_score = 0;
  96.         int current_score = best_score;
  97.         CCBoardState tmp_board;
  98.  
  99.         for(CCMove m : legal_moves){
  100.             tmp_board = (CCBoardState)board.clone();
  101.             //          if (!board.isLegal(m))
  102.             //              return best_score;
  103.             board.move(m);
  104.             current_score = getBestMove(tmp_board,moves_left-1);
  105.  
  106.             if(current_score > best_score){
  107.                 best_score = current_score;
  108.             }
  109.         }
  110.  
  111.         return best_score;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement