[LIB-9] Set of entities for PGN parsing and formatting
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / pgn / format / ExportMovesFormat.java
1 /*
2  * Copyright (c) 2018-2020. 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.pgn.format;
18
19 import java.util.Collections;
20 import java.util.List;
21
22 import org.hedgecode.chess.pgn.entity.DetailMove;
23 import org.hedgecode.chess.pgn.entity.Move;
24 import org.hedgecode.chess.pgn.entity.Moves;
25 import org.hedgecode.chess.pgn.entity.Variation;
26
27 import static org.hedgecode.chess.pgn.PGNConstants.*;
28
29 /**
30  * ExportMovesFormat
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class ExportMovesFormat extends AbstractMovesFormat {
35
36     @Override
37     public String format(List<Move> moves) {
38         Collections.sort(moves);
39         StringBuilder sb = new StringBuilder();
40         Move prevMove = null;
41         for (Move move : moves) {
42             sb.append(
43                     formatSan(move, prevMove)
44             ).append(
45                     PGN_SPACE
46             );
47             if (move instanceof DetailMove) {
48                 DetailMove detailMove = (DetailMove) move;
49                 sb.append(
50                         formatAddition(detailMove)
51                 );
52             }
53             prevMove = move;
54         }
55         return sb.toString().trim();
56     }
57
58     @Override
59     public String format(Moves moves) {
60         Collections.sort(moves.getMoves());
61         StringBuilder sb = new StringBuilder();
62         sb.append(
63                 formatComment(moves.nullMove())
64         );
65         DetailMove prevMove = null;
66         for (DetailMove move : moves.getMoves()) {
67             sb.append(
68                     formatSan(move, prevMove)
69             ).append(
70                     PGN_SPACE
71             ).append(
72                     formatAddition(move)
73             );
74             prevMove = move;
75         }
76         return sb.toString().trim();
77     }
78
79     private String formatSan(Move move, Move prevMove) {
80         return move.ply() % 2 != 0
81                 ? whiteFormat(move)
82                 : blackFormat(move, prevMove);
83     }
84
85     private String whiteFormat(Move move) {
86         return String.format(
87                 WHITE_MOVE_FORMAT, move.ply() / 2 + 1, move.move()
88         );
89     }
90
91     private String blackFormat(Move move, Move prevMove) {
92         if (prevMove != null && prevMove instanceof DetailMove) {
93             DetailMove detailMove = (DetailMove) prevMove;
94             if (detailMove.comments().size() > 0 || detailMove.variations().size() > 0) {
95                 return String.format(
96                         BLACK_MOVE_DOT_FORMAT, move.ply() / 2, move.move()
97                 );
98             }
99         }
100         return String.format(BLACK_MOVE_FORMAT, move.move());
101     }
102
103     private String formatAddition(DetailMove move) {
104         StringBuilder sb = new StringBuilder();
105         sb.append(
106                 formatComment(move)
107         ).append(
108                 formatVariation(move)
109         );
110         return sb.toString();
111     }
112
113     private String formatComment(DetailMove move) {
114         StringBuilder sb = new StringBuilder();
115         for (String comment : move.comments()) {
116             sb.append(
117                     String.format(COMMENT_FORMAT, comment)
118             ).append(PGN_SPACE);
119         }
120         return sb.toString();
121     }
122
123     private String formatVariation(DetailMove move) {
124         StringBuilder sb = new StringBuilder();
125         for (Variation variation : move.variations()) {
126             sb.append(
127                     String.format(VARIATION_FORMAT, format(variation))
128             ).append(PGN_SPACE);
129         }
130         return sb.toString();
131     }
132
133 }