Skip to content

Commit

Permalink
implemented generateJumpMoves(Piece).
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Apr 21, 2016
1 parent 30fd758 commit f179c3c
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/model/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,57 @@ public ArrayList<Board> generateMoveFrontier(Color color) {
return frontier;
}

/**
* Move the jumper and erase the jumpee.
* @param jump
*/
public void jump(Move jump) {
representation
[(jump.destination.row + jump.source.row)/2]
[(jump.destination.column + jump.source.column)/2] = null; // monkey
representation[jump.destination.row][jump.destination.column] =
representation[jump.source.row][jump.source.column];
representation[jump.source.row][jump.source.column] = null;
}

/**
* Returns the possible jumps a piece can take.
* @param color
* @return
*/
public ArrayList<Move> generateJumpMoves(Piece p) {
ArrayList<Move> jumps = new ArrayList<Move>();
int row = p.getLocation().row,
col = p.getLocation().column;
boolean up = p.getColor() == Color.BLACK || p.getType() == Type.KING;
boolean down = p.getColor() == Color.RED || p.getType() == Type.KING;
if (up) {
// Up left
Move upleft = new Move(new Location(row, col), new Location(row - 2, col - 2));
if (isValidJump(upleft)) {
jumps.add(upleft);
}
// Up right
Move upright = new Move(new Location(row, col), new Location(row - 2, col + 2));
if (isValidJump(upright)) {
jumps.add(upright);
}
}
if (down) {
// Down left
Move downleft = new Move(new Location(row, col), new Location(row + 2, col - 2));
if (isValidJump(downleft)) {
jumps.add(downleft);
}
// Down right
Move downright = new Move(new Location(row, col), new Location(row + 2, col + 2));
if (isValidJump(downright)) {
jumps.add(downright);
}
}
return jumps;
}

/**
* Generates the frontier.
* @param color The color of pieces to generate the frontier for.
Expand Down Expand Up @@ -196,7 +247,11 @@ else if (representation[row][col].getColor() == Color.RED) {
}
}


/**
* Determines whether a move is a valid jump.
* @param move
* @return
*/
public boolean isValidJump(Move move) {
Piece monkey = representation[(move.destination.row + move.source.row)/2][(move.destination.column + move.source.column)/2];
Piece toMove = representation[move.source.row][move.source.column];
Expand Down

0 comments on commit f179c3c

Please sign in to comment.