2 * Copyright (c) 2018-2019. 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.HashMap;
22 import org.hedgecode.chess.position.Castle;
23 import org.hedgecode.chess.position.Color;
24 import org.hedgecode.chess.position.ColorPiece;
25 import org.hedgecode.chess.position.Piece;
26 import org.hedgecode.chess.position.Square;
29 * Forsyth–Edwards Notation (FEN) constants.
31 * @author Dmitry Samoshin aka gotty
33 public final class FEN {
36 PIECES (0, "[pPnNbBrRqQkK1-8]+/?"),
38 CASTLE (2, "[qQkK]+|-"),
39 EN_PASSANT (3, "[a-h][36]|-"),
40 HALFMOVE (4, "[0-9]+"),
41 FULLMOVE (5, "[1-9][0-9]*");
46 Fields(int index, String regex) {
55 public String regex() {
60 public static final char WHITE = 'w';
61 public static final char BLACK = 'b';
62 public static final char HYPHEN = '-';
64 public static final String FIELDS_SEP = " ";
65 public static final String LINE_SEP = "/";
67 public static final char WHITE_PAWN = 'P';
68 public static final char WHITE_KNIGHT = 'N';
69 public static final char WHITE_BISHOP = 'B';
70 public static final char WHITE_ROOK = 'R';
71 public static final char WHITE_QUEEN = 'Q';
72 public static final char WHITE_KING = 'K';
74 public static final char BLACK_PAWN = 'p';
75 public static final char BLACK_KNIGHT = 'n';
76 public static final char BLACK_BISHOP = 'b';
77 public static final char BLACK_ROOK = 'r';
78 public static final char BLACK_QUEEN = 'q';
79 public static final char BLACK_KING = 'k';
81 private static final Map<Color, Character> COLORS = new HashMap<Color, Character>() {
83 put(Color.WHITE, WHITE);
84 put(Color.BLACK, BLACK);
88 private static final Map<ColorPiece, Character> PIECES = new HashMap<ColorPiece, Character>() {
90 put(ColorPiece.WHITE_PAWN, WHITE_PAWN); put(ColorPiece.BLACK_PAWN, BLACK_PAWN);
91 put(ColorPiece.WHITE_KNIGHT, WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, BLACK_KNIGHT);
92 put(ColorPiece.WHITE_BISHOP, WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, BLACK_BISHOP);
93 put(ColorPiece.WHITE_ROOK, WHITE_ROOK); put(ColorPiece.BLACK_ROOK, BLACK_ROOK);
94 put(ColorPiece.WHITE_QUEEN, WHITE_QUEEN); put(ColorPiece.BLACK_QUEEN, BLACK_QUEEN);
95 put(ColorPiece.WHITE_KING, WHITE_KING); put(ColorPiece.BLACK_KING, BLACK_KING);
102 public static Color getColor(char color) {
103 for (Map.Entry<Color, Character> entry : COLORS.entrySet()) {
104 if (entry.getValue() == color)
105 return entry.getKey();
110 public static String getColor(Color color) {
111 return String.valueOf(
114 : COLORS.get(Color.WHITE)
118 public static ColorPiece getColorPiece(char piece) {
119 for (Map.Entry<ColorPiece, Character> entry : PIECES.entrySet()) {
120 if (entry.getValue() == piece)
121 return entry.getKey();
126 public static char getFenPiece(Color color, Piece piece) {
128 ColorPiece.getColorPiece(color, piece)
132 public static char getFenPiece(ColorPiece colorPiece) {
133 return PIECES.get(colorPiece);
136 public static Castle getCastle(Color color, String castles) {
138 if (castles.indexOf(HYPHEN) >= 0) {
139 castle = Castle.NONE;
141 boolean isKing = false, isQueen = false;
144 isKing = (castles.indexOf(WHITE_KING) >= 0);
145 isQueen = (castles.indexOf(WHITE_QUEEN) >= 0);
148 isKing = (castles.indexOf(BLACK_KING) >= 0);
149 isQueen = (castles.indexOf(BLACK_QUEEN) >= 0);
152 castle = isKing && isQueen ? Castle.BOTH
153 : isKing ? Castle.KING
154 : isQueen ? Castle.QUEEN
160 public static String getCastle(Castle whiteCastle, Castle blackCastle) {
161 StringBuilder sb = new StringBuilder();
162 switch (whiteCastle) {
164 sb.append(WHITE_KING);
167 sb.append(WHITE_QUEEN);
170 sb.append(WHITE_KING).append(WHITE_QUEEN);
173 switch (blackCastle) {
175 sb.append(BLACK_KING);
178 sb.append(BLACK_QUEEN);
181 sb.append(BLACK_KING).append(BLACK_QUEEN);
184 String castles = sb.toString();
185 return castles.isEmpty()
186 ? String.valueOf(HYPHEN)
190 public static Square getEnPassant(String enPassant) {
191 if (enPassant.indexOf(HYPHEN) < 0) {
192 return Square.getSquare(enPassant);
197 public static String getEnPassant(Square square){
198 return (square != null)
199 ? square.name().toLowerCase()
200 : String.valueOf(HYPHEN);