Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Change behaviors of captures and jump moves
I changed this because of [this](https://piazza.com/class/iy9nmh0fyvc2jx?cid=40) piazza post. Hopefully this is correct.
  • Loading branch information
joesweeney committed Apr 3, 2017
1 parent edeb3e3 commit 9144ec8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
30 changes: 18 additions & 12 deletions src/CheckersGameState2.java
Expand Up @@ -118,7 +118,7 @@ public class CheckersGameState2 implements CheckersGameState {
return neighbors;
}

private ArrayList<Move> findJumpMoves(int starting, int current, int[] path, int capture) {
private ArrayList<Move> findJumpMoves(int starting, int current, int[] path) {
ArrayList<Move> moves = new ArrayList<Move>();
int piece = board[starting];
int[] neighbors = neighbors(current);
Expand All @@ -141,9 +141,12 @@ public class CheckersGameState2 implements CheckersGameState {
if(next != -1 && board[next] == 0) {
int [] newPath = Arrays.copyOf(path, path.length+1);
newPath[path.length] = next;
int c = capture == -1 ? neighbors[i] : capture;
moves.add(new Move2(newPath, c));
moves.addAll(findJumpMoves(starting, next, newPath, c));
ArrayList<Move> jumpMoves = findJumpMoves(starting, next, newPath);
if(jumpMoves.size() == 0){
moves.add(new Move2(newPath));
} else {
moves.addAll(jumpMoves);
}
}
}
}
Expand All @@ -155,7 +158,7 @@ public class CheckersGameState2 implements CheckersGameState {
for(int i = 0; i<32; i++) {
int piece = board[i];
if(piece != 0 && correctColor(piece)) {
captures.addAll(findJumpMoves(i, i, new int[]{i}, -1));
captures.addAll(findJumpMoves(i, i, new int[]{i}));
}
}
return captures;
Expand All @@ -170,26 +173,26 @@ public class CheckersGameState2 implements CheckersGameState {
if(space == 1) {
// black piece
if(neighbors[0] != -1 && board[neighbors[0]] == 0) {
moves.add(new Move2(i, neighbors[0], -1));
moves.add(new Move2(i, neighbors[0]));
}
if(neighbors[1] != -1 && board[neighbors[1]] == 0) {
moves.add(new Move2(i, neighbors[1], -1));
moves.add(new Move2(i, neighbors[1]));
}
}
else if(space == 2) {
// white piece
if(neighbors[2] != -1 && board[neighbors[2]] == 0) {
moves.add(new Move2(i, neighbors[2], -1));
moves.add(new Move2(i, neighbors[2]));
}
if(neighbors[3] != -1 && board[neighbors[3]] == 0) {
moves.add(new Move2(i, neighbors[3], -1));
moves.add(new Move2(i, neighbors[3]));
}
}
else {
// king
for(int j = 0; j<4; j++) {
if(neighbors[j] != -1 && board[neighbors[j]] == 0) {
moves.add(new Move2(i, neighbors[j], -1));
moves.add(new Move2(i, neighbors[j]));
}
}
}
Expand All @@ -213,8 +216,11 @@ public class CheckersGameState2 implements CheckersGameState {
Move2 move = (Move2) x;
int[] newBoard = Arrays.copyOf(board, board.length);
int newPlayer = otherPlayer();
if(move.captures != -1) {
newBoard[move.captures] = 0;
int[] captures = move.captures();
if(captures[0] != -1) {
for(int i = 0; i<captures.length; i++) {
newBoard[captures[i]] = 0;
}
}
newBoard[move.dest] = newBoard[move.src];
newBoard[move.src] = 0;
Expand Down
65 changes: 60 additions & 5 deletions src/Move2.java
Expand Up @@ -4,17 +4,72 @@ public class Move2 implements Move {
public int[] path;
public int captures;

public Move2(int src, int dest, int capture) {
public Move2(int src, int dest) {
this.src = src;
this.dest = dest;
path = new int[] {src, dest};
this.captures = capture;
}
public Move2(int[] p, int capture) {
public Move2(int[] p) {
src = p[0];
dest = p[p.length - 1];
path = p;
this.captures = capture;
}

private int[] neighbors(int index) {
int[] neighbors = new int[4];
int row = (index - index%4)/4;
int col = index%4;
if (row % 2 == 0) {
neighbors[0] = index + 4;
neighbors[1] = index + 5;
neighbors[2] = index - 4;
neighbors[3] = index - 3;

if(col == 3) {
neighbors[1] = -1;
neighbors[3] = -1;
}
}
else {
neighbors[0] = index + 3;
neighbors[1] = index + 4;
neighbors[2] = index - 5;
neighbors[3] = index - 4;

if(col == 0) {
neighbors[0] = -1;
neighbors[2] = -1;
}
}

for(int i = 0; i<4; i++) {
if(neighbors[i] < 0 || neighbors[i] >=32) {
neighbors[i] = -1;
}
}

return neighbors;
}

public int[] captures() {
int[] captures = new int[path.length-1];
for(int i = 0; i<path.length-1; i++) {
int[] neighbors = neighbors(path[i]);
boolean direct = false;
for(int j = 0; j<4; j++) {
if(neighbors[j] == path[i+1]){
direct = true;
break;
}
else if(neighbors[j] != -1 && neighbors(neighbors[j])[j] == path[i+1]) {
captures[i] = neighbors[j];
}
}
if(direct){
captures[i] = -1;
}
}
return captures;
}

private String convert(int position) {
Expand All @@ -31,7 +86,7 @@ public class Move2 implements Move {
public String toString() {
String output = "";
for(int i = 0; i<path.length; i++) {
if(i!=0){output += ":";}
output += i!=0 ? ":" : "";
output += convert(path[i]);
}
return output;
Expand Down

0 comments on commit 9144ec8

Please sign in to comment.