4162a554e4683a9b228205076062cca4cfbc28df
[chesshog.git] / src / main / java / org / hedgecode / chess / fen / FENParser.java
1 /*
2  * Copyright (c) 2018. 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.fen;
18
19 import java.util.regex.Pattern;
20
21 import org.hedgecode.chess.position.Color;
22 import org.hedgecode.chess.position.ParseException;
23 import org.hedgecode.chess.position.Parser;
24 import org.hedgecode.chess.position.Position;
25 import org.hedgecode.chess.position.Positions;
26 import org.hedgecode.chess.position.Square;
27
28 /**
29  * Forsyth–Edwards Notation (FEN) parser.
30  *
31  * @author Dmitry Samoshin aka gotty
32  */
33 public final class FENParser implements Parser {
34
35     private static final String SPLIT_REGEX = "\\s+";
36     private static final String LINE_REGEX = FEN.LINE_SEP;
37
38     private static final Pattern PATTERN =
39             Pattern.compile(
40                     String.format("^(%s){%d} (%s) (%s) (%s) (%s) (%s)$",
41                             FEN.Fields.PIECES.regex(), Square.getSize(),
42                             FEN.Fields.MOVE.regex(),
43                             FEN.Fields.CASTLE.regex(), FEN.Fields.EN_PASSANT.regex(),
44                             FEN.Fields.HALFMOVE.regex(), FEN.Fields.FULLMOVE.regex()
45                     )
46             );
47
48 /*
49     private static final Pattern PATTERN =
50             Pattern.compile(
51                     "^([pPnNbBrRqQkK1-8]+/?)* [wb] [qQkK]+|- ([a-h][36])|- [0-9]+ [1-9][0-9]*$"
52             );
53 */
54
55     private static Parser _instance = new FENParser();
56
57     private FENParser() {
58     }
59
60     public boolean isValidFen(String fen) {
61         return PATTERN.matcher(fen).matches();
62     }
63
64     @Override
65     public Position parse(String string) throws ParseException {
66         if (string == null)
67             throw new ParseException("parse.null.input.string");
68         if (!isValidFen(string))
69             throw new ParseException("parse.fen.invalid.string");
70
71         Position position = Positions.EMPTY.getPosition();
72
73         String[] fields = string.split(SPLIT_REGEX);
74
75         _parsePieces(
76                 fields[FEN.Fields.PIECES.index()],
77                 position
78         );
79         _parseMove(
80                 fields[FEN.Fields.MOVE.index()],
81                 position
82         );
83         _parseCastle(
84                 fields[FEN.Fields.CASTLE.index()],
85                 position
86         );
87         _parseEnPassant(
88                 fields[FEN.Fields.EN_PASSANT.index()],
89                 position
90         );
91         _parseHalfMove(
92                 fields[FEN.Fields.HALFMOVE.index()],
93                 position
94         );
95         _parseFullMove(
96                 fields[FEN.Fields.FULLMOVE.index()],
97                 position
98         );
99         return position;
100     }
101
102     private void _parsePieces(String fen, Position position) throws ParseException {
103         String[] lines = fen.split(LINE_REGEX);
104         if (lines.length != Square.getSize())
105             throw new ParseException("parse.fen.incorrect.board");
106         for (int i = 0; i < lines.length; ++i) {
107             int hLine = lines.length - (i + 1);
108             int vLine = 0;
109             char[] pieces = lines[i].toCharArray();
110             for (char piece : pieces) {
111                 if (vLine >= Square.getSize())
112                     throw new ParseException("parse.fen.incorrect.board");
113                 if (Character.isDigit(piece)) {
114                     int num = Character.getNumericValue(piece);
115                     if (vLine + num > Square.getSize())
116                         throw new ParseException("parse.fen.incorrect.board");
117                     for (int j = 0; j < num; ++j) {
118                         position.setPiece(
119                                 null,
120                                 Square.getSquare(vLine, hLine)
121                         );
122                         vLine++;
123                     }
124                 } else {
125                     position.setPiece(
126                             FEN.getColorPiece(piece),
127                             Square.getSquare(vLine, hLine)
128                     );
129                     vLine++;
130                 }
131             }
132         }
133     }
134
135     private void _parseMove(String move, Position position) {
136         position.setMove(
137                 FEN.getColor(move.charAt(0))
138         );
139     }
140
141     private void _parseCastle(String castle, Position position) {
142         position.setCastle(
143                 Color.WHITE,
144                 FEN.getCastle(Color.WHITE, castle)
145         );
146         position.setCastle(
147                 Color.BLACK,
148                 FEN.getCastle(Color.BLACK, castle)
149         );
150     }
151
152     private void _parseEnPassant(String enPassant, Position position) {
153         position.setEnPassant(
154                 FEN.getEnPassant(enPassant)
155         );
156     }
157
158     private void _parseHalfMove(String halfMove, Position position) {
159         position.setHalfMove(
160                 Integer.parseInt(halfMove)
161         );
162     }
163
164     private void _parseFullMove(String fullMove, Position position) {
165         position.setFullMove(
166                 Integer.parseInt(fullMove)
167         );
168     }
169
170     public static Parser getInstance() {
171         return _instance;
172     }
173
174     public static void main(String[] args) throws ParseException {
175
176 /*
177         Pattern pattern =
178                 Pattern.compile(
179                         "^([pPnNbBrRqQkK1-8]+/?){8} [wb] ([qQkK]+|-) ([a-h][36]|-) [0-9]+ [1-9][0-9]*$"
180                 );
181         System.out.println(
182                 pattern.matcher("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - a6 0 1").matches()
183         );
184 */
185
186         Position position = getInstance().parse(
187                 //"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
188                 "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq a3 20 20"
189         );
190         System.out.println(position);
191     }
192
193 }