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.wiki;
20 import java.util.ResourceBundle;
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;
31 * Wikipedia chess diagram builder.
33 * @author Dmitry Samoshin aka gotty
35 public final class WikiBuilder implements Builder {
37 private static Builder _instance = new WikiBuilder();
39 private WikiBuilder() {
43 public String build(Position position) {
45 position, Wiki.withTemplate()
49 private String _build(Position position, boolean withTemplate) {
50 StringBuilder sb = new StringBuilder();
51 Map<Square, ColorPiece> squares = position.getSquares();
54 ResourceBundle resourceBundle =
55 ResourceBundle.getBundle(ChessHogConstants.LOCALE_BUNDLE_FILE);
56 sb.append(Wiki.TEMPLATE_BEGIN).append(
57 resourceBundle.getString("wiki.chess.diagram.name")
59 sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
60 sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
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);
74 sb.append(Wiki.SEPARATOR).append(Wiki.CRLF);
75 sb.append(Wiki.TEMPLATE_END).append(Wiki.CRLF);
81 public static Builder getInstance() {
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);
100 Wiki.setTemplate(true);
102 getInstance().build(position)