+++ /dev/null
-/*
- * Copyright (c) 2018. Developed by Hedgecode.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.hedgecode.chess.fen;
-
-import java.util.Map;
-
-import org.hedgecode.chess.Parsers;
-import org.hedgecode.chess.position.Builder;
-import org.hedgecode.chess.position.Color;
-import org.hedgecode.chess.position.ColorPiece;
-import org.hedgecode.chess.position.ParseException;
-import org.hedgecode.chess.position.Position;
-import org.hedgecode.chess.position.Square;
-
-/**
- * Forsyth–Edwards Notation (FEN) builder.
- *
- * @author Dmitry Samoshin aka gotty
- */
-public final class FENBuilder implements Builder {
-
- private static Builder _instance = new FENBuilder();
-
- private FENBuilder() {
- }
-
- @Override
- public String build(Position position) {
- return _build(
- position
- );
- }
-
- private String _build(Position position) {
- StringBuilder sb = new StringBuilder();
- sb.append(
- _buildPosition(position)
- ).append(
- FEN.FIELDS_SEP
- ).append(
- FEN.getColor(
- position.getMove()
- )
- ).append(
- FEN.FIELDS_SEP
- ).append(
- FEN.getCastle(
- position.getCastle(Color.WHITE),
- position.getCastle(Color.BLACK)
- )
- ).append(
- FEN.FIELDS_SEP
- ).append(
- FEN.getEnPassant(
- position.getEnPassant()
- )
- ).append(
- FEN.FIELDS_SEP
- ).append(
- position.getHalfMove()
- ).append(
- FEN.FIELDS_SEP
- ).append(
- position.getFullMove()
- );
- return sb.toString();
- }
-
- private String _buildPosition(Position position) {
- StringBuilder sb = new StringBuilder();
- Map<Square, ColorPiece> squares = position.getSquares();
- for (int i = Square.getSize() - 1; i >= 0; --i) {
- int empty = 0;
- for (int j = 0; j < Square.getSize(); ++j) {
- Square square = Square.getSquare(j, i);
- ColorPiece piece = squares.get(square);
- if (piece != null) {
- if (empty > 0)
- sb.append(empty);
- sb.append(
- FEN.getFenPiece(piece)
- );
- empty = 0;
- } else {
- ++empty;
- }
- }
- if (empty > 0)
- sb.append(empty);
- if (i > 0)
- sb.append(FEN.LINE_SEP);
- }
- return sb.toString();
- }
-
- public static Builder getInstance() {
- return _instance;
- }
-
-
- public static void main(String[] args) throws ParseException {
-
- Position position = Parsers.FEN.parser().parse(
- "r7/7p/8/8/8/8/PPPPPP2/1N1Q2NR w KQkq a3 1 20"
- );
- System.out.println(
- getInstance().build(position)
- );
-
- }
-
-}