[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / ascii / ASCIIBuilder.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.Map;
20
21 import org.hedgecode.chess.Parsers;
22 import org.hedgecode.chess.position.Builder;
23 import org.hedgecode.chess.position.ColorPiece;
24 import org.hedgecode.chess.position.ParseException;
25 import org.hedgecode.chess.position.Position;
26 import org.hedgecode.chess.position.Square;
27
28 /**
29  * ASCII chess diagram builder.
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public final class ASCIIBuilder implements Builder {
34
35     private static Builder _instance = new ASCIIBuilder();
36
37     private ASCIIBuilder() {
38     }
39
40     @Override
41     public String build(Position position) {
42         return _build(
43                 ASCII.getType(), position
44         );
45     }
46
47     private String _build(ASCIIBoardType type, Position position) {
48         StringBuilder sb = new StringBuilder();
49         Map<Square, ColorPiece> squares = position.getSquares();
50
51         String indent = ASCII.INDENT;
52         char empty = type.getEmpty();
53         char space = type.getSpace();
54         String separator = type.getSeparator();
55
56         if (ASCII.isNotation())
57             sb.append(indent).append(
58                     _buildNotation(separator != null)
59             ).append(ASCII.CRLF);
60
61         if (type.getHeader() != null)
62             sb.append(indent).append(type.getHeader()).append(ASCII.CRLF);
63
64         for (int i = Square.getSize() - 1; i >= 0; --i) {
65             if (ASCII.isNotation())
66                 sb.append(i + 1).append(ASCII.EMPTY);
67             else
68                 sb.append(indent);
69
70             sb.append(type.getEdge());
71
72             for (int j = 0; j < Square.getSize(); ++j) {
73                 Square square = Square.getSquare(j, i);
74                 ColorPiece piece = squares.get(square);
75                 char pieceChar = (piece != null) ? ASCII.getAsciiPiece(piece) : empty;
76                 sb.append(space).append(pieceChar).append(space);
77                 if (separator != null && j < Square.getSize() - 1)
78                     sb.append(separator);
79             }
80
81             sb.append(type.getEdge());
82             if (ASCII.isNotation())
83                 sb.append(ASCII.EMPTY).append(i + 1);
84             else
85                 sb.append(indent);
86
87             sb.append(ASCII.CRLF);
88
89             if (i > 0 && type.getLine() != null)
90                 sb.append(indent).append(type.getLine()).append(ASCII.CRLF);
91         }
92
93         if (type.getFooter() != null)
94             sb.append(indent).append(type.getFooter()).append(ASCII.CRLF);
95
96         if (ASCII.isNotation())
97             sb.append(indent).append(
98                     _buildNotation(separator != null)
99             ).append(ASCII.CRLF);
100
101         return sb.toString();
102     }
103
104     private String _buildNotation(boolean withSeparator) {
105         StringBuilder sb = new StringBuilder();
106         sb.append(ASCII.EMPTY);
107         for (char letter : ASCII.NOTATION) {
108             sb.append(ASCII.EMPTY).append(letter).append(ASCII.EMPTY);
109             if (withSeparator)
110                 sb.append(ASCII.EMPTY);
111         }
112         sb.append(ASCII.EMPTY);
113         return sb.toString();
114     }
115
116     public static Builder getInstance() {
117         return _instance;
118     }
119
120
121     public static void main(String[] args) throws ParseException {
122         ASCII.setType(ASCIIBoardType.HYPHEN);
123         ASCII.setNotation(false);
124
125         Position position = Parsers.FEN.parser().parse(
126                 "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq - 20 20"
127         );
128         System.out.println(
129                 getInstance().build(position)
130         );
131
132 /*
133         Position position = Parsers.WIKI.parser().parse(
134                 "{{Шахматная диаграмма\n" +
135                         "|\n" +
136                         "|\n" +
137                         "|rd|nd|bd|qd|kd|bd|nd|rd\n" +
138                         "|pd|pd|  |pd|pd|pd|pd|pd\n" +
139                         "|  |  |  |  |  |  |  |\n" +
140                         "|  |  |  |  |  |  |  |\n" +
141                         "|  |  |  |pd|pl|  |  |\r\n" +
142                         "|  |  |pl|  |  |  |  |\n" +
143                         "|pl|pl|  |  |  |pl|pl|pl\n" +
144                         "|rl|nl|bl|ql|kl|bl|nl|rl\n" +
145                         "| }}"
146         );
147         System.out.println(
148                 getInstance().build(position)
149         );
150 */
151
152 /*
153         Position position = Parsers.TCD.parser().parse("AHL30v0GIv+PqX16ap-G");
154         System.out.println(
155                 getInstance().build(position)
156         );
157 */
158
159         /*
160         System.out.println(
161                 getInstance().build(Positions.INITIAL.getPosition())
162         );
163 */
164     }
165
166 }