2 * Copyright (c) 2018. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.chess.ascii;
19 import java.util.HashMap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.hedgecode.chess.position.Color;
25 import org.hedgecode.chess.position.ColorPiece;
26 import org.hedgecode.chess.position.Piece;
29 * ASCII chess diagram constants.
31 * @author Dmitry Samoshin aka gotty
33 public final class ASCII {
35 public static final String CRLF = System.lineSeparator();
37 public static final char EMPTY = ' ';
38 public static final String INDENT = " ";
40 public static final char WHITE_PAWN = 'P';
41 public static final char WHITE_KNIGHT = 'N';
42 public static final char WHITE_BISHOP = 'B';
43 public static final char WHITE_ROOK = 'R';
44 public static final char WHITE_QUEEN = 'Q';
45 public static final char WHITE_KING = 'K';
47 public static final char BLACK_PAWN = 'p';
48 public static final char BLACK_KNIGHT = 'n';
49 public static final char BLACK_BISHOP = 'b';
50 public static final char BLACK_ROOK = 'r';
51 public static final char BLACK_QUEEN = 'q';
52 public static final char BLACK_KING = 'k';
54 public static final char[] NOTATION = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
56 public static final String HYPHEN_LINE = "+-------------------------------+";
57 public static final String HYPHEN_SEP = "|";
59 public static final String DOT_LINE = "+------------------------+";
60 public static final String DOT_EDGE = "|";
61 public static final char DOT_EMPTY = '.';
63 public static final String TIGHT_LINE = " ___ ___ ___ ___ ___ ___ ___ ___ ";
64 public static final String TIGHT_SEP = "|";
65 public static final char TIGHT_EMPTY = '_';
67 public static final String CP866_HEADER = "╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗";
68 public static final String CP866_LINE = "╟───┼───┼───┼───┼───┼───┼───┼───╢";
69 public static final String CP866_FOOTER = "╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝";
70 public static final String CP866_SEP = "│";
71 public static final String CP866_EDGE = "║";
73 public static final String BAR_REGEX = HYPHEN_SEP + TIGHT_SEP + CP866_SEP;
74 public static final String EDGE_REGEX = HYPHEN_SEP + DOT_EDGE + TIGHT_SEP + CP866_EDGE;
76 public static final String PIECE_REGEX = "PNBRQKpnbrqk";
77 public static final String DOT_PIECE_REGEX = PIECE_REGEX + DOT_EMPTY;
79 private static final Pattern PIECE_PATTERN = Pattern.compile(
80 String.format("([%s])", PIECE_REGEX)
83 private static final Map<ColorPiece, Character> PIECES = new HashMap<ColorPiece, Character>() {
85 put(ColorPiece.WHITE_PAWN, WHITE_PAWN); put(ColorPiece.BLACK_PAWN, BLACK_PAWN);
86 put(ColorPiece.WHITE_KNIGHT, WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, BLACK_KNIGHT);
87 put(ColorPiece.WHITE_BISHOP, WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, BLACK_BISHOP);
88 put(ColorPiece.WHITE_ROOK, WHITE_ROOK); put(ColorPiece.BLACK_ROOK, BLACK_ROOK);
89 put(ColorPiece.WHITE_QUEEN, WHITE_QUEEN); put(ColorPiece.BLACK_QUEEN, BLACK_QUEEN);
90 put(ColorPiece.WHITE_KING, WHITE_KING); put(ColorPiece.BLACK_KING, BLACK_KING);
94 private static ASCIIBoardType boardType = ASCIIBoardType.HYPHEN;
95 private static boolean withNotation = false;
100 public static void setType(ASCIIBoardType type) {
104 public static ASCIIBoardType getType() {
108 public static void setNotation(boolean isNotation) {
109 withNotation = isNotation;
112 public static boolean isNotation() {
116 public static ColorPiece getColorPiece(String piece) {
118 Matcher matcher = PIECE_PATTERN.matcher(piece);
119 if (matcher.find()) {
120 char chPiece = matcher.group(1).charAt(0);
121 for (Map.Entry<ColorPiece, Character> entry : PIECES.entrySet()) {
122 if (entry.getValue() == chPiece)
123 return entry.getKey();
130 public static char getAsciiPiece(Color color, Piece piece) {
131 return getAsciiPiece(
132 ColorPiece.getColorPiece(color, piece)
136 public static char getAsciiPiece(ColorPiece colorPiece) {
137 return (colorPiece != null)
138 ? PIECES.get(colorPiece)