FenParser

dicechess.engine.domain.FenParser
object FenParser

Parser and serialiser for Forsyth-Edwards Notation (FEN).

FEN encodes a complete chess position in a single ASCII string consisting of six space-separated fields:

<piece placement> <active color> <castling rights> <en-passant square> <half-move clock> <full-move number>

Example (starting position):

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

Dice Chess Extension (7th field)

This parser also supports an optional 7th field that stores the current turn's dice pool:

rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1 pnb

The 7th field is a string of piece letters (e.g. "PNB" for White or "pnb" for Black = dice pool [1, 2, 3]), never longer than GameFlags.DiceSlots because a turn rolls three dice. It is absent ("-") or omitted when no dice have been rolled yet. serialize appends it automatically when the pool is non-empty.

Both parse and serialize are inverses of each other for any valid FEN string.

Attributes

Source
FenParser.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
FenParser.type

Members list

Value members

Concrete methods

def parse(fen: String): Either[String, GameState]

Parses a FEN string into a dicechess.engine.domain.GameState.

Parses a FEN string into a dicechess.engine.domain.GameState.

Validates the number of fields, the board layout (exactly 8 ranks, 8 files each), and each piece character. The half-move clock and full-move number default to 0 and 1 respectively when absent (short FEN).

Every field is also checked against what dicechess.engine.domain.GameState can actually hold, so a Right never carries a value that differs from what was parsed: at most seven fields, at most GameFlags.DiceSlots dice, a half-move clock within GameFlags.MaxHalfMoveClock, and numeric fields that fit an Int. Input past any of those bounds is a Left rather than a silently truncated state (#551).

Value parameters

fen

a FEN string with 4 to 7 space-separated fields

Attributes

Returns

Right(state) on success, or Left(errorMessage) describing the first parse failure

Source
FenParser.scala
def serialize(state: GameState): String

Serialises a dicechess.engine.domain.GameState back to a FEN string.

Serialises a dicechess.engine.domain.GameState back to a FEN string.

Produces all six standard FEN fields, plus an optional 7th field if the dice pool is non-empty. Piece placement is written rank-8-first (as required by the standard). This method is the inverse of parse: parse(serialize(s)) always yields Right(s) for a valid state.

Value parameters

state

the game state to encode

Attributes

Returns

a valid FEN string representing state

Source
FenParser.scala

Concrete fields

val InitialPosition: String

FEN string for the standard chess starting position.

FEN string for the standard chess starting position.

Attributes

Source
FenParser.scala