public final void setBoardStyle(BoardStyle newStyle) { // We don't allow any changes to the board's dimensions; only changes to // the appearance of the board. if (boardStyle != null && (boardStyle.getFrameWidth() != newStyle.getFrameWidth() || boardStyle.getSquareSize() != newStyle.getSquareSize() || boardStyle.getHeight() != newStyle.getHeight())) { throw new ChessException( "New board style dimensions do not match the current board dimensions"); } boardStyle = newStyle; chessPieceSet = ChessSet.getChessSet(boardStyle.getPieceStyleName()); }
/** * Get the Chesspresso square index of the given location * * @param loc desired location * @return the square index, or Chess.NO_SQUARE if not on the board */ int getSquareAt(Location loc) { if (!areaBoard.contains(loc)) { return Chess.NO_SQUARE; } int row = 0, col = 0; switch (rotation) { case NORTH: row = 7 - ((loc.getBlockX() - areaBoard.getLowerX()) / boardStyle.getSquareSize()); col = 7 - ((loc.getBlockZ() - areaBoard.getLowerZ()) / boardStyle.getSquareSize()); break; case EAST: row = 7 - ((loc.getBlockZ() - areaBoard.getLowerZ()) / boardStyle.getSquareSize()); col = -((areaBoard.getLowerX() - loc.getBlockX()) / boardStyle.getSquareSize()); break; case SOUTH: row = -((areaBoard.getLowerX() - loc.getBlockX()) / boardStyle.getSquareSize()); col = -((areaBoard.getLowerZ() - loc.getBlockZ()) / boardStyle.getSquareSize()); break; case WEST: row = -((areaBoard.getLowerZ() - loc.getBlockZ()) / boardStyle.getSquareSize()); col = 7 - ((loc.getBlockX() - areaBoard.getLowerX()) / boardStyle.getSquareSize()); break; } return Chess.coorToSqi(col, row); }
/** * Get the Cuboid region for this square <i>of the chessboard itself</i> * * @param row * @param col * @return a Cuboid representing the square */ public Cuboid getSquare(int row, int col) { if (row < 0 || col < 0 || row > 7 || col > 7) { throw new ChessException("ChessBoard: getSquare: bad (row, col): (" + row + "," + col + ")"); } Cuboid sq = new Cuboid(a1Corner.getLocation()); int s = boardStyle.getSquareSize(); CuboidDirection dir = rotation.getDirection(); CuboidDirection dirRight = rotation.getRight().getDirection(); sq = sq.shift(dir, row * s).shift(dirRight, col * s); sq = sq.expand(dir, s - 1).expand(dirRight, s - 1); return sq; }
private PersistableLocation initH8Corner(Location a1) { Location h8 = new Location(a1.getWorld(), a1.getBlockX(), a1.getBlockY(), a1.getBlockZ()); int size = boardStyle.getSquareSize(); switch (rotation) { case NORTH: h8.add(-size * 8 + 1, 0, -size * 8 + 1); break; case EAST: h8.add(size * 8 - 1, 0, -size * 8 + 1); break; case SOUTH: h8.add(size * 8 - 1, 0, size * 8 - 1); break; case WEST: h8.add(-size * 8 + 1, 0, size * 8 - 1); break; } return new PersistableLocation(h8); }
private PersistableLocation initA1Corner(Location origin, BoardRotation rotation) { Location a1 = new Location(origin.getWorld(), origin.getBlockX(), origin.getBlockY(), origin.getBlockZ()); int offset = boardStyle.getSquareSize() / 2; switch (rotation) { case NORTH: a1.add(offset, 0, offset); break; case EAST: a1.add(-offset, 0, offset); break; case SOUTH: a1.add(-offset, 0, -offset); break; case WEST: a1.add(offset, 0, -offset); break; } return new PersistableLocation(a1); }