diff --git a/src/CheckersGameState2.java b/src/CheckersGameState2.java index 8a7b57e..bffc613 100644 --- a/src/CheckersGameState2.java +++ b/src/CheckersGameState2.java @@ -4,6 +4,8 @@ import java.util.Arrays; public class CheckersGameState2 implements CheckersGameState { + // TODO: Fix king infinite recursion + // 0 = empty // 1 = black piece // 2 = white piece @@ -17,10 +19,11 @@ public class CheckersGameState2 implements CheckersGameState { public static void main(String args[]) { CheckersGameState2 state = new CheckersGameState2(); state.printState(); - for(int i = 0; i<6; i++){ + for(int i = 0; i<100; i++){ List actions = state.actions(); + if(actions.size()==0){break;} for(Move move : actions) { - System.out.println((Move2)move+" ~~ "+((Move2)move).dest); + System.out.println((Move2)move+" ~~ "+((Move2)move).destination()); } state = (CheckersGameState2)state.result(actions.get(0)); state.printState(); @@ -136,7 +139,8 @@ public class CheckersGameState2 implements CheckersGameState { end = 4; } for(int i = beg; i captures() { ArrayList captures = new ArrayList(); for(int i = 0; i<32; i++) { @@ -222,12 +237,30 @@ public class CheckersGameState2 implements CheckersGameState { newBoard[captures[i]] = 0; } } - newBoard[move.dest] = newBoard[move.src]; - newBoard[move.src] = 0; + newBoard[move.destination()] = newBoard[move.source()]; + newBoard[move.source()] = 0; + + if(promote(move.source(), move.destination())) { + newBoard[move.destination()] += 2; + } return new CheckersGameState2(newPlayer, newBoard); } + private boolean promote(int src, int dest) { + switch(board[src]) { + case 0: + return false; + case 1: + return dest>=28; + case 2: + return dest<=3; + case 3: case 4: + return false; + } + return false; + } + public void printState () { String output = ""; for(int i = 0; i<32; i++) { diff --git a/src/Move2.java b/src/Move2.java index 20e229e..027e060 100644 --- a/src/Move2.java +++ b/src/Move2.java @@ -15,6 +15,14 @@ public class Move2 implements Move { path = p; } + public int source() { + return src; + } + + public int destination() { + return dest; + } + private int[] neighbors(int index) { int[] neighbors = new int[4]; int row = (index - index%4)/4;