Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package sXXXXXXXXX.mytools;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import omweso.CCBoardState;
  6. import omweso.CCMove;
  7.  
  8. public class MyTools {
  9.    
  10.     public static final int OP_GT_16 = 0;
  11.     public static final int WIN = 1000;
  12.     public static final int OP_LT_16 = 100;
  13.     public static final int CAPTURE = 50;
  14.     public static final int LOSS = -10;
  15.     public static final int SEEDS_LT_16 = -100;
  16.     public static final int NONE = 1;
  17.  
  18.     public static double getSomething(){
  19.         return Math.random();
  20.     }
  21.    
  22.     public static int calculateHeuristic(int[] myseeds_initial, int[] myseeds_final, CCBoardState bs, int play){
  23.        
  24.         int heuristic = 0;
  25.        
  26.         int[][] fin = bs.getBoard();
  27.         int[] myf;
  28.         int[] opf;
  29.        
  30.         if(play == 0){
  31.             myf = fin[0];
  32.             opf = fin[1];
  33.         }
  34.         else{
  35.             myf = fin[1];
  36.             opf = fin[0];
  37.         }
  38.        
  39.         int[] myi = myseeds_initial;
  40.         int[] opi = myseeds_final;
  41.        
  42.         int my_init = 0;
  43.         for(int i : myi){
  44.             my_init += i;
  45.         }
  46.        
  47.         int op_init = 0;
  48.         for(int i : opi){
  49.             op_init += i;
  50.         }
  51.        
  52.         int my_final = 0;
  53.         for(int i : myf){
  54.             my_final += i;
  55.         }
  56.        
  57.         int op_final = 0;
  58.         for(int i : opf){
  59.             op_final += i;
  60.         }
  61.        
  62.         if(op_final < 16){
  63.             heuristic = OP_LT_16;
  64.         }
  65.         else if(my_final < 16){
  66.             heuristic = SEEDS_LT_16;
  67.         }
  68.         else if(my_final - my_init < 0){
  69.             heuristic = LOSS;
  70.         }
  71.         else if(op_final - op_init < 0){
  72.             heuristic = CAPTURE;
  73.         }
  74.         else if(op_final > 16){
  75.             heuristic = OP_GT_16;
  76.         }
  77.        
  78.         return heuristic;
  79.     }
  80.    
  81. /*    public static int minimax(int level, int play, int[][] init_state, CCBoardState bs, CCBoardState moved){      // 0 -> computer, 1 -> opponent
  82.        
  83.         int best_node = 0;
  84.         int curr_score = 0;
  85.         int bestScore = (player == 0) ? Integer.MIN_VALUE : Integer.MAX_VALUE;
  86.        
  87.         ArrayList<CCMove> remaining_moves = bs.getLegalMoves();
  88.         if(level == 0 || remaining_moves.isEmpty()){
  89.             return calculate_heuristic(bs, moved);
  90.         }
  91.        
  92.         if(play == 0){
  93.             ArrayList<CCMove> moves = bs.getLegalMoves();
  94.             for(CCMove m : moves){
  95.                 CCBoardState s = (CCBoardState) bs.clone();
  96.                 curr_score = minimax(level - 1, 1, bs, moved);
  97.                 if(curr_score > best_score){
  98.                     best_node = m.getPit();
  99.                 }
  100.             }
  101.         }
  102.         else{
  103.             ArrayList<CCMove> moves = bs.getLegalMoves();
  104.             for(CCMove m : moves){
  105.                 CCBoardState s = (CCBoardState) bs.clone();
  106.                 curr_score = minimax(level - 1, 0, bs, moved);
  107.                 if(curr_score < best_score){
  108.                     best_node = m.getPit();
  109.                 }
  110.             }
  111.         }
  112.        
  113.         return best_node;
  114.     }*/
  115.    
  116.    
  117.     public static int[] minimax(int level, int play, CCBoardState bs, int[] myseeds_initial, int[] opseeds_initial){
  118.        
  119.         int best_node = 0;
  120.         int curr_score = 0;
  121.         int best_score = (play == 0) ? Integer.MIN_VALUE : Integer.MAX_VALUE;
  122.        
  123.         ArrayList<CCMove> remaining_moves = bs.getLegalMoves();
  124.         if(level == 0 || remaining_moves.isEmpty()){
  125.             best_score = calculateHeuristic(myseeds_initial, opseeds_initial, bs, play);
  126.         }
  127.         else{
  128.             if(play == 0){
  129.                 ArrayList<CCMove> moves = bs.getLegalMoves();
  130.  
  131.                 for(int i = 0; i < moves.size(); i++){
  132.                     CCMove m = moves.get(i);
  133.                     int[][] state = bs.getBoard();
  134.                     CCBoardState clone = (CCBoardState) bs.clone();
  135.                     clone.move(m);
  136.                     if(bs.haveWon()){
  137.                         best_node = i;
  138.                         break;
  139.                     }
  140.                     //int next = (play == 0) ? 0 : 1;
  141.                     curr_score = minimax(level - 1, 1, clone, state[0], state[1])[0];
  142.  
  143.                     if(curr_score > best_score){
  144.                         best_score = curr_score;
  145.                         best_node = i;
  146.                     }
  147.                 }
  148.             }
  149.             else{
  150.                 ArrayList<CCMove> moves = bs.getLegalMoves();
  151.  
  152.                 for(int i = 0; i < moves.size(); i++){
  153.                     CCMove m = moves.get(i);
  154.                     int[][] state = bs.getBoard();
  155.                     CCBoardState clone = (CCBoardState) bs.clone();
  156.                     clone.move(m);
  157.                     if(bs.haveLost()){
  158.                         best_node = i;
  159.                         break;
  160.                     }
  161.                     //int next = (play == 0) ? 0 : 1;
  162.                     curr_score = minimax(level - 1, 0, clone, state[1], state[0])[0];
  163.  
  164.                     if(curr_score < best_score){
  165.                         best_score = curr_score;
  166.                         best_node = i;
  167.                     }
  168.                 }
  169.             }
  170.         }
  171.                
  172.         return new int[] {best_score, best_node};
  173.     }
  174.    
  175.     public static void printMoveList(ArrayList<CCMove> m){
  176.        
  177.         System.out.print("List of legal moves: " );
  178.         for(CCMove e : m){
  179.             System.out.print(e.getPit() + " ");
  180.         }
  181.         System.out.println();
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement