[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / ascii / ASCII.java
1 /*
2  * Copyright (c) 2018. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.ascii;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23
24 import org.hedgecode.chess.position.Color;
25 import org.hedgecode.chess.position.ColorPiece;
26 import org.hedgecode.chess.position.Piece;
27
28 /**
29  * ASCII chess diagram constants.
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public final class ASCII {
34
35     public static final String CRLF = System.lineSeparator();
36
37     public static final char EMPTY = ' ';
38     public static final String INDENT = "  ";
39
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';
46
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';
53
54     public static final char[] NOTATION = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
55
56     public static final String HYPHEN_LINE  = "+-------------------------------+";
57     public static final String HYPHEN_SEP   = "|";
58
59     public static final String DOT_LINE     = "+------------------------+";
60     public static final String DOT_EDGE     = "|";
61     public static final char   DOT_EMPTY    = '.';
62
63     public static final String TIGHT_LINE   = " ___ ___ ___ ___ ___ ___ ___ ___ ";
64     public static final String TIGHT_SEP    = "|";
65     public static final char   TIGHT_EMPTY  = '_';
66
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   = "║";
72
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;
75
76     public static final String PIECE_REGEX = "PNBRQKpnbrqk";
77     public static final String DOT_PIECE_REGEX = PIECE_REGEX + DOT_EMPTY;
78
79     private static final Pattern PIECE_PATTERN = Pattern.compile(
80             String.format("([%s])", PIECE_REGEX)
81     );
82
83     private static final Map<ColorPiece, Character> PIECES = new HashMap<ColorPiece, Character>() {
84         {
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);
91         }
92     };
93
94     private static ASCIIBoardType boardType = ASCIIBoardType.HYPHEN;
95     private static boolean withNotation = false;
96
97     private ASCII() {
98     }
99
100     public static void setType(ASCIIBoardType type) {
101         boardType = type;
102     }
103
104     public static ASCIIBoardType getType() {
105         return boardType;
106     }
107
108     public static void setNotation(boolean isNotation) {
109         withNotation = isNotation;
110     }
111
112     public static boolean isNotation() {
113         return withNotation;
114     }
115
116     public static ColorPiece getColorPiece(String piece) {
117         if (piece != null) {
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();
124                 }
125             }
126         }
127         return null;
128     }
129
130     public static char getAsciiPiece(Color color, Piece piece) {
131         return getAsciiPiece(
132                 ColorPiece.getColorPiece(color, piece)
133         );
134     }
135
136     public static char getAsciiPiece(ColorPiece colorPiece) {
137         return (colorPiece != null)
138                 ? PIECES.get(colorPiece)
139                 : EMPTY;
140     }
141
142 }