import java.util.Vector; public class AIPlayer implements Player{ String description;//descierea jucator int level;//nivel player int color;//culoarea proprie jucatorului Vector moves;//mutarile move bestmove;//cea mai buna mutare gasita la mutarea curenta int highestlimit;//cea mai mare adancime de cautare int lowestlimit;//cea mai mica adancime de cautare Evaluation eval;//clasa de evaluare a unei pozitii static final long minusinf=-2000000000; static final long plusinf=+2000000000; public AIPlayer(String desc,int lev,int culoare){ description=desc; level=lev; color=culoare; moves=new Vector(); bestmove=null; eval=new Evaluation(); } public Vector lookForMoves(board tabla,int culoare){//cauta toate culorile posibile pentru //o anumita culoare Vector ret=new Vector(); for (int i=1;i<=8;i++) for (int j=1;j<=8;j++) if (tabla.getElementAt(i,j)==0){ int isValid=checkMove(tabla,i,j,culoare); if (isValid>0) ret.add(new move(i,j,isValid+1)); } return ret; } public int checkMove(board tabla,int row,int column,int culoare){ int ii,jj; int suma=0; for (ii=-1;ii<2;ii++) for (jj=-1;jj<2;jj++) if ((ii==0)&&(jj==0)){ //cazul asta nu exista }else if (!offLimits(ii+row,jj+column)) suma=suma+checkDirection(tabla,row,column,ii,jj,culoare); if (suma==0) return -1; else return suma; } public int checkDirection(board tabla,int row,int column,int dx,int dy,int culoare){ int piese=0; int oposite,col; int i,j; col=culoare; oposite=3-col; i=row;j=column; if (!offLimits(i+2*dx,j+2*dy)){//daca incape cel putin una buna+una rea i+=dx;j+=dy; if (tabla.getElementAt(i,j)==oposite){//daca ma aflu langa un element opus boolean gata=false,bun=false;//incerc sa vad daca gasesc pe aceeasi directie un element de culoarea mea int ii=i,jj=j; do{ ii+=dx;jj+=dy; if (offLimits(ii,jj)) gata=true; else if (tabla.getElementAt(ii,jj)!=oposite){//daca e de-al meu sau gol gata=true; if (tabla.getElementAt(ii,jj)==col) bun=true; } }while (!gata); if (bun){ if (dx!=0) piese+=Math.abs(i-ii); else piese+=Math.abs(j-jj); } } } return piese; } public boolean offLimits(int row,int column){ return ((row<1)||(row>8)||(column<1)||(column>8)); } public int isLegalMove(board b,int row,int column){ return 0; } public String getDescription(){ return description; } public int getType(){//intoarce tipul de jucator, adica player AI return 2; } public void addMove(int i,int j, int k){ moves.add(new move(i,j,k)); } public String getLastMove(){ if (moves.size()==0) return ""; else{ String ret=""; ret+=moves.size()+"."; move mv=(move)moves.get(moves.size()-1); ret+=mv.toString(); return ret; } } public move makeMove(board tabla){ move ret=null; switch (level){ case 1://nivel random Vector mutariposibile=lookForMoves(tabla,this.color); int size; size=mutariposibile.size(); if (size!=0){ int poz=(int)(size*Math.random()); if (poz>size-1) poz=size-1; ret=(move)mutariposibile.get(poz); } break; case 2://nivel 1 Vector posib2=lookForMoves(tabla,this.color); if (posib2.size()!=0){ setLowestLimit(3); setHighestLimit(5); bestmove=null; long k=alfa_beta_search(tabla,this.color,-2000000000,2000000000,1); if (bestmove!=null) //System.out.println("Mutare aleasa:"+bestmove.toString()+" de cost "+k); ret=bestmove; bestmove=null; } break; case 3://nivel 2 Vector pos=lookForMoves(tabla,this.color); if (pos.size()!=0){ setLowestLimit(5); setHighestLimit(7); bestmove=null; long k2=alfa_beta_search(tabla,this.color,-2000000000,2000000000,1); if (bestmove!=null) //System.out.println("Mutare aleasa:"+bestmove.toString()+" de cost "+k2); ret=bestmove; bestmove=null; } break; case 0://nivel -1 - calc joaca pentru adversar Vector posb=lookForMoves(tabla,this.color); if (posb.size()!=0){ setLowestLimit(3); setHighestLimit(5); bestmove=null; long k3=alfa_beta_search(tabla,this.color,-2000000000,2000000000,1); if (bestmove!=null) //System.out.println("Mutare aleasa:"+bestmove.toString()+" de cost "+k3); ret=bestmove; bestmove=null; } break; default: break; } return ret; } public void setHighestLimit(int k){ this.highestlimit=k; } public void setLowestLimit(int k){ this.lowestlimit=k; } public boolean turbulentBoard(board tabl,int culoare){ if ((tabl.getElementAt(2,2)!=0)||(tabl.getElementAt(7,7)!=0)||(tabl.getElementAt(7,2)!=0)||(tabl.getElementAt(2,7)!=0)) return true; else return false; } public long alfa_beta_search(board tabla,int culoare,long min,long max,int depth){ if ((depth==lowestlimit) && (!turbulentBoard(tabla,culoare))) return eval.evaluate_position(tabla,culoare,depth,level); if (depth==highestlimit) return eval.evaluate_position(tabla,culoare,depth,level); Vector posib=lookForMoves(tabla,culoare); if (depth%2==1){//un nod de tip max long value=min; for (int i=0;ivalue){ value=value2; bestmove=mv; } if (value>max) return max; } return value; }else{//un nod de tip min long value=max; for (int i=0;i