diff --git a/src/CheckersAI.java b/src/CheckersAI.java index 35a0a1b..4515132 100644 --- a/src/CheckersAI.java +++ b/src/CheckersAI.java @@ -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; @@ -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; @@ -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; } diff --git a/src/CheckersGameState.java b/src/CheckersGameState.java index 70c826d..1c3210f 100644 --- a/src/CheckersGameState.java +++ b/src/CheckersGameState.java @@ -3,5 +3,6 @@ public interface CheckersGameState { String player (); List < Move > actions (); CheckersGameState result ( Move x ); +boolean isTerminal(); void printState (); } diff --git a/src/CheckersGameState3.java b/src/CheckersGameState3.java index de1aed7..840198f 100644 --- a/src/CheckersGameState3.java +++ b/src/CheckersGameState3.java @@ -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 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 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){