Skip to content
Permalink
Browse files

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 9144ec823ea141b50cbf0689bbab5401ff187c00
Showing with 78 additions and 17 deletions.
  1. +18 −12 src/CheckersGameState2.java
  2. +60 −5 src/Move2.java
@@ -118,7 +118,7 @@ private boolean isEnemy(int piece) {
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);
@@ -141,9 +141,12 @@ else if(piece == 2) {
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);
}
}
}
}
@@ -155,7 +158,7 @@ else if(piece == 2) {
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;
@@ -170,26 +173,26 @@ else if(piece == 2) {
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]));
}
}
}
@@ -213,8 +216,11 @@ public CheckersGameState result ( Move x ) {
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;
@@ -4,17 +4,72 @@
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) {
@@ -31,7 +86,7 @@ private String convert(int position) {
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;

0 comments on commit 9144ec8

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