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;
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;
29 * ASCII chess diagram builder.
31 * @author Dmitry Samoshin aka gotty
33 public final class ASCIIBuilder implements Builder {
35 private static Builder _instance = new ASCIIBuilder();
37 private ASCIIBuilder() {
41 public String build(Position position) {
43 ASCII.getType(), position
47 private String _build(ASCIIBoardType type, Position position) {
48 StringBuilder sb = new StringBuilder();
49 Map<Square, ColorPiece> squares = position.getSquares();
51 String indent = ASCII.INDENT;
52 char empty = type.getEmpty();
53 char space = type.getSpace();
54 String separator = type.getSeparator();
56 if (ASCII.isNotation())
57 sb.append(indent).append(
58 _buildNotation(separator != null)
61 if (type.getHeader() != null)
62 sb.append(indent).append(type.getHeader()).append(ASCII.CRLF);
64 for (int i = Square.getSize() - 1; i >= 0; --i) {
65 if (ASCII.isNotation())
66 sb.append(i + 1).append(ASCII.EMPTY);
70 sb.append(type.getEdge());
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)
81 sb.append(type.getEdge());
82 if (ASCII.isNotation())
83 sb.append(ASCII.EMPTY).append(i + 1);
87 sb.append(ASCII.CRLF);
89 if (i > 0 && type.getLine() != null)
90 sb.append(indent).append(type.getLine()).append(ASCII.CRLF);
93 if (type.getFooter() != null)
94 sb.append(indent).append(type.getFooter()).append(ASCII.CRLF);
96 if (ASCII.isNotation())
97 sb.append(indent).append(
98 _buildNotation(separator != null)
101 return sb.toString();
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);
110 sb.append(ASCII.EMPTY);
112 sb.append(ASCII.EMPTY);
113 return sb.toString();
116 public static Builder getInstance() {
121 public static void main(String[] args) throws ParseException {
122 ASCII.setType(ASCIIBoardType.HYPHEN);
123 ASCII.setNotation(false);
125 Position position = Parsers.FEN.parser().parse(
126 "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq - 20 20"
129 getInstance().build(position)
133 Position position = Parsers.WIKI.parser().parse(
134 "{{Шахматная диаграмма\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" +
148 getInstance().build(position)
153 Position position = Parsers.TCD.parser().parse("AHL30v0GIv+PqX16ap-G");
155 getInstance().build(position)
161 getInstance().build(Positions.INITIAL.getPosition())