X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fformat%2FExportMovesFormat.java;fp=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fformat%2FExportMovesFormat.java;h=4de3b4161ea9dd6af2152217f91e3d5826e4fd45;hp=0000000000000000000000000000000000000000;hb=5d271f23371d6a435a0435f000370f4d9b8c621a;hpb=569af88b0115065e1a4551a20501bbd200fcba17 diff --git a/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/ExportMovesFormat.java b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/ExportMovesFormat.java new file mode 100644 index 0000000..4de3b41 --- /dev/null +++ b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/ExportMovesFormat.java @@ -0,0 +1,133 @@ +/* + * 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(); + } + +} \ No newline at end of file