Skip to content
Permalink
Browse files

Got GameState2 all working!

It can play the checkers!
  • Loading branch information
joesweeney committed Apr 4, 2017
1 parent 5bf801c commit 8388b88cfe2801b9d47873f85f2d56b072eca47a
Showing with 46 additions and 5 deletions.
  1. +38 −5 src/CheckersGameState2.java
  2. +8 −0 src/Move2.java
@@ -4,6 +4,8 @@

public class CheckersGameState2 implements CheckersGameState {

// TODO: Fix king infinite recursion

// 0 = empty
// 1 = black piece
// 2 = white piece
@@ -17,10 +19,11 @@
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<Move> 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 @@ else if(piece == 2) {
end = 4;
}
for(int i = beg; i<end; i++) {
if(neighbors[i] != -1 && isEnemy(board[neighbors[i]])) {
boolean valid = neighbors[i] != -1 && isEnemy(board[neighbors[i]]);
if(valid && !isCaptured(neighbors[i], path)) {
int next = neighbors(neighbors[i])[i];
if(next != -1 && board[next] == 0) {
int [] newPath = Arrays.copyOf(path, path.length+1);
@@ -153,6 +157,17 @@ else if(piece == 2) {
return moves;
}

private boolean isCaptured(int space, int[] path) {
Move temp = new Move2(path);
int[] captured = temp.captures();
for(int i = 0; i<captured.length; i++) {
if(captured[i]==space) {
return true;
}
}
return false;
}

private ArrayList<Move> captures() {
ArrayList<Move> captures = new ArrayList<Move>();
for(int i = 0; i<32; i++) {
@@ -222,12 +237,30 @@ public CheckersGameState result ( Move x ) {
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++) {
@@ -15,6 +15,14 @@ public Move2(int[] p) {
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;

0 comments on commit 8388b88

Please sign in to comment.
You can’t perform that action at this time.