--- /dev/null
+/*
+ * Copyright (c) 2018. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.ascii;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.hedgecode.chess.Builders;
+import org.hedgecode.chess.position.Color;
+import org.hedgecode.chess.position.ParseException;
+import org.hedgecode.chess.position.Parser;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Positions;
+import org.hedgecode.chess.position.Square;
+
+/**
+ * ASCII chess diagram parser.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class ASCIIParser implements Parser {
+
+ private static final String LINE_REGEX = "\\r?\\n";
+ private static final String BAR_SQUARE_REGEX = String.format("[%s]", ASCII.BAR_REGEX);
+ private static final String DOT_SQUARE_REGEX = String.format("[^%s]+", ASCII.DOT_PIECE_REGEX);
+
+ private static final Pattern BAR_PATTERN =
+ Pattern.compile(
+ String.format(
+ "^.*[%s](([^%s]+[%s]){%d}[^%s]+)[%s].*$",
+ ASCII.EDGE_REGEX, ASCII.BAR_REGEX, ASCII.BAR_REGEX,
+ Square.getSize() - 1, ASCII.EDGE_REGEX, ASCII.EDGE_REGEX
+ )
+ );
+
+ private static final Pattern DOT_PATTERN =
+ Pattern.compile(
+ String.format(
+ "^.*[%s](([^%s]+[%s]){%d}[^%s]+)[%s].*$",
+ ASCII.DOT_EDGE, ASCII.DOT_PIECE_REGEX, ASCII.DOT_PIECE_REGEX,
+ Square.getSize(), ASCII.DOT_PIECE_REGEX, ASCII.DOT_EDGE
+ )
+ );
+
+ private static Parser _instance = new ASCIIParser();
+
+ private ASCIIParser() {
+ }
+
+ @Override
+ public Position parse(String string) throws ParseException {
+ return _parse(
+ string.split(LINE_REGEX)
+ );
+ }
+
+ private Position _parse(String[] strings) throws ParseException {
+ String[] lines;
+ String squareRegex = BAR_SQUARE_REGEX;
+ lines = _parseLines(strings, BAR_PATTERN);
+ if (lines.length != Square.getSize()) {
+ squareRegex = DOT_SQUARE_REGEX;
+ lines = _parseLines(strings, DOT_PATTERN);
+ if (lines.length != Square.getSize())
+ throw new ParseException("parse.ascii.incorrect.board");
+ }
+ Position position = Positions.EMPTY.getPosition();
+ _parsePieces(
+ lines,
+ position,
+ squareRegex
+ );
+ return position;
+ }
+
+ private String[] _parseLines(String[] strings, Pattern linePattern) {
+ List<String> lines = new ArrayList<>();
+ for (String line : strings) {
+ Matcher matcher = linePattern.matcher(line);
+ if (matcher.find()) {
+ lines.add(
+ matcher.group(1).trim()
+ );
+ }
+ }
+ return lines.toArray(
+ new String[lines.size()]
+ );
+ }
+
+ private void _parsePieces(String[] lines, Position position, String squareRegex) throws ParseException {
+ for (int i = 0; i < lines.length; ++i) {
+ int line = lines.length - i;
+ String[] pieces = lines[i].split(squareRegex, -1);
+ if (pieces.length != Square.getSize())
+ throw new ParseException("parse.ascii.incorrect.board");
+ for (int j = 0; j < pieces.length; ++j) {
+ position.setPiece(
+ ASCII.getColorPiece(pieces[j]),
+ Square.getSquare(j, line - 1)
+ );
+ }
+ }
+ }
+
+ public static Parser getInstance() {
+ return _instance;
+ }
+
+
+ public static void main(String[] args) throws ParseException {
+ ASCII.setType(ASCIIBoardType.DOT);
+ ASCII.setNotation(false);
+
+ // Position position = Positions.INITIAL.getPosition();
+ Position position = Positions.EMPTY.getPosition();
+ position.setKing(Color.WHITE, Square.A1);
+ position.setPawn(Color.WHITE, Square.A2);
+ position.setPawn(Color.WHITE, Square.E4);
+ position.setQueen(Color.WHITE, Square.A7);
+ position.setKing(Color.BLACK, Square.H8);
+ position.setPawn(Color.BLACK, Square.G7);
+ position.setPawn(Color.BLACK, Square.H7);
+ position.setKnight(Color.BLACK, Square.G2);
+ position.setBishop(Color.BLACK, Square.D4);
+ position.setRook(Color.BLACK, Square.E2);
+
+ String ascii = Builders.ASCII.builder().build(position);
+ System.out.println(
+ ascii
+ );
+
+ position = getInstance().parse(ascii);
+
+ ASCII.setType(ASCIIBoardType.HYPHEN);
+ ascii = Builders.ASCII.builder().build(position);
+ System.out.println(
+ ascii
+ );
+ }
+
+}