[LIB-9] Separate chesshog-format module
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / ascii / ASCIIBuilder.java
diff --git a/chesshog-format/src/main/java/org/hedgecode/chess/ascii/ASCIIBuilder.java b/chesshog-format/src/main/java/org/hedgecode/chess/ascii/ASCIIBuilder.java
new file mode 100644 (file)
index 0000000..54599bb
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * 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.Map;
+
+import org.hedgecode.chess.Parsers;
+import org.hedgecode.chess.position.Builder;
+import org.hedgecode.chess.position.ColorPiece;
+import org.hedgecode.chess.position.ParseException;
+import org.hedgecode.chess.position.Position;
+import org.hedgecode.chess.position.Square;
+
+/**
+ * ASCII chess diagram builder.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class ASCIIBuilder implements Builder {
+
+    private static Builder _instance = new ASCIIBuilder();
+
+    private ASCIIBuilder() {
+    }
+
+    @Override
+    public String build(Position position) {
+        return _build(
+                ASCII.getType(), position
+        );
+    }
+
+    private String _build(ASCIIBoardType type, Position position) {
+        StringBuilder sb = new StringBuilder();
+        Map<Square, ColorPiece> squares = position.getSquares();
+
+        String indent = ASCII.INDENT;
+        char empty = type.getEmpty();
+        char space = type.getSpace();
+        String separator = type.getSeparator();
+
+        if (ASCII.isNotation())
+            sb.append(indent).append(
+                    _buildNotation(separator != null)
+            ).append(ASCII.CRLF);
+
+        if (type.getHeader() != null)
+            sb.append(indent).append(type.getHeader()).append(ASCII.CRLF);
+
+        for (int i = Square.getSize() - 1; i >= 0; --i) {
+            if (ASCII.isNotation())
+                sb.append(i + 1).append(ASCII.EMPTY);
+            else
+                sb.append(indent);
+
+            sb.append(type.getEdge());
+
+            for (int j = 0; j < Square.getSize(); ++j) {
+                Square square = Square.getSquare(j, i);
+                ColorPiece piece = squares.get(square);
+                char pieceChar = (piece != null) ? ASCII.getAsciiPiece(piece) : empty;
+                sb.append(space).append(pieceChar).append(space);
+                if (separator != null && j < Square.getSize() - 1)
+                    sb.append(separator);
+            }
+
+            sb.append(type.getEdge());
+            if (ASCII.isNotation())
+                sb.append(ASCII.EMPTY).append(i + 1);
+            else
+                sb.append(indent);
+
+            sb.append(ASCII.CRLF);
+
+            if (i > 0 && type.getLine() != null)
+                sb.append(indent).append(type.getLine()).append(ASCII.CRLF);
+        }
+
+        if (type.getFooter() != null)
+            sb.append(indent).append(type.getFooter()).append(ASCII.CRLF);
+
+        if (ASCII.isNotation())
+            sb.append(indent).append(
+                    _buildNotation(separator != null)
+            ).append(ASCII.CRLF);
+
+        return sb.toString();
+    }
+
+    private String _buildNotation(boolean withSeparator) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(ASCII.EMPTY);
+        for (char letter : ASCII.NOTATION) {
+            sb.append(ASCII.EMPTY).append(letter).append(ASCII.EMPTY);
+            if (withSeparator)
+                sb.append(ASCII.EMPTY);
+        }
+        sb.append(ASCII.EMPTY);
+        return sb.toString();
+    }
+
+    public static Builder getInstance() {
+        return _instance;
+    }
+
+
+    public static void main(String[] args) throws ParseException {
+        ASCII.setType(ASCIIBoardType.HYPHEN);
+        ASCII.setNotation(false);
+
+        Position position = Parsers.FEN.parser().parse(
+                "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq - 20 20"
+        );
+        System.out.println(
+                getInstance().build(position)
+        );
+
+/*
+        Position position = Parsers.WIKI.parser().parse(
+                "{{Шахматная диаграмма\n" +
+                        "|\n" +
+                        "|\n" +
+                        "|rd|nd|bd|qd|kd|bd|nd|rd\n" +
+                        "|pd|pd|  |pd|pd|pd|pd|pd\n" +
+                        "|  |  |  |  |  |  |  |\n" +
+                        "|  |  |  |  |  |  |  |\n" +
+                        "|  |  |  |pd|pl|  |  |\r\n" +
+                        "|  |  |pl|  |  |  |  |\n" +
+                        "|pl|pl|  |  |  |pl|pl|pl\n" +
+                        "|rl|nl|bl|ql|kl|bl|nl|rl\n" +
+                        "| }}"
+        );
+        System.out.println(
+                getInstance().build(position)
+        );
+*/
+
+/*
+        Position position = Parsers.TCD.parser().parse("AHL30v0GIv+PqX16ap-G");
+        System.out.println(
+                getInstance().build(position)
+        );
+*/
+
+        /*
+        System.out.println(
+                getInstance().build(Positions.INITIAL.getPosition())
+        );
+*/
+    }
+
+}