/* * 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.wiki; import java.util.Map; import java.util.ResourceBundle; import org.hedgecode.chess.ChessHogConstants; import org.hedgecode.chess.position.Builder; import org.hedgecode.chess.position.Color; import org.hedgecode.chess.position.ColorPiece; import org.hedgecode.chess.position.Position; import org.hedgecode.chess.position.Positions; import org.hedgecode.chess.position.Square; /** * Wikipedia chess diagram builder. * * @author Dmitry Samoshin aka gotty */ public final class WikiBuilder implements Builder { private static Builder _instance = new WikiBuilder(); private WikiBuilder() { } @Override public String build(Position position) { return _build( position, Wiki.withTemplate() ); } private String _build(Position position, boolean withTemplate) { StringBuilder sb = new StringBuilder(); Map squares = position.getSquares(); if (withTemplate) { ResourceBundle resourceBundle = ResourceBundle.getBundle(ChessHogConstants.LOCALE_BUNDLE_FILE); sb.append(Wiki.TEMPLATE_BEGIN).append( resourceBundle.getString("wiki.chess.diagram.name") ).append(Wiki.CRLF); sb.append(Wiki.SEPARATOR).append(Wiki.CRLF); sb.append(Wiki.SEPARATOR).append(Wiki.CRLF); } for (int i = Square.getSize() - 1; i >= 0; --i) { for (int j = 0; j < Square.getSize(); ++j) { Square square = Square.getSquare(j, i); ColorPiece piece = squares.get(square); String pieceName = (piece != null) ? Wiki.getWikiPiece(piece) : Wiki.EMPTY; sb.append(Wiki.SEPARATOR).append(pieceName); } sb.append(Wiki.CRLF); } if (withTemplate) { sb.append(Wiki.SEPARATOR).append(Wiki.CRLF); sb.append(Wiki.TEMPLATE_END).append(Wiki.CRLF); } return sb.toString(); } public static Builder getInstance() { return _instance; } public static void main(String[] args) { // 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); Wiki.setTemplate(true); System.out.println( getInstance().build(position) ); } }