Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Working alpha beta pruning
  • Loading branch information
sas12028 committed Apr 17, 2017
1 parent b7d4203 commit 6321c44
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 71 deletions.
35 changes: 21 additions & 14 deletions src/CheckersAI.java
Expand Up @@ -2,35 +2,42 @@ public class CheckersAI{


Evaluator eval;
int player;

public CheckersAI(Evaluator e, int player){
this.eval = e;
this.player = player;
}

public Move minimax(CheckersGameState s, int ply){
double alpha = Double.POSITIVE_INFINITY;
double beta = Double.NEGATIVE_INFINITY;
int depth = 0;
if(s.isTerminal() || depth == ply){
return null;
}
double alpha = Double.NEGATIVE_INFINITY;
double beta = Double.POSITIVE_INFINITY;
double v = Double.NEGATIVE_INFINITY;
double check;
Move max;
Move max = null;
System.out.println(s.actions().size());
for(Move a: s.actions()){
check = minValue(s.result(a), alpha, beta, ply, depth + 1);
if(check > v){
v = check;
max = a;
}
if(v > beta){
alpha = Math.max(alpha, v);
if(alpha >= beta){
return max;
}
alpha = Math.max(alpha, v);
}
returm max;
return max;
}

private double maxValue(CheckersGameState s, double alpha, double beta, int ply, int depth){

if(s.isTerminal() || depth == ply){
return eval.evaluate(s); // if terminal, piece ratio should be infinite
if(s.isTerminal() || depth == ply){
return eval.evaluate(s, this.player); // if terminal, piece ratio should be infinite
}
double v = Double.NEGATIVE_INFINITY;
double check;
Expand All @@ -39,18 +46,18 @@ public class CheckersAI{
if(check > v){
v = check;
}
if (v >= beta){
return v;
}
alpha = Math.max(alpha, v);
if (alpha >= beta){
return v;
}
}
return v;
}

private double minValue(CheckersGameState s, double alpha, double beta, int ply, int depth){

if(s.isTerminal() || depth == ply){
return eval.evaluate(s);
return eval.evaluate(s, this.player);
}
double v = Double.POSITIVE_INFINITY;
double check;
Expand All @@ -59,10 +66,10 @@ public class CheckersAI{
if(check < v){
v = check;
}
if( v <= alpha){
beta = Math.min(beta, v);
if( beta <= alpha){
return v;
}
beta = Math.min(beta, v);
}
return v;
}
Expand Down
1 change: 1 addition & 0 deletions src/CheckersGameState.java
Expand Up @@ -3,5 +3,6 @@ public interface CheckersGameState {
String player ();
List < Move > actions ();
CheckersGameState result ( Move x );
boolean isTerminal();
void printState ();
}
27 changes: 19 additions & 8 deletions src/CheckersGameState3.java
Expand Up @@ -252,27 +252,38 @@ public class CheckersGameState3 implements CheckersGameState{
}
}

public boolean myPiece(int i){
if(this.player == 1 && i == 1 || this.player == 1 && i == 3) //black
return true;
if(this.player == 0 && i == 2 || this.player == 0 && i == 4) //white
return true;
else return false;
public boolean myPiece(int i, int player){
if(i == player || i == player +2){
return true;
}
else{
return false;
}
///if(this.player == 1 && i == 1 || this.player == 1 && i == 3) //black
/// return true;
///if(this.player == 0 && i == 2 || this.player == 0 && i == 4) //white
/// return true;
///else return false;
}

public double pieceRatio(){
public double pieceRatio(int player){
double total = 0.0;
double mypieces = 0.0;
for(int i = 0; i<this.board.length; i++){
if(i%9!=8){
if(this.board[i] != 0 ) total+=1.0;
if(myPiece(this.board[i])) mypieces+=1.0;
if(myPiece(this.board[i], player)) mypieces+=1.0;
}
}
//System.out.println("" + mypieces);
return mypieces/total;
}

public boolean isTerminal(){
double rat = pieceRatio(this.player);
return (rat == 0 || rat == 1);
}

public void printState(){
boolean leading = false;
int printed = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Evaluator.java
@@ -1,5 +1,5 @@
public interface Evaluator{

double evaluate(CheckersGameState s);
double evaluate(CheckersGameState s, int player);

}
4 changes: 2 additions & 2 deletions src/Evaluator00.java
Expand Up @@ -3,9 +3,9 @@

public class Evaluator00 implements Evaluator {

public double evaluate(CheckersGameState s){
public double evaluate(CheckersGameState s, int player){
CheckersGameState3 gs = (CheckersGameState3) s;
return gs.pieceRatio();
return gs.pieceRatio(player);
}

}
98 changes: 52 additions & 46 deletions src/Test.java
Expand Up @@ -33,7 +33,7 @@ public class Test{
"-", "-", "w", "-", "w", "-", "w", "-"};

String[] b4 = {"-", "-", "-", "b", "-", "b", "-", "b",
"b", "-", "-", "-", "b", "-", "b", "-",
"b", "-", "w", "-", "b", "-", "b", "-",
"-", "-", "-", "B", "-", "-", "-", "b",
"-", "-", "w", "-", "w", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-",
Expand All @@ -51,54 +51,60 @@ public class Test{
"-", "-", "-", "-", "-", "-", "-", "-"};


Random r = new Random();
//Random r = new Random();

CheckersGameState s1 = new CheckersGameState3(1, b);
CheckersGameState s2 = new CheckersGameState3(2, b);
CheckersGameState s3 = new CheckersGameState3(1, b2);
CheckersGameState s4 = new CheckersGameState3(2, b2);
CheckersGameState s5 = new CheckersGameState3(1, b3);
CheckersGameState s6 = new CheckersGameState3(2, b3);
//CheckersGameState s1 = new CheckersGameState3(1, b);
//CheckersGameState s2 = new CheckersGameState3(2, b);
//CheckersGameState s3 = new CheckersGameState3(1, b2);
//CheckersGameState s4 = new CheckersGameState3(2, b2);
//CheckersGameState s5 = new CheckersGameState3(1, b3);
//CheckersGameState s6 = new CheckersGameState3(2, b3);
CheckersGameState s7 = new CheckersGameState3(1, b4);
System.out.println("State 1");
printMoves(s1);
System.out.println("State 2");
printMoves(s2);
System.out.println("State 3");
printMoves(s3);
System.out.println("State 4");
printMoves(s4);
System.out.println("State 5");
printMoves(s5);
System.out.println("Result of State 5");
s5.result(s5.actions().get(0)).printState();
System.out.println("State 6");
printMoves(s6);
System.out.println("Result of State 6");
s6.result(s6.actions().get(0)).printState();
System.out.println("State 7");
printMoves(s7);
System.out.println("Result of State 7");
s7.result(s7.actions().get(0)).printState();
System.out.println("Playing game");
CheckersGameState s8 = new CheckersGameState3(1, b5);
printMoves(s8);
s8.result(s8.actions().get(0)).printState();
CheckersGameState state = new CheckersGameState3(1, b);
state.printState();
List<Move> moves = state.actions();
while(moves.size() > 0){
System.out.println("Possible actions: ");
for(Move move : moves) {
System.out.println(move);
}
Evaluator eval = new Evaluator00();
CheckersAI ai = new CheckersAI(eval, 1);
s7.printState();
for(Move m: s7.actions()){
System.out.println(m);
}
System.out.println(ai.minimax(s7, 3));
//System.out.println("State 1");
//printMoves(s1);
//System.out.println("State 2");
//printMoves(s2);
//System.out.println("State 3");
//printMoves(s3);
//System.out.println("State 4");
//printMoves(s4);
//System.out.println("State 5");
//printMoves(s5);
//System.out.println("Result of State 5");
//s5.result(s5.actions().get(0)).printState();
//System.out.println("State 6");
//printMoves(s6);
//System.out.println("Result of State 6");
//s6.result(s6.actions().get(0)).printState();
//System.out.println("State 7");
//printMoves(s7);
//System.out.println("Result of State 7");
//s7.result(s7.actions().get(0)).printState();
//System.out.println("Playing game");
//CheckersGameState s8 = new CheckersGameState3(1, b5);
//printMoves(s8);
//s8.result(s8.actions().get(0)).printState();
//CheckersGameState state = new CheckersGameState3(1, b);
//state.printState();
//List<Move> moves = state.actions();
//while(moves.size() > 0){
// System.out.println("Possible actions: ");
// for(Move move : moves) {
// System.out.println(move);
// }

Move action = moves.get(r.nextInt(moves.size()));
System.out.println("Chosen action: " + action);
state = state.result(action);
state.printState();
moves = state.actions();
}
//Move action = moves.get(r.nextInt(moves.size()));
//System.out.println("Chosen action: " + action);
//state = state.result(action);
//state.printState();
//moves = state.actions();
}

static void printMoves(CheckersGameState s){
Expand Down

0 comments on commit 6321c44

Please sign in to comment.