/** * Returns a list with the valid Plies of this <code>Rook</code>, as specified in the overview. * * @return A <code>List</code> of valid <code>Plies</code> for this <code>Rook</code>; if the * <code>Rook</code> is not currently in a <code>Board</code>, the <code>List</code> will be * empty. */ public List<Ply> getPlies() { checkRep(); Board board = getBoard(); // Create list of valid cells to move to List<int[]> validCells = new LinkedList<int[]>(); List<Ply> validPlies = new ArrayList<Ply>(); if (board.contains(this)) { int[] pos = board.getPosition(this); int[] temp = new int[2]; // Get positions where rook can move (horizontally or vertically) for (int i = 0; i < 4; i++) { // 0 = north; 1 = east; 2 = south; 3 = west temp = oneCellNext(pos, i); while (board.isUsable(temp) && (board.isEmpty(temp) || ((board.getPiece(temp).isWhite() && !isWhite()) || (!board.getPiece(temp).isWhite() && isWhite())))) { validCells.add(temp.clone()); if (!board.isEmpty(temp) && ((board.getPiece(temp).isWhite() && !isWhite()) || (!board.getPiece(temp).isWhite() && isWhite()))) break; temp = oneCellNext(temp, i); } } // Create list of valid moves for (int[] validCell : validCells) validPlies.add(new Move(pos, validCell)); } checkRep(); return validPlies; }
/** * Returns the initial position of a <code>Rook</code>. This method makes stronger assumption that * the <code>Rook</code> plays in a <code>RectangularBoard</code> with dimensions greater than or * equal to 8. Its initial position then corresponds to the initial position of rooks in a regular * game of chess. That is, if the <code>Rook</code> is white, the initial positions are a1 or h1, * and a8 or h8 otherwise. * * <p>Note that use of this method is not necessary for the correct functioning of a <code>Rook * </code>. In other words, a <code>Rook</code> could be placed in any arbitrary cell in a * 2-dimensional <code>Board</code>, and it all its functionality will work appropriately. */ protected int[] initialPos() { Board board = getBoard(); int row; int[] pos = new int[2]; if (isWhite()) { row = 0; } else { row = 7; } pos[1] = row; pos[0] = 0; if (!board.isEmpty(pos)) pos[0] = 7; return pos; }