2 * Copyright (c) 2018-2020. 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;
21 import org.hedgecode.chess.Builder;
22 import org.hedgecode.chess.Parsers;
23 import org.hedgecode.chess.ParseException;
24 import org.hedgecode.chess.position.Color;
25 import org.hedgecode.chess.position.ColorPiece;
26 import org.hedgecode.chess.position.Position;
27 import org.hedgecode.chess.position.Square;
30 * Forsyth–Edwards Notation (FEN) builder.
32 * @author Dmitry Samoshin aka gotty
34 public final class FENBuilder implements Builder {
36 private static Builder _instance = new FENBuilder();
38 private FENBuilder() {
42 public String build(Position position) {
48 private String _build(Position position) {
49 StringBuilder sb = new StringBuilder();
51 _buildPosition(position)
62 position.getCastle(Color.WHITE),
63 position.getCastle(Color.BLACK)
69 position.getEnPassant()
74 position.getHalfMove()
78 position.getFullMove()
83 private String _buildPosition(Position position) {
84 StringBuilder sb = new StringBuilder();
85 Map<Square, ColorPiece> squares = position.getSquares();
86 for (int i = Square.getSize() - 1; i >= 0; --i) {
88 for (int j = 0; j < Square.getSize(); ++j) {
89 Square square = Square.getSquare(j, i);
90 ColorPiece piece = squares.get(square);
95 FEN.getFenPiece(piece)
105 sb.append(FEN.LINE_SEP);
107 return sb.toString();
110 public static Builder getInstance() {
115 public static void main(String[] args) throws ParseException {
117 Position position = Parsers.FEN.parser().parse(
118 "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq a3 1 20"
121 getInstance().build(position)