Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.29 KB | None | 0 0
  1. package facilityGame;
  2. //Ilopoisi Minimax me Alpha-Beta Pruning
  3. //logo megalu n me ton server xrisimopoiw mikro DEPTH gia na iparxei fisiologiko runtime
  4. //xrisimopoiw arketes long giati fovame sto 1500 epi to value to kathe node mipos exume kapio oveflow
  5. import java.util.Vector;
  6.  
  7. public class SteliosMouslech extends FPlayer {
  8.     static String playerName = "SteliosMouslech";
  9.     static String version = "1.0";
  10.     // Give your personal information in Greek
  11.     static int afm = 57382; // AFM should be in form 5XXXX
  12.     static String firstname = "Στυλιανος-Γεωργιος";
  13.     static String lastname = "Μουσλεχ";
  14.  
  15.     // Member variables
  16.     int n; // Number of nodes in the graph
  17.     EnumFacilityStatus[] nStatus;
  18.     int [] nValues;
  19.     Vector<Integer> lastMovesA=new Vector<Integer>();
  20.     Vector<Integer> lastMovesB=new Vector<Integer>();
  21.     int moveIndex;
  22.     int DEPTH;
  23.     // Constructor
  24.     public SteliosMouslech(EnumPlayer player) {
  25.         // Call the constructor of the parent class
  26.         super(player, playerName, version, afm, firstname, lastname);
  27.     }
  28.  
  29.     // Initialize Player
  30.     // This method is called before the game starts
  31.     // Each player can override this method
  32.    
  33.     //i sinartisi kanei mia kinisi gia ton kathe pexti
  34.     public void setMove(EnumPlayer player,int move)
  35.     {
  36.         EnumFacilityStatus nodeStatus = null;
  37.         if (player == EnumPlayer.PLAYER_A) {
  38.              lastMovesA.add(move);
  39.             nodeStatus = EnumFacilityStatus.PLAYER_A;
  40.         } else if (player == EnumPlayer.PLAYER_B) {
  41.              lastMovesB.add(move);
  42.             nodeStatus = EnumFacilityStatus.PLAYER_B;
  43.  
  44.         }
  45.        
  46.         nStatus[move]=nodeStatus;
  47.         if(move==n-1)
  48.         {
  49.         nStatus[move-1]=EnumFacilityStatus.BLOCKED;}
  50.         else if(move==0)
  51.             {nStatus[move+1]=EnumFacilityStatus.BLOCKED;}
  52.         else
  53.         {   nStatus[move+1]=EnumFacilityStatus.BLOCKED;
  54.             nStatus[move-1]=EnumFacilityStatus.BLOCKED;}
  55.        
  56.        
  57.     }
  58.    
  59.    
  60.     //pernei pisw tin kinisi
  61.     public void undoMove(EnumPlayer player,int move)
  62.     {
  63.         EnumFacilityStatus nodeStatus =EnumFacilityStatus.FREE;
  64.         if (player == EnumPlayer.PLAYER_A) {
  65.              lastMovesA.remove(lastMovesA.size()-1);
  66.         } else if (player == EnumPlayer.PLAYER_B) {
  67.              lastMovesB.remove(lastMovesB.size()-1);           
  68.         }
  69.        
  70.         nStatus[move]=nodeStatus;
  71.         //prosoxi na mim kanume unblock kati pu prepei na ine blocked meta to undo
  72.         if(move==0 ||move==1)
  73.         {
  74.             if (nStatus[move+2]==nodeStatus || nStatus[move+2]==EnumFacilityStatus.BLOCKED)
  75.             {
  76.                 if(move==0)
  77.                 {
  78.                     nStatus[move+1]=nodeStatus;
  79.                 }
  80.                 else
  81.                 {
  82.                     nStatus[move+1]=nodeStatus;
  83.                     nStatus[move-1]=nodeStatus;
  84.                 }
  85.             }
  86.         }
  87.         else if ( move==n-1 || move==n-2)
  88.         {
  89.             if (nStatus[move-2]==nodeStatus || nStatus[move-2]==EnumFacilityStatus.BLOCKED)
  90.             {  
  91.                 if(move==n-1)
  92.                 {
  93.                     nStatus[move-1]=nodeStatus;
  94.                 }
  95.                 else
  96.                 {
  97.                     nStatus[move+1]=nodeStatus;
  98.                     nStatus[move-1]=nodeStatus;
  99.                 }
  100.  
  101.             }
  102.         }
  103.         else
  104.         {
  105.             if (nStatus[move+2]==nodeStatus || nStatus[move+2]==EnumFacilityStatus.BLOCKED)
  106.             {
  107.                 nStatus[move+1]=nodeStatus;
  108.             }
  109.             if(nStatus[move-2]==nodeStatus || nStatus[move-2]==EnumFacilityStatus.BLOCKED)
  110.             {
  111.                 nStatus[move-1]=nodeStatus;
  112.             }
  113.         }
  114.            
  115.     }
  116.    
  117.     public void initialize(FacilityGameAPI game) {
  118.         n = game.getN();
  119.         nStatus= new EnumFacilityStatus[n];
  120.         nValues=new int[n];
  121.         moveIndex=-1;
  122.         nStatus=game.getStatusCopy();
  123.         nValues=game.getValueCopy();
  124.         DEPTH=2;
  125.        
  126.     }
  127.     //i sinartisi axiologisis tu board ine i diafora ton skor etsi o player A ine o maximizer kai o player b o minimizer
  128.     public long evalBoard(){
  129.         long a=(long)scoreCalc(EnumPlayer.PLAYER_A);
  130.         long b=(long)scoreCalc(EnumPlayer.PLAYER_B);
  131.         long eval=0;
  132.         eval=a-b;
  133.  
  134.         return eval;
  135.     }
  136.    
  137.     //sinartisi ipologismu tu score ine san aftin pu exete kanei gia ton server
  138.    
  139.     public int scoreCalc(EnumPlayer player){
  140.         int score = 0;
  141.  
  142.         EnumFacilityStatus nodeStatus = null;
  143.         if (player == EnumPlayer.PLAYER_A) {
  144.             nodeStatus = EnumFacilityStatus.PLAYER_A;
  145.         } else if (player == EnumPlayer.PLAYER_B) {
  146.             nodeStatus = EnumFacilityStatus.PLAYER_B;
  147.         }
  148.  
  149.         int tempBonus = 0;
  150.         int numOfConsecutiveFacilities = 0;
  151.  
  152.         int node = 0;
  153.         while (node < n) {
  154.             if (nStatus[node] == nodeStatus) {
  155.                 int nodeValue = nValues[node];
  156.                 score += nodeValue;
  157.  
  158.                 // bonus
  159.                 tempBonus += 3* nodeValue;
  160.                 numOfConsecutiveFacilities++;
  161.                 node += 1;
  162.             } else if (nStatus[node] == EnumFacilityStatus.BLOCKED) {
  163.                 node += 1;
  164.             } else {
  165.                 if (numOfConsecutiveFacilities >= 3) {
  166.                     score += tempBonus;
  167.                 }
  168.                 tempBonus = 0;
  169.                 numOfConsecutiveFacilities = 0;
  170.                 node += 1;
  171.             }
  172.         }
  173.         if (numOfConsecutiveFacilities >= 3) {
  174.             score += tempBonus;
  175.         }
  176.  
  177.         return score;
  178.     }
  179.     //elexos an isxii to switch mode
  180.    
  181.     public boolean switchModeExists(EnumPlayer player)
  182.     {
  183.         boolean exist=false;
  184.         if (player==EnumPlayer.PLAYER_A)
  185.         {
  186.             if (lastMovesA.size()==0)
  187.             {
  188.                 return false;
  189.             }
  190.             else
  191.             {
  192.                 for (int i=0; i<n; i++)
  193.                 {
  194.                     if(Math.abs(i-lastMovesA.get(lastMovesA.size()-1))>3 && nStatus[i]==EnumFacilityStatus.FREE)
  195.                     {
  196.                         return true;
  197.                     }
  198.                 }
  199.                
  200.             }
  201.         }
  202.         else{
  203.             if (lastMovesB.size()==0)
  204.             {
  205.                 return false;
  206.             }
  207.             else
  208.             {
  209.                 for (int i=0; i<n; i++)
  210.                 {
  211.                     if(Math.abs(i-lastMovesB.get(lastMovesB.size()-1))>3 && nStatus[i]==EnumFacilityStatus.FREE)
  212.                     {
  213.                         return true;
  214.                     }
  215.                 }
  216.                
  217.             }
  218.         }
  219.         return exist;
  220.     }
  221.    
  222.     //i minimax epistrefei tin timi tis kaliteris kinisis apo to evluation egw thelw to index tis kinisis aftis
  223.     //enas alos tropos pio orthologikos itan na epistrefei adikimeno toso me tin thesi oso kai tin timi
  224.     //logo xronu protiimithike an exw mia metavliti melos moveIndex kai na allazw ekei otan prepei tin timi tis
  225.     public long  minimax(int depth,long alpha,long beta, boolean isMaxPlayer)
  226.     {
  227.         long bestMove=-1;
  228.         long nextMove;
  229.         if(depth==0)
  230.         {
  231.             return evalBoard();
  232.         }
  233.    
  234.         else if(isMaxPlayer)   //MAXIMIZER
  235.         {
  236.              bestMove=-999999999;
  237.             if (switchModeExists(EnumPlayer.PLAYER_A)==false )
  238.             {
  239.                 for (int i=0; i<n; i++)
  240.                 {
  241.                     if(nStatus[i]==EnumFacilityStatus.FREE)
  242.                     {
  243.                         setMove(EnumPlayer.PLAYER_A,i);
  244.                         nextMove=minimax(depth-1,alpha,beta,false);
  245.                         if (nextMove>=bestMove && depth==DEPTH)
  246.                         {moveIndex=i;}
  247.                         bestMove=Math.max(bestMove,nextMove);
  248.                         undoMove(EnumPlayer.PLAYER_A,i);
  249.                         alpha=Math.max(bestMove, alpha);
  250.                         if(beta <= alpha) {
  251.                             return bestMove;
  252.  
  253.                         }
  254.  
  255.                     }
  256.                 }
  257.                 //etsi elexw an ftasei se simio pu den exei ales kinisis
  258.             if (bestMove==-999999999)
  259.             {
  260.                 return evalBoard();
  261.             }
  262.             }
  263.             else
  264.             {
  265.                 for (int i=0; i<n; i++)
  266.                 {
  267.                     if (Math.abs(i-lastMovesA.lastElement())>3)
  268.                     {
  269.                         if(nStatus[i]==EnumFacilityStatus.FREE)
  270.                         {
  271.                             setMove(EnumPlayer.PLAYER_A,i);
  272.                             nextMove=minimax(depth-1,alpha,beta,false);
  273.                             if (nextMove>=bestMove && depth==DEPTH)
  274.                             {moveIndex=i;}
  275.                             bestMove=Math.max(bestMove,nextMove);
  276.                             undoMove(EnumPlayer.PLAYER_A,i);
  277.                             alpha=Math.max(bestMove, alpha);
  278.                             if(beta <= alpha) {
  279.                                 return bestMove;
  280.  
  281.                             }
  282.  
  283.                         }
  284.                     }
  285.                 }
  286.                 if (bestMove==-999999999)
  287.                 {
  288.                     return evalBoard();
  289.                 }
  290.             }
  291.         }
  292.         else  //MINIMIZER
  293.         {
  294.                  bestMove=999999999;
  295.                 if (switchModeExists(EnumPlayer.PLAYER_B)==false )
  296.                 {
  297.                     for (int i=0; i<n; i++)
  298.                     {
  299.                         if(nStatus[i]==EnumFacilityStatus.FREE)
  300.                         {
  301.                             setMove(EnumPlayer.PLAYER_B,i);
  302.                             nextMove=minimax(depth-1,alpha,beta,true);
  303.                             if (nextMove<=bestMove && depth==DEPTH)
  304.                             {moveIndex=i;}
  305.                             bestMove=Math.min(bestMove,nextMove);
  306.                             undoMove(EnumPlayer.PLAYER_B,i);
  307.                            
  308.                             beta=Math.min(bestMove, beta);
  309.                             if(beta <= alpha) {
  310.                                 return bestMove;
  311.                             }
  312.  
  313.  
  314.                         }
  315.                     }
  316.                     if (bestMove==999999999)
  317.                     {
  318.                         return evalBoard();
  319.                     }
  320.                
  321.                 }else
  322.                 {
  323.                     for (int i=0; i<n; i++)
  324.                     {
  325.                         if (Math.abs(i-lastMovesB.lastElement())>3)
  326.                         {
  327.                             if(nStatus[i]==EnumFacilityStatus.FREE)
  328.                             {
  329.                                 setMove(EnumPlayer.PLAYER_B,i);
  330.                                 nextMove=minimax(depth-1,alpha,beta,true);
  331.                                 if (nextMove<=bestMove && depth==DEPTH)
  332.                                 {moveIndex=i;}
  333.                                 bestMove=Math.min(bestMove,nextMove);
  334.                                 undoMove(EnumPlayer.PLAYER_B,i);
  335.                                 beta=Math.min(bestMove, beta);
  336.                                 if(beta <= alpha) {
  337.                                     return bestMove;
  338.                                 }
  339.  
  340.                             }
  341.                         }
  342.                     }
  343.                     if (bestMove==999999999)
  344.                     {
  345.                         return evalBoard();
  346.                     }
  347.                 }
  348.             }
  349.         return bestMove;
  350.         }
  351.        
  352.    
  353.        
  354.    
  355.  
  356.            
  357.                    
  358.    
  359.     public int nextMove(FacilityGameAPI game) {
  360.         nStatus=game.getStatusCopy();
  361.         int move=-1; //den xrisimopoiite
  362.         int numberOfMoves=game.getCurMoveIndex();
  363.         Vector<Integer> moveLocs=game.getMoveLocation();
  364.         long alpha=-999999999;
  365.         long beta=  999999999;
  366.         if (whoAmI()==(EnumPlayer.PLAYER_A))
  367.         {
  368.            
  369.             if (numberOfMoves!=0)
  370.             {
  371.                 lastMovesB.clear();
  372.                 lastMovesB.add(moveLocs.get(numberOfMoves-1));
  373.             }
  374.            
  375.             move=(int)minimax(DEPTH,alpha,beta,true);
  376.             lastMovesA.clear();
  377.             lastMovesA.add(moveIndex);
  378.         }
  379.         else
  380.         {
  381.                 lastMovesA.clear();
  382.                 lastMovesA.add(moveLocs.get(numberOfMoves-1));             
  383.                 move=(int)minimax(DEPTH,alpha,beta,false);
  384.                 lastMovesB.clear();
  385.                 lastMovesB.add(moveIndex);
  386.         }
  387.        
  388.         return moveIndex;
  389.     }
  390.    
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement