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.ArrayList;
20 import java.util.List;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.hedgecode.chess.Builders;
25 import org.hedgecode.chess.position.Color;
26 import org.hedgecode.chess.position.ParseException;
27 import org.hedgecode.chess.position.Parser;
28 import org.hedgecode.chess.position.Position;
29 import org.hedgecode.chess.position.Positions;
30 import org.hedgecode.chess.position.Square;
33 * ASCII chess diagram parser.
35 * @author Dmitry Samoshin aka gotty
37 public final class ASCIIParser implements Parser {
39 private static final String LINE_REGEX = "\\r?\\n";
40 private static final String BAR_SQUARE_REGEX = String.format("[%s]", ASCII.BAR_REGEX);
41 private static final String DOT_SQUARE_REGEX = String.format("[^%s]+", ASCII.DOT_PIECE_REGEX);
43 private static final Pattern BAR_PATTERN =
46 "^.*[%s](([^%s]+[%s]){%d}[^%s]+)[%s].*$",
47 ASCII.EDGE_REGEX, ASCII.BAR_REGEX, ASCII.BAR_REGEX,
48 Square.getSize() - 1, ASCII.EDGE_REGEX, ASCII.EDGE_REGEX
52 private static final Pattern DOT_PATTERN =
55 "^.*[%s](([^%s]+[%s]){%d}[^%s]+)[%s].*$",
56 ASCII.DOT_EDGE, ASCII.DOT_PIECE_REGEX, ASCII.DOT_PIECE_REGEX,
57 Square.getSize(), ASCII.DOT_PIECE_REGEX, ASCII.DOT_EDGE
61 private static Parser _instance = new ASCIIParser();
63 private ASCIIParser() {
67 public Position parse(String string) throws ParseException {
69 string.split(LINE_REGEX)
73 private Position _parse(String[] strings) throws ParseException {
75 String squareRegex = BAR_SQUARE_REGEX;
76 lines = _parseLines(strings, BAR_PATTERN);
77 if (lines.length != Square.getSize()) {
78 squareRegex = DOT_SQUARE_REGEX;
79 lines = _parseLines(strings, DOT_PATTERN);
80 if (lines.length != Square.getSize())
81 throw new ParseException("parse.ascii.incorrect.board");
83 Position position = Positions.EMPTY.getPosition();
92 private String[] _parseLines(String[] strings, Pattern linePattern) {
93 List<String> lines = new ArrayList<>();
94 for (String line : strings) {
95 Matcher matcher = linePattern.matcher(line);
98 matcher.group(1).trim()
102 return lines.toArray(
103 new String[lines.size()]
107 private void _parsePieces(String[] lines, Position position, String squareRegex) throws ParseException {
108 for (int i = 0; i < lines.length; ++i) {
109 int line = lines.length - i;
110 String[] pieces = lines[i].split(squareRegex, -1);
111 if (pieces.length != Square.getSize())
112 throw new ParseException("parse.ascii.incorrect.board");
113 for (int j = 0; j < pieces.length; ++j) {
115 ASCII.getColorPiece(pieces[j]),
116 Square.getSquare(j, line - 1)
122 public static Parser getInstance() {
127 public static void main(String[] args) throws ParseException {
128 ASCII.setType(ASCIIBoardType.DOT);
129 ASCII.setNotation(false);
131 // Position position = Positions.INITIAL.getPosition();
132 Position position = Positions.EMPTY.getPosition();
133 position.setKing(Color.WHITE, Square.A1);
134 position.setPawn(Color.WHITE, Square.A2);
135 position.setPawn(Color.WHITE, Square.E4);
136 position.setQueen(Color.WHITE, Square.A7);
137 position.setKing(Color.BLACK, Square.H8);
138 position.setPawn(Color.BLACK, Square.G7);
139 position.setPawn(Color.BLACK, Square.H7);
140 position.setKnight(Color.BLACK, Square.G2);
141 position.setBishop(Color.BLACK, Square.D4);
142 position.setRook(Color.BLACK, Square.E2);
144 String ascii = Builders.ASCII.builder().build(position);
149 position = getInstance().parse(ascii);
151 ASCII.setType(ASCIIBoardType.HYPHEN);
152 ascii = Builders.ASCII.builder().build(position);