/* * Copyright (c) 2018. Developed by Hedgecode. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.hedgecode.chess.tcd; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; /** * Tiny Chess Diagram (TCD) constants. * * @author Dmitry Samoshin aka gotty */ public final class TCD { public static final byte BYTE_MASK = 0b01000000; public static final byte FRAME_MASK = 0b111; public static final byte FRAME_LENGTH = 3; public static final byte WHITE = 0b000; public static final byte BLACK = 0b111; public static final byte END = 0b111; public static final byte PAWN = 0b001; public static final byte KNIGHT = 0b010; public static final byte BISHOP = 0b011; public static final byte ROOK = 0b100; public static final byte QUEEN = 0b101; public static final byte KING = 0b110; private static final byte SQUARE_A = 0b000, SQUARE_1 = SQUARE_A; private static final byte SQUARE_B = 0b001, SQUARE_2 = SQUARE_B; private static final byte SQUARE_C = 0b010, SQUARE_3 = SQUARE_C; private static final byte SQUARE_D = 0b011, SQUARE_4 = SQUARE_D; private static final byte SQUARE_E = 0b100, SQUARE_5 = SQUARE_E; private static final byte SQUARE_F = 0b101, SQUARE_6 = SQUARE_F; private static final byte SQUARE_G = 0b110, SQUARE_7 = SQUARE_G; private static final byte SQUARE_H = 0b111, SQUARE_8 = SQUARE_H; public static final byte SQUARE_SIZE = 8; public static final byte[][] SQUARE = { {SQUARE_A, SQUARE_1}, {SQUARE_B, SQUARE_2}, {SQUARE_C, SQUARE_3}, {SQUARE_D, SQUARE_4}, {SQUARE_E, SQUARE_5}, {SQUARE_F, SQUARE_6}, {SQUARE_G, SQUARE_7}, {SQUARE_H, SQUARE_8}, }; public static final byte MOVE = 0b001; public static final byte CASTLE_WHITE = 0b000; public static final byte CASTLE_BLACK = 0b111; public static final byte CASTLE_NONE = 0b000; public static final byte CASTLE_KING = 0b001; public static final byte CASTLE_QUEEN = 0b010; public static final byte CASTLE_BOTH = 0b011; public static final byte[] CASTLE_OPTS = {CASTLE_NONE, CASTLE_KING, CASTLE_QUEEN, CASTLE_BOTH}; public static final byte EN_PASSANT = 0b100; public static final byte HALFMOVE = 0b010; public static final byte FULLMOVE = 0b011; public static final byte MIN_BYTE = 0x40; public static final byte MAX_BYTE = 0x7F; private static final byte[] REPLACE_TO = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2D}; private static final byte[] REPLACE_FROM = {0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F}; public static final Charset OUTPUT_CHARSET = StandardCharsets.US_ASCII; private static boolean onlyDiagram = true; private TCD() { } public static void setDiagram(boolean isDiagram) { onlyDiagram = isDiagram; } public static boolean isDiagram() { return onlyDiagram; } public static void replaceTo(byte[] bytes) { for (int i = 0; i < bytes.length; ++i) { int j = frameSearch(REPLACE_FROM, bytes[i]); if (j >= 0) bytes[i] = REPLACE_TO[j]; } } public static void replaceFrom(byte[] bytes) { for (int i = 0; i < bytes.length; ++i) { int j = frameSearch(REPLACE_TO, bytes[i]); if (j >= 0) bytes[i] = REPLACE_FROM[j]; } } public static boolean isValid(byte[] bytes) { if (bytes.length < 1) return false; for (byte bite : bytes) { if (bite < MIN_BYTE) return false; } return true; } public static boolean isColor(byte frame) { return frame == WHITE || frame == BLACK; } public static boolean isNotColor(byte frame) { return !isColor(frame); } public static boolean isWhiteBlack(byte first, byte second) { return first == WHITE && second == BLACK; } public static boolean isBlackWhite(byte first, byte second) { return isWhiteBlack(second, first); } public static boolean isNotCastle(byte frame) { return frameSearch(CASTLE_OPTS, frame) < 0; } private static int frameSearch(byte[] bytes, byte frame) { for (int i = 0; i < bytes.length; ++i) if (bytes[i] == frame) return i; return -1; } }