Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package facilityGame;
- //Ilopoisi Minimax me Alpha-Beta Pruning
- //logo megalu n me ton server xrisimopoiw mikro DEPTH gia na iparxei fisiologiko runtime
- //xrisimopoiw arketes long giati fovame sto 1500 epi to value to kathe node mipos exume kapio oveflow
- import java.util.Vector;
- public class SteliosMouslech extends FPlayer {
- static String playerName = "SteliosMouslech";
- static String version = "1.0";
- // Give your personal information in Greek
- static int afm = 57382; // AFM should be in form 5XXXX
- static String firstname = "Στυλιανος-Γεωργιος";
- static String lastname = "Μουσλεχ";
- // Member variables
- int n; // Number of nodes in the graph
- EnumFacilityStatus[] nStatus;
- int [] nValues;
- Vector<Integer> lastMovesA=new Vector<Integer>();
- Vector<Integer> lastMovesB=new Vector<Integer>();
- int moveIndex;
- int DEPTH;
- // Constructor
- public SteliosMouslech(EnumPlayer player) {
- // Call the constructor of the parent class
- super(player, playerName, version, afm, firstname, lastname);
- }
- // Initialize Player
- // This method is called before the game starts
- // Each player can override this method
- //i sinartisi kanei mia kinisi gia ton kathe pexti
- public void setMove(EnumPlayer player,int move)
- {
- EnumFacilityStatus nodeStatus = null;
- if (player == EnumPlayer.PLAYER_A) {
- lastMovesA.add(move);
- nodeStatus = EnumFacilityStatus.PLAYER_A;
- } else if (player == EnumPlayer.PLAYER_B) {
- lastMovesB.add(move);
- nodeStatus = EnumFacilityStatus.PLAYER_B;
- }
- nStatus[move]=nodeStatus;
- if(move==n-1)
- {
- nStatus[move-1]=EnumFacilityStatus.BLOCKED;}
- else if(move==0)
- {nStatus[move+1]=EnumFacilityStatus.BLOCKED;}
- else
- { nStatus[move+1]=EnumFacilityStatus.BLOCKED;
- nStatus[move-1]=EnumFacilityStatus.BLOCKED;}
- }
- //pernei pisw tin kinisi
- public void undoMove(EnumPlayer player,int move)
- {
- EnumFacilityStatus nodeStatus =EnumFacilityStatus.FREE;
- if (player == EnumPlayer.PLAYER_A) {
- lastMovesA.remove(lastMovesA.size()-1);
- } else if (player == EnumPlayer.PLAYER_B) {
- lastMovesB.remove(lastMovesB.size()-1);
- }
- nStatus[move]=nodeStatus;
- //prosoxi na mim kanume unblock kati pu prepei na ine blocked meta to undo
- if(move==0 ||move==1)
- {
- if (nStatus[move+2]==nodeStatus || nStatus[move+2]==EnumFacilityStatus.BLOCKED)
- {
- if(move==0)
- {
- nStatus[move+1]=nodeStatus;
- }
- else
- {
- nStatus[move+1]=nodeStatus;
- nStatus[move-1]=nodeStatus;
- }
- }
- }
- else if ( move==n-1 || move==n-2)
- {
- if (nStatus[move-2]==nodeStatus || nStatus[move-2]==EnumFacilityStatus.BLOCKED)
- {
- if(move==n-1)
- {
- nStatus[move-1]=nodeStatus;
- }
- else
- {
- nStatus[move+1]=nodeStatus;
- nStatus[move-1]=nodeStatus;
- }
- }
- }
- else
- {
- if (nStatus[move+2]==nodeStatus || nStatus[move+2]==EnumFacilityStatus.BLOCKED)
- {
- nStatus[move+1]=nodeStatus;
- }
- if(nStatus[move-2]==nodeStatus || nStatus[move-2]==EnumFacilityStatus.BLOCKED)
- {
- nStatus[move-1]=nodeStatus;
- }
- }
- }
- public void initialize(FacilityGameAPI game) {
- n = game.getN();
- nStatus= new EnumFacilityStatus[n];
- nValues=new int[n];
- moveIndex=-1;
- nStatus=game.getStatusCopy();
- nValues=game.getValueCopy();
- DEPTH=2;
- }
- //i sinartisi axiologisis tu board ine i diafora ton skor etsi o player A ine o maximizer kai o player b o minimizer
- public long evalBoard(){
- long a=(long)scoreCalc(EnumPlayer.PLAYER_A);
- long b=(long)scoreCalc(EnumPlayer.PLAYER_B);
- long eval=0;
- eval=a-b;
- return eval;
- }
- //sinartisi ipologismu tu score ine san aftin pu exete kanei gia ton server
- public int scoreCalc(EnumPlayer player){
- int score = 0;
- EnumFacilityStatus nodeStatus = null;
- if (player == EnumPlayer.PLAYER_A) {
- nodeStatus = EnumFacilityStatus.PLAYER_A;
- } else if (player == EnumPlayer.PLAYER_B) {
- nodeStatus = EnumFacilityStatus.PLAYER_B;
- }
- int tempBonus = 0;
- int numOfConsecutiveFacilities = 0;
- int node = 0;
- while (node < n) {
- if (nStatus[node] == nodeStatus) {
- int nodeValue = nValues[node];
- score += nodeValue;
- // bonus
- tempBonus += 3* nodeValue;
- numOfConsecutiveFacilities++;
- node += 1;
- } else if (nStatus[node] == EnumFacilityStatus.BLOCKED) {
- node += 1;
- } else {
- if (numOfConsecutiveFacilities >= 3) {
- score += tempBonus;
- }
- tempBonus = 0;
- numOfConsecutiveFacilities = 0;
- node += 1;
- }
- }
- if (numOfConsecutiveFacilities >= 3) {
- score += tempBonus;
- }
- return score;
- }
- //elexos an isxii to switch mode
- public boolean switchModeExists(EnumPlayer player)
- {
- boolean exist=false;
- if (player==EnumPlayer.PLAYER_A)
- {
- if (lastMovesA.size()==0)
- {
- return false;
- }
- else
- {
- for (int i=0; i<n; i++)
- {
- if(Math.abs(i-lastMovesA.get(lastMovesA.size()-1))>3 && nStatus[i]==EnumFacilityStatus.FREE)
- {
- return true;
- }
- }
- }
- }
- else{
- if (lastMovesB.size()==0)
- {
- return false;
- }
- else
- {
- for (int i=0; i<n; i++)
- {
- if(Math.abs(i-lastMovesB.get(lastMovesB.size()-1))>3 && nStatus[i]==EnumFacilityStatus.FREE)
- {
- return true;
- }
- }
- }
- }
- return exist;
- }
- //i minimax epistrefei tin timi tis kaliteris kinisis apo to evluation egw thelw to index tis kinisis aftis
- //enas alos tropos pio orthologikos itan na epistrefei adikimeno toso me tin thesi oso kai tin timi
- //logo xronu protiimithike an exw mia metavliti melos moveIndex kai na allazw ekei otan prepei tin timi tis
- public long minimax(int depth,long alpha,long beta, boolean isMaxPlayer)
- {
- long bestMove=-1;
- long nextMove;
- if(depth==0)
- {
- return evalBoard();
- }
- else if(isMaxPlayer) //MAXIMIZER
- {
- bestMove=-999999999;
- if (switchModeExists(EnumPlayer.PLAYER_A)==false )
- {
- for (int i=0; i<n; i++)
- {
- if(nStatus[i]==EnumFacilityStatus.FREE)
- {
- setMove(EnumPlayer.PLAYER_A,i);
- nextMove=minimax(depth-1,alpha,beta,false);
- if (nextMove>=bestMove && depth==DEPTH)
- {moveIndex=i;}
- bestMove=Math.max(bestMove,nextMove);
- undoMove(EnumPlayer.PLAYER_A,i);
- alpha=Math.max(bestMove, alpha);
- if(beta <= alpha) {
- return bestMove;
- }
- }
- }
- //etsi elexw an ftasei se simio pu den exei ales kinisis
- if (bestMove==-999999999)
- {
- return evalBoard();
- }
- }
- else
- {
- for (int i=0; i<n; i++)
- {
- if (Math.abs(i-lastMovesA.lastElement())>3)
- {
- if(nStatus[i]==EnumFacilityStatus.FREE)
- {
- setMove(EnumPlayer.PLAYER_A,i);
- nextMove=minimax(depth-1,alpha,beta,false);
- if (nextMove>=bestMove && depth==DEPTH)
- {moveIndex=i;}
- bestMove=Math.max(bestMove,nextMove);
- undoMove(EnumPlayer.PLAYER_A,i);
- alpha=Math.max(bestMove, alpha);
- if(beta <= alpha) {
- return bestMove;
- }
- }
- }
- }
- if (bestMove==-999999999)
- {
- return evalBoard();
- }
- }
- }
- else //MINIMIZER
- {
- bestMove=999999999;
- if (switchModeExists(EnumPlayer.PLAYER_B)==false )
- {
- for (int i=0; i<n; i++)
- {
- if(nStatus[i]==EnumFacilityStatus.FREE)
- {
- setMove(EnumPlayer.PLAYER_B,i);
- nextMove=minimax(depth-1,alpha,beta,true);
- if (nextMove<=bestMove && depth==DEPTH)
- {moveIndex=i;}
- bestMove=Math.min(bestMove,nextMove);
- undoMove(EnumPlayer.PLAYER_B,i);
- beta=Math.min(bestMove, beta);
- if(beta <= alpha) {
- return bestMove;
- }
- }
- }
- if (bestMove==999999999)
- {
- return evalBoard();
- }
- }else
- {
- for (int i=0; i<n; i++)
- {
- if (Math.abs(i-lastMovesB.lastElement())>3)
- {
- if(nStatus[i]==EnumFacilityStatus.FREE)
- {
- setMove(EnumPlayer.PLAYER_B,i);
- nextMove=minimax(depth-1,alpha,beta,true);
- if (nextMove<=bestMove && depth==DEPTH)
- {moveIndex=i;}
- bestMove=Math.min(bestMove,nextMove);
- undoMove(EnumPlayer.PLAYER_B,i);
- beta=Math.min(bestMove, beta);
- if(beta <= alpha) {
- return bestMove;
- }
- }
- }
- }
- if (bestMove==999999999)
- {
- return evalBoard();
- }
- }
- }
- return bestMove;
- }
- public int nextMove(FacilityGameAPI game) {
- nStatus=game.getStatusCopy();
- int move=-1; //den xrisimopoiite
- int numberOfMoves=game.getCurMoveIndex();
- Vector<Integer> moveLocs=game.getMoveLocation();
- long alpha=-999999999;
- long beta= 999999999;
- if (whoAmI()==(EnumPlayer.PLAYER_A))
- {
- if (numberOfMoves!=0)
- {
- lastMovesB.clear();
- lastMovesB.add(moveLocs.get(numberOfMoves-1));
- }
- move=(int)minimax(DEPTH,alpha,beta,true);
- lastMovesA.clear();
- lastMovesA.add(moveIndex);
- }
- else
- {
- lastMovesA.clear();
- lastMovesA.add(moveLocs.get(numberOfMoves-1));
- move=(int)minimax(DEPTH,alpha,beta,false);
- lastMovesB.clear();
- lastMovesB.add(moveIndex);
- }
- return moveIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement