/* * Copyright (c) 2018-2020. 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.pgn.format; import java.util.Collections; import java.util.List; import org.hedgecode.chess.pgn.entity.DetailMove; import org.hedgecode.chess.pgn.entity.Move; import org.hedgecode.chess.pgn.entity.Moves; import org.hedgecode.chess.pgn.entity.Variation; import static org.hedgecode.chess.pgn.PGNConstants.*; /** * ExportMovesFormat * * @author Dmitry Samoshin aka gotty */ public class ExportMovesFormat extends AbstractMovesFormat { @Override public String format(List moves) { Collections.sort(moves); StringBuilder sb = new StringBuilder(); Move prevMove = null; for (Move move : moves) { sb.append( formatSan(move, prevMove) ).append( PGN_SPACE ); if (move instanceof DetailMove) { DetailMove detailMove = (DetailMove) move; sb.append( formatAddition(detailMove) ); } prevMove = move; } return sb.toString().trim(); } @Override public String format(Moves moves) { Collections.sort(moves.getMoves()); StringBuilder sb = new StringBuilder(); sb.append( formatComment(moves.nullMove()) ); DetailMove prevMove = null; for (DetailMove move : moves.getMoves()) { sb.append( formatSan(move, prevMove) ).append( PGN_SPACE ).append( formatAddition(move) ); prevMove = move; } return sb.toString().trim(); } private String formatSan(Move move, Move prevMove) { return move.ply() % 2 != 0 ? whiteFormat(move) : blackFormat(move, prevMove); } private String whiteFormat(Move move) { return String.format( WHITE_MOVE_FORMAT, move.ply() / 2 + 1, move.move() ); } private String blackFormat(Move move, Move prevMove) { if (prevMove != null && prevMove instanceof DetailMove) { DetailMove detailMove = (DetailMove) prevMove; if (detailMove.comments().size() > 0 || detailMove.variations().size() > 0) { return String.format( BLACK_MOVE_DOT_FORMAT, move.ply() / 2, move.move() ); } } return String.format(BLACK_MOVE_FORMAT, move.move()); } private String formatAddition(DetailMove move) { StringBuilder sb = new StringBuilder(); sb.append( formatComment(move) ).append( formatVariation(move) ); return sb.toString(); } private String formatComment(DetailMove move) { StringBuilder sb = new StringBuilder(); for (String comment : move.comments()) { sb.append( String.format(COMMENT_FORMAT, comment) ).append(PGN_SPACE); } return sb.toString(); } private String formatVariation(DetailMove move) { StringBuilder sb = new StringBuilder(); for (Variation variation : move.variations()) { sb.append( String.format(VARIATION_FORMAT, format(variation)) ).append(PGN_SPACE); } return sb.toString(); } }