/** * A representation of a tile type by the directions of the possible connections * to other tiles. * * @author Felix Reidl */ public enum TileType { // Read: right is the LSB! BLANK("0000"), LR("0101"), TB("1010"), LT("0011"), TR("0110"), RB("1100"), LB("1001"), // 2-tiles LTR("0111"), TRB("1110"), LRB("1101"), LTB("1011"), // 3-tiles LTRB("1111"); // 4-tile private final byte mask; TileType(String mask) { this.mask = Byte.parseByte(mask, 2); } public static TileType fromByte(byte mask) { TileType[] tiles = TileType.values(); for (int i = 0; i < tiles.length; i++) { if (tiles[i].mask == mask) { return tiles[i]; } } throw new RuntimeException("Invalid tile mask '" + Integer.toBinaryString(mask) + "' (" + mask + ")"); } public static boolean isBlank(TileType type) { return type == BLANK; } public static boolean isCurve(TileType type) { return type == LT || type == TR || type == RB || type == LB; } public static boolean isStraight(TileType type) { return type == TB || type == LR; } public static boolean isTee(TileType type) { return type == LTR || type == TRB || type == LRB || type == LTB; } public static boolean isCross(TileType type) { return type == LTRB; } /** * The byte representation of the tile has for relevant position. If a * bit is set to 1, it means that the tile has a connection in that * direction. From left to right the positions mean: Left, Top, Right and * Bottom, i.e. clockwise starting left. * @return A byte representation of the tile. */ public byte toByte() { return mask; } }