2 * Copyright (c) 2018. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.fen;
19 import java.util.regex.Pattern;
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;
29 * Forsyth–Edwards Notation (FEN) parser.
31 * @author Dmitry Samoshin aka gotty
33 public final class FENParser implements Parser {
35 private static final String SPLIT_REGEX = "\\s+";
36 private static final String LINE_REGEX = FEN.LINE_SEP;
38 private static final Pattern PATTERN =
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()
49 private static final Pattern PATTERN =
51 "^([pPnNbBrRqQkK1-8]+/?)* [wb] [qQkK]+|- ([a-h][36])|- [0-9]+ [1-9][0-9]*$"
55 private static Parser _instance = new FENParser();
60 public boolean isValidFen(String fen) {
61 return PATTERN.matcher(fen).matches();
65 public Position parse(String string) throws ParseException {
67 throw new ParseException("parse.null.input.string");
68 if (!isValidFen(string))
69 throw new ParseException("parse.fen.invalid.string");
71 Position position = Positions.EMPTY.getPosition();
73 String[] fields = string.split(SPLIT_REGEX);
76 fields[FEN.Fields.PIECES.index()],
80 fields[FEN.Fields.MOVE.index()],
84 fields[FEN.Fields.CASTLE.index()],
88 fields[FEN.Fields.EN_PASSANT.index()],
92 fields[FEN.Fields.HALFMOVE.index()],
96 fields[FEN.Fields.FULLMOVE.index()],
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);
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) {
120 Square.getSquare(vLine, hLine)
126 FEN.getColorPiece(piece),
127 Square.getSquare(vLine, hLine)
135 private void _parseMove(String move, Position position) {
137 FEN.getColor(move.charAt(0))
141 private void _parseCastle(String castle, Position position) {
144 FEN.getCastle(Color.WHITE, castle)
148 FEN.getCastle(Color.BLACK, castle)
152 private void _parseEnPassant(String enPassant, Position position) {
153 position.setEnPassant(
154 FEN.getEnPassant(enPassant)
158 private void _parseHalfMove(String halfMove, Position position) {
159 position.setHalfMove(
160 Integer.parseInt(halfMove)
164 private void _parseFullMove(String fullMove, Position position) {
165 position.setFullMove(
166 Integer.parseInt(fullMove)
170 public static Parser getInstance() {
174 public static void main(String[] args) throws ParseException {
179 "^([pPnNbBrRqQkK1-8]+/?){8} [wb] ([qQkK]+|-) ([a-h][36]|-) [0-9]+ [1-9][0-9]*$"
182 pattern.matcher("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - a6 0 1").matches()
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"
190 System.out.println(position);