/* * 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.ascii; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.hedgecode.chess.position.Color; import org.hedgecode.chess.position.ColorPiece; import org.hedgecode.chess.position.Piece; /** * ASCII chess diagram constants. * * @author Dmitry Samoshin aka gotty */ public final class ASCII { public static final String CRLF = System.lineSeparator(); public static final char EMPTY = ' '; public static final String INDENT = " "; public static final char WHITE_PAWN = 'P'; public static final char WHITE_KNIGHT = 'N'; public static final char WHITE_BISHOP = 'B'; public static final char WHITE_ROOK = 'R'; public static final char WHITE_QUEEN = 'Q'; public static final char WHITE_KING = 'K'; public static final char BLACK_PAWN = 'p'; public static final char BLACK_KNIGHT = 'n'; public static final char BLACK_BISHOP = 'b'; public static final char BLACK_ROOK = 'r'; public static final char BLACK_QUEEN = 'q'; public static final char BLACK_KING = 'k'; public static final char[] NOTATION = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; public static final String HYPHEN_LINE = "+-------------------------------+"; public static final String HYPHEN_SEP = "|"; public static final String DOT_LINE = "+------------------------+"; public static final String DOT_EDGE = "|"; public static final char DOT_EMPTY = '.'; public static final String TIGHT_LINE = " ___ ___ ___ ___ ___ ___ ___ ___ "; public static final String TIGHT_SEP = "|"; public static final char TIGHT_EMPTY = '_'; public static final String CP866_HEADER = "╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗"; public static final String CP866_LINE = "╟───┼───┼───┼───┼───┼───┼───┼───╢"; public static final String CP866_FOOTER = "╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝"; public static final String CP866_SEP = "│"; public static final String CP866_EDGE = "║"; public static final String BAR_REGEX = HYPHEN_SEP + TIGHT_SEP + CP866_SEP; public static final String EDGE_REGEX = HYPHEN_SEP + DOT_EDGE + TIGHT_SEP + CP866_EDGE; public static final String PIECE_REGEX = "PNBRQKpnbrqk"; public static final String DOT_PIECE_REGEX = PIECE_REGEX + DOT_EMPTY; private static final Pattern PIECE_PATTERN = Pattern.compile( String.format("([%s])", PIECE_REGEX) ); private static final Map PIECES = new HashMap() { { put(ColorPiece.WHITE_PAWN, WHITE_PAWN); put(ColorPiece.BLACK_PAWN, BLACK_PAWN); put(ColorPiece.WHITE_KNIGHT, WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, BLACK_KNIGHT); put(ColorPiece.WHITE_BISHOP, WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, BLACK_BISHOP); put(ColorPiece.WHITE_ROOK, WHITE_ROOK); put(ColorPiece.BLACK_ROOK, BLACK_ROOK); put(ColorPiece.WHITE_QUEEN, WHITE_QUEEN); put(ColorPiece.BLACK_QUEEN, BLACK_QUEEN); put(ColorPiece.WHITE_KING, WHITE_KING); put(ColorPiece.BLACK_KING, BLACK_KING); } }; private static ASCIIBoardType boardType = ASCIIBoardType.HYPHEN; private static boolean withNotation = false; private ASCII() { } public static void setType(ASCIIBoardType type) { boardType = type; } public static ASCIIBoardType getType() { return boardType; } public static void setNotation(boolean isNotation) { withNotation = isNotation; } public static boolean isNotation() { return withNotation; } public static ColorPiece getColorPiece(String piece) { if (piece != null) { Matcher matcher = PIECE_PATTERN.matcher(piece); if (matcher.find()) { char chPiece = matcher.group(1).charAt(0); for (Map.Entry entry : PIECES.entrySet()) { if (entry.getValue() == chPiece) return entry.getKey(); } } } return null; } public static char getAsciiPiece(Color color, Piece piece) { return getAsciiPiece( ColorPiece.getColorPiece(color, piece) ); } public static char getAsciiPiece(ColorPiece colorPiece) { return (colorPiece != null) ? PIECES.get(colorPiece) : EMPTY; } }