[LIB-9] Separate chesshog-format module
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / wiki / WikiBuilder.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.wiki;
18
19 import java.util.Map;
20 import java.util.ResourceBundle;
21
22 import org.hedgecode.chess.ChessHogConstants;
23 import org.hedgecode.chess.position.Builder;
24 import org.hedgecode.chess.position.Color;
25 import org.hedgecode.chess.position.ColorPiece;
26 import org.hedgecode.chess.position.Position;
27 import org.hedgecode.chess.position.Positions;
28 import org.hedgecode.chess.position.Square;
29
30 /**
31  * Wikipedia chess diagram builder.
32  *
33  * @author Dmitry Samoshin aka gotty
34  */
35 public final class WikiBuilder implements Builder {
36
37     private static Builder _instance = new WikiBuilder();
38
39     private WikiBuilder() {
40     }
41
42     @Override
43     public String build(Position position) {
44         return _build(
45                 position, Wiki.withTemplate()
46         );
47     }
48
49     private String _build(Position position, boolean withTemplate) {
50         StringBuilder sb = new StringBuilder();
51         Map<Square, ColorPiece> squares = position.getSquares();
52
53         if (withTemplate) {
54             ResourceBundle resourceBundle =
55                     ResourceBundle.getBundle(ChessHogConstants.LOCALE_BUNDLE_FILE);
56             sb.append(Wiki.TEMPLATE_BEGIN).append(
57                     resourceBundle.getString("wiki.chess.diagram.name")
58             ).append(Wiki.CRLF);
59             sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
60             sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
61         }
62
63         for (int i = Square.getSize() - 1; i >= 0; --i) {
64             for (int j = 0; j < Square.getSize(); ++j) {
65                 Square square = Square.getSquare(j, i);
66                 ColorPiece piece = squares.get(square);
67                 String pieceName = (piece != null) ? Wiki.getWikiPiece(piece) : Wiki.EMPTY;
68                 sb.append(Wiki.SEPARATOR).append(pieceName);
69             }
70             sb.append(Wiki.CRLF);
71         }
72
73         if (withTemplate) {
74             sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
75             sb.append(Wiki.TEMPLATE_END).append(Wiki.CRLF);
76         }
77
78         return sb.toString();
79     }
80
81     public static Builder getInstance() {
82         return _instance;
83     }
84
85
86     public static void main(String[] args) {
87         // Position position = Positions.INITIAL.getPosition()
88         Position position = Positions.EMPTY.getPosition();
89         position.setKing(Color.WHITE, Square.A1);
90         position.setPawn(Color.WHITE, Square.A2);
91         position.setPawn(Color.WHITE, Square.E4);
92         position.setQueen(Color.WHITE, Square.A7);
93         position.setKing(Color.BLACK, Square.H8);
94         position.setPawn(Color.BLACK, Square.G7);
95         position.setPawn(Color.BLACK, Square.H7);
96         position.setKnight(Color.BLACK, Square.G2);
97         position.setBishop(Color.BLACK, Square.D4);
98         position.setRook(Color.BLACK, Square.E2);
99
100         Wiki.setTemplate(true);
101         System.out.println(
102                 getInstance().build(position)
103         );
104     }
105
106 }