Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement variable depth minimax
  • Loading branch information
sas12028 committed Apr 19, 2017
1 parent d596983 commit 9f27007
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
35 changes: 18 additions & 17 deletions src/CheckersAI.java
Expand Up @@ -9,29 +9,32 @@ public class CheckersAI{
this.player = player;
}

private boolean stop(CheckersGameState state, boolean jumped, int depth){
private boolean stop(CheckersGameState state, boolean jumped, int depth, int min_ply){
CheckersGameState3 s = (CheckersGameState3) state;
if(!s.canJump() && !jumped && !s.canExchange() && depth == 3){
if(depth < min_ply){
return false;
}
else if(depth == min_ply && !s.canJump() && !jumped && !s.canExchange()){
return true;
}
else if(!s.canJump() && !s.canExchange() && depth == 4){
else if(depth == min_ply +1 && !s.canJump() && !s.canExchange() && depth == 4){
return true;
}
else if(depth < 11 && !s.canJump()){
else if(depth > min_ply +1 && depth < min_ply + 10 && !s.canJump()){
return true;
}
else if(depth < 20 && s.twoKings()){
else if(depth > min_ply +10 && depth < min_ply + 20 && s.twoKings()){
return true;
}
else if(depth >= 20){
else if(depth >= min_ply + 20){
return true;
}
else{
return false;
}
}

public Move minimax(CheckersGameState s){
public Move minimax(CheckersGameState s, int min_ply){
int depth = 0;
if(s.isTerminal()){
return null;
Expand All @@ -43,7 +46,7 @@ public class CheckersAI{
Move max = null;
// System.out.println(s.actions().size());
for(Move a: s.actions()){
check = minValue(s.result(a), alpha, beta, depth + 1, a.isJump());
check = minValue(s.result(a), alpha, beta, depth + 1, a.isJump(), min_ply);
if(check > v){
v = check;
max = a;
Expand All @@ -56,15 +59,14 @@ public class CheckersAI{
return max;
}

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

if(s.isTerminal() || stop(s, jumped, depth)){
return eval.evaluate(s, this.player); // if terminal, piece ratio should be infinite
private double maxValue(CheckersGameState s, double alpha, double beta, int depth, boolean jumped, int min_ply){
if(s.isTerminal() || stop(s, jumped, depth, min_ply)){
return eval.evaluate(s, this.player); // if terminal, piece ratio should be infinite
}
double v = Double.NEGATIVE_INFINITY;
double check;
for(Move a: s.actions()){
check = minValue(s.result(a), alpha, beta, depth + 1, a.isJump());
check = minValue(s.result(a), alpha, beta, depth + 1, a.isJump(), min_ply);
if(check > v){
v = check;
}
Expand All @@ -76,15 +78,14 @@ public class CheckersAI{
return v;
}

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

if(s.isTerminal() || stop(s, jumped, depth)){
private double minValue(CheckersGameState s, double alpha, double beta, int depth, boolean jumped, int min_ply){
if(s.isTerminal() || stop(s, jumped, depth, min_ply)){
return eval.evaluate(s, this.player);
}
double v = Double.POSITIVE_INFINITY;
double check;
for(Move a: s.actions()){
check = maxValue(s.result(a), alpha, beta, depth + 1, a.isJump());
check = maxValue(s.result(a), alpha, beta, depth + 1, a.isJump(), min_ply);
if(check < v){
v = check;
}
Expand Down
2 changes: 1 addition & 1 deletion src/CheckersGameState3.java
Expand Up @@ -38,7 +38,7 @@ public class CheckersGameState3 implements CheckersGameState{

boolean canJump(){
//System.out.println(this.moves);
return this.actions.get(0).isJump();
return (this.actions.size() > 0 && this.actions.get(0).isJump());
}

boolean canExchange(){
Expand Down
2 changes: 1 addition & 1 deletion src/RmCheckersClient.java
Expand Up @@ -145,7 +145,7 @@ public class RmCheckersClient {
}
while(currentState.actions().size()>0){
currentState.printState();
Move myMove = ai.minimax(currentState);
Move myMove = ai.minimax(currentState, 8);
writeMessageAndEcho(myMove.toString());
if(!applyMove(myMove.toString())) {
System.out.println("couldn't apply my move");
Expand Down

0 comments on commit 9f27007

Please sign in to comment.