/** * Do the Jump, removing the piece from x1, y1 and setting the piece in x2, y2. Remove the piece * from captureX, captureY as well. If the piece is now on their "King's Row", then promote the * piece should be promoted to a King */ public void execute(final Jump jump) { // CHECK IF ALL OF THEM ON BOARD if (isOnBoard(jump.getX1(), jump.getX2()) && isOnBoard(jump.getY1(), jump.getY2()) && isOnBoard(jump.getCaptureX(), jump.getCaptureY())) { // check if landing square is empty if (isEmptySquare(jump.getY1(), jump.getY2())) { Piece piece = getPiece(jump.getY1(), jump.getX1()); // check if capture piece is enemy color if (getPiece(jump.getCaptureY(), jump.getCaptureX()).isEnemyColor(piece.getColor())) { if (Piece.WHITE_MAN.equals(piece)) { if (jump.getY2() == 7) { piece = Piece.WHITE_KING; } } else if (Piece.RED_MAN.equals(piece)) { if (jump.getY2() == 0) { piece = Piece.RED_KING; } } setPiece(jump.getY2(), jump.getX2(), piece); removePiece(jump.getCaptureY(), jump.getCaptureX()); removePiece(jump.getY1(), jump.getX1()); } } } }
/** * Returns a list of all possible moves from the piece at (x,y). If there is no piece on that * square, or the piece cannot make a legal move then return an empty list */ public List<Move> getMoves(final int x, final int y) { Piece piece = getPiece(y, x); if (piece == null) { return new ArrayList<Move>(0); } else { List<Move> moves = new ArrayList<Move>(4); if (Piece.WHITE_MAN.equals(piece)) { if (isOnBoard(x - 1, y + 1) && isEmptySquare(x - 1, y + 1)) { moves.add(new Move(x, y, x - 1, y + 1)); } if (isOnBoard(x + 1, y + 1) && isEmptySquare(x + 1, y + 1)) { moves.add(new Move(x, y, x + 1, y + 1)); } } else if (Piece.RED_MAN.equals(piece)) { if (isOnBoard(x - 1, y - 1) && isEmptySquare(x - 1, y - 1)) { moves.add(new Move(x, y, x - 1, y - 1)); } if (isOnBoard(x + 1, y - 1) && isEmptySquare(x + 1, y - 1)) { moves.add(new Move(x, y, x + 1, y - 1)); } } else { if (isOnBoard(x - 1, y - 1) && isEmptySquare(x - 1, y - 1)) { moves.add(new Move(x, y, x - 1, y - 1)); } if (isOnBoard(x + 1, y - 1) && isEmptySquare(x + 1, y - 1)) { moves.add(new Move(x, y, x + 1, y - 1)); } if (isOnBoard(x - 1, y + 1) && isEmptySquare(x - 1, y + 1)) { moves.add(new Move(x, y, x - 1, y + 1)); } if (isOnBoard(x + 1, y + 1) && isEmptySquare(x + 1, y + 1)) { moves.add(new Move(x, y, x + 1, y + 1)); } } return moves; } }
/** * Returns a list of all possible jumps from the piece at (x,y). If there is no piece on that * square, or the piece cannot make a legal jump then return an empty list */ public List<Jump> getJumps(final int x, final int y) { Piece piece = getPiece(y, x); if (piece == null) { return new ArrayList<Jump>(0); } else { List<Jump> jumps = new ArrayList<Jump>(); if (Piece.WHITE_MAN.equals(piece)) { if (isOnBoard(x - 1, y + 1) && isOnBoard(x - 2, y + 2) && !isEmptySquare(x - 1, y + 1) && getPiece(y + 1, x - 1).isEnemyColor(piece.getColor()) && isEmptySquare(x - 2, y + 2)) { jumps.add(new Jump(y, x, x - 1, y + 1, x - 2, y + 2)); } if (isOnBoard(x + 1, y + 1) && isOnBoard(x + 2, y + 2) && !isEmptySquare(x + 1, y + 1) && getPiece(y + 1, x + 1).isEnemyColor(piece.getColor()) && isEmptySquare(x + 2, y + 2)) { jumps.add(new Jump(y, x, x + 1, y + 1, x + 2, y + 2)); } } else if (Piece.RED_MAN.equals(piece)) { if (isOnBoard(x - 1, y - 1) && isOnBoard(x - 2, y - 2) && !isEmptySquare(x - 1, y - 1) && getPiece(y - 1, x - 1).isEnemyColor(piece.getColor()) && isEmptySquare(x - 2, y - 2)) { jumps.add(new Jump(y, x, x - 1, y - 1, x - 2, y - 2)); } if (isOnBoard(x + 1, y - 1) && isOnBoard(x + 2, y - 2) && !isEmptySquare(x + 1, y - 1) && getPiece(y - 1, x + 1).isEnemyColor(piece.getColor()) && isEmptySquare(x + 2, y - 2)) { jumps.add(new Jump(y, x, x + 1, y - 1, x + 2, y - 2)); } } else { if (isOnBoard(x - 1, y - 1) && isOnBoard(x - 2, y - 2) && !isEmptySquare(x - 1, y - 1) && getPiece(y - 1, x - 1).isEnemyColor(piece.getColor()) && isEmptySquare(x - 2, y - 2)) { jumps.add(new Jump(y, x, x - 1, y - 1, x - 2, y - 2)); } if (isOnBoard(x + 1, y - 1) && isOnBoard(x + 2, y - 2) && !isEmptySquare(x + 1, y - 1) && getPiece(y - 1, x + 1).isEnemyColor(piece.getColor()) && isEmptySquare(x + 2, y - 2)) { jumps.add(new Jump(y, x, x + 1, y - 1, x + 2, y - 2)); } if (isOnBoard(x - 1, y + 1) && isOnBoard(x - 2, y + 2) && !isEmptySquare(x - 1, y + 1) && getPiece(y + 1, x - 1).isEnemyColor(piece.getColor()) && isEmptySquare(x - 2, y + 2)) { jumps.add(new Jump(y, x, x - 1, y + 1, x - 2, y + 2)); } if (isOnBoard(x + 1, y + 1) && isOnBoard(x + 2, y + 2) && !isEmptySquare(x + 1, y + 1) && getPiece(y + 1, x + 1).isEnemyColor(piece.getColor()) && isEmptySquare(x + 2, y + 2)) { jumps.add(new Jump(y, x, x + 1, y + 1, x + 2, y + 2)); } } return jumps; } }