intV3 min_alpha_betaG(List<Vector2> GP, List<bool> deadGP, List<Vector2> BP, List<bool> deadBP, int alpha, int beta, int level, int depth, bool inverted) { int valid; int value; intV3 best_move=new intV3(6000000, nullpos,nullpos); intV3 valandmove; Vector2 ghost=nullpos; Vector2 destination,saved; bool gameOVER = false; for (int i=0; i<4; i++) { if(GP[i]==blueDen) gameOVER=true; } for (int i=0; i<4; i++) { if(BP[i]==greenDen) gameOVER=true; } if(level==depth||gameOVER==true){ if(inverted) return new intV3 ((-1)*SEF (GP, deadGP, BP, deadBP), nullpos, nullpos); else return new intV3 (SEF (GP, deadGP, BP, deadBP), nullpos, nullpos); } //for each possible play of the program(program is assumed blue, program BP) for (int i=0; i<4; i++) {//try to move each piece up if (!deadBP [i]) { destination = saved = BP [i]; // Debug.Log ("saved is " + saved); destination.y += 1; valid = checkPlay (GP, BP, i, destination); //Debug.Log ("moving BP:" + i + " up, valid: " + valid); if (valid >= 0) { //make play BP [i] = destination; if (valid < 5) { ghost = GP [valid]; GP [valid] = nullpos; deadGP [valid] = true; } //call recursively valandmove = max_alpha_betaG (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; // Debug.Log ("value, alpha: "+value+","+alpha + "at level "+(level+1)); //Debug.Log ("value, alpha: "+value+","+alpha); //rescind the play BP [i] = saved; if (valid < 5) { GP [valid] = ghost; deadGP [valid] = false; } //calculations if(beta>value){ beta=value; best_move=new intV3(value,saved,destination); } if(beta<=alpha) return new intV3(alpha, nullpos,nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece down if (!deadBP [i]) { destination = saved = BP [i]; destination.y -= 1; valid = checkPlay (GP, BP, i, destination); if (valid >= 0) { //make play BP [i] = destination; if (valid < 5) { ghost = GP [valid]; GP [valid] = nullpos; deadGP [valid] = true; } //call recursively valandmove = max_alpha_betaG (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; // Debug.Log ("value, alpha: "+value+","+alpha + "at level "+(level+1)); //Debug.Log ("value, alpha: "+value+","+alpha); //rescind the play BP [i] = saved; if (valid < 5) { GP [valid] = ghost; deadGP [valid] = false; } //calculations if(beta>value){ beta=value; best_move=new intV3(value,saved,destination); } if(beta<=alpha) return new intV3(alpha, nullpos,nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece left if (!deadBP [i]) { destination = saved = BP [i]; destination.x -= 1; valid = checkPlay (GP, BP, i, destination); if (valid >= 0) { //make play BP [i] = destination; if (valid < 5) { ghost = GP [valid]; GP [valid] = nullpos; deadGP [valid] = true; } //call recursively valandmove =max_alpha_betaG (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; //Debug.Log ("value, alpha: "+value+","+alpha + "at level "+(level+1)); //rescind the play BP [i] = saved; if (valid < 5) { GP [valid] = ghost; deadGP [valid] = false; } //calculations if(beta>value){ beta=value; best_move=new intV3(value,saved,destination); } if(beta<=alpha) return new intV3(alpha, nullpos,nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece right if (!deadBP [i]) { destination = saved = BP [i]; destination.x += 1; valid = checkPlay (GP, BP, i, destination); if (valid >= 0) { //make play BP [i] = destination; if (valid < 5) { ghost = GP [valid]; GP [valid] = nullpos; deadGP [valid] = true; } //call recursively valandmove = max_alpha_betaG (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; //Debug.Log ("value, alpha: "+value+","+alpha + "at level "+(level+1)); //Debug.Log ("value, alpha: "+value+","+alpha); //rescind the play BP [i] = saved; if (valid < 5) { GP [valid] = ghost; deadGP [valid] = false; } //calculations if(beta>value){ beta=value; best_move=new intV3(value,saved,destination); } if(beta<=alpha) return new intV3(alpha, nullpos,nullpos); } } } return best_move; }
intV3 min_alpha_beta(List<Vector2> GP, List<bool> deadGP, List<Vector2> BP, List<bool> deadBP, int alpha, int beta, int level, int depth,bool inverted) { //Debug.Log ("inside of min_alpha_beta at depth: "+level); bool bestvalchanged = false; int valid; int value; intV3 best_move=new intV3(6000000, nullpos,nullpos); intV3 valandmove; Vector2 ghost=nullpos; Vector2 destination,saved; bool gameOVER = false; for (int i=0; i<4; i++) { if(GP[i]==blueDen) gameOVER=true; } for (int i=0; i<4; i++) { if(BP[i]==greenDen) gameOVER=true; } if (level == depth||gameOVER==true) { if(inverted) return new intV3 ((-1)*SEF (GP, deadGP, BP, deadBP), nullpos, nullpos); else return new intV3 (SEF (GP, deadGP, BP, deadBP), nullpos, nullpos); } //for each possible play of the opponent(opponent assumed green, opponent GP) for (int i=0; i<4; i++) {//try to move each piece up if(!deadGP[i]){ destination=saved=GP[i]; destination.y+=1; valid=checkPlay (BP,GP,i,destination); if(valid>=0){ //make play GP[i]=destination; if(valid<5){ ghost=BP[valid]; BP[valid]=nullpos; deadBP[valid]=true; } //call recursively valandmove=max_alpha_beta (GP,deadGP,BP,deadBP,alpha,beta,level+1, depth,inverted); value=valandmove.sefscore; //Debug.Log ("value, beta: "+value+","+beta + "at level "+(level+1)); //Debug.Log ("value, beta: "+value+","+beta); //rescind the play GP[i]=saved; if(valid<5){ BP[valid]=ghost; deadBP[valid]=false; } //calculations if(beta>value){ beta=value; bestvalchanged=true; best_move=new intV3(value,saved,destination); } if(beta<=alpha) return new intV3(alpha, nullpos,nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece down if (!deadGP [i]) { destination = saved = GP [i]; destination.y -= 1; valid = checkPlay (BP, GP, i, destination); if (valid >= 0) { //make play GP [i] = destination; if (valid < 5) { ghost = BP [valid]; BP [valid] = nullpos; deadBP [valid] = true; } //call recursively valandmove = max_alpha_beta (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; // Debug.Log ("value, beta: "+value+","+beta + "at level "+(level+1)); //Debug.Log ("value, beta: "+value+","+beta); //rescind the play GP [i] = saved; if (valid < 5) { BP [valid] = ghost; deadBP [valid] = false; } //calculations if (beta > value) { beta = value; bestvalchanged=true; best_move = new intV3 (value, saved, destination); } if (beta <= alpha) return new intV3 (alpha, nullpos, nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece left if (!deadGP [i]) { destination = saved = GP [i]; destination.x -= 1; valid = checkPlay (BP, GP, i, destination); if (valid >= 0) { //make play GP [i] = destination; if (valid < 5) { ghost = BP [valid]; BP [valid] = nullpos; deadBP [valid] = true; } //call recursively valandmove = max_alpha_beta (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; // Debug.Log ("value, beta: "+value+","+beta + "at level "+(level+1)); //Debug.Log ("value, beta: "+value+","+beta); //rescind the play GP [i] = saved; if (valid < 5) { BP [valid] = ghost; deadBP [valid] = false; } //calculations if (beta > value) { beta = value; bestvalchanged=true; best_move = new intV3 (value, saved, destination); } if (beta <= alpha) return new intV3 (alpha, nullpos, nullpos); } } } for (int i=0; i<4; i++) {//try to move each piece right if (!deadGP [i]) { destination = saved = GP [i]; destination.x += 1; valid = checkPlay (BP, GP, i, destination); if (valid >= 0) { //make play GP [i] = destination; if (valid < 5) { ghost = BP [valid]; BP [valid] = nullpos; deadBP [valid] = true; } //call recursively valandmove = max_alpha_beta (GP, deadGP, BP, deadBP, alpha, beta, level + 1, depth, inverted); value = valandmove.sefscore; // Debug.Log ("value, beta: "+value+","+beta + "at level "+(level+1)); // Debug.Log ("value, beta: "+value+","+beta); //rescind the play GP [i] = saved; if (valid < 5) { BP [valid] = ghost; deadBP [valid] = false; } //calculations if (beta > value) { beta = value; bestvalchanged=true; best_move = new intV3 (value, saved, destination); } if (beta <= alpha) return new intV3 (alpha, nullpos, nullpos); } } } /*Debug.Log ("returning best_move\n"+ "best.move.startpos: "+best_move.startpos+ "\nbest.move.startpos: "+best_move.startpos+ "\nbest.move.endpos: "+best_move.endpos+ "\nbest.move.value: "+best_move.sefscore); */ /*if (!bestvalchanged) Debug.Log ("best val not changed..\n GP:" + GP [0] + GP [1] + GP [2] + GP [3] + "\ndeadGP" + deadGP [0] + deadGP [1] + deadGP [2] + deadGP [3] + "\nBP" + BP [0] + BP [1] + BP [2] + BP [3] + "\ndeadBP" + deadBP [0] + deadBP [1] + deadBP [2] + deadBP [3] + "\nalpha:" + alpha + "\nbeta:" + beta + "\nlevel:" + level + "\ndepth:" + depth );*/ return best_move; }