Module: Feen
- Defined in:
- lib/feen.rb,
lib/feen/dumper.rb,
lib/feen/parser.rb,
lib/feen/dumper/style_turn.rb,
lib/feen/parser/style_turn.rb,
lib/feen/dumper/pieces_in_hand.rb,
lib/feen/parser/pieces_in_hand.rb,
lib/feen/dumper/piece_placement.rb,
lib/feen/parser/piece_placement.rb
Overview
This module provides a Ruby interface for data serialization and deserialization in FEEN format.
FEEN (Forsyth–Edwards Enhanced Notation) is a compact, canonical, and rule-agnostic textual format for representing static board positions in two-player piece-placement games.
Defined Under Namespace
Class Method Summary collapse
-
.dump(piece_placement:, pieces_in_hand:, style_turn:) ⇒ String
Dumps position components into a FEEN string.
-
.parse(feen_string) ⇒ Hash
Parses a FEEN string into position components.
-
.safe_parse(feen_string) ⇒ Hash?
Safely parses a FEEN string into position components without raising exceptions.
-
.valid?(feen_string) ⇒ Boolean
Validates if the given string is a valid and canonical FEEN string.
Class Method Details
.dump(piece_placement:, pieces_in_hand:, style_turn:) ⇒ String
Dumps position components into a FEEN string.
42 43 44 |
# File 'lib/feen.rb', line 42 def self.dump(piece_placement:, pieces_in_hand:, style_turn:) Dumper.dump(piece_placement:, pieces_in_hand:, style_turn:) end |
.parse(feen_string) ⇒ Hash
Parses a FEEN string into position components.
71 72 73 |
# File 'lib/feen.rb', line 71 def self.parse(feen_string) Parser.parse(feen_string) end |
.safe_parse(feen_string) ⇒ Hash?
Safely parses a FEEN string into position components without raising exceptions.
This method works like ‘parse` but returns nil instead of raising an exception if the FEEN string is invalid.
90 91 92 |
# File 'lib/feen.rb', line 90 def self.safe_parse(feen_string) Parser.safe_parse(feen_string) end |
.valid?(feen_string) ⇒ Boolean
Validates if the given string is a valid and canonical FEEN string
This method performs a complete validation in two steps:
-
Syntax check: Verifies the string can be parsed as FEEN
-
Canonicity check: Ensures the string is in canonical form by comparing it with a freshly generated FEEN string created from its parsed components
This approach guarantees that the string not only follows FEEN syntax but is also in its most compact, canonical representation.
According to FEEN v1.0.0 specification:
-
Pieces in hand may include PNN modifiers (prefixes and suffixes)
-
Pieces in hand must be sorted canonically according to the sorting algorithm:
-
By player (uppercase/lowercase separated by ‘/’)
-
By quantity (descending)
-
By base letter (ascending)
-
By prefix (specific order: ‘-’, ‘+’, then no prefix)
-
By suffix (specific order: no suffix, then “‘”)
-
-
Style identifiers must follow SNN specification with semantic casing
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/feen.rb', line 128 def self.valid?(feen_string) # First check: Basic syntax validation parsed_data = safe_parse(feen_string) return false if parsed_data.nil? # Second check: Canonicity validation through round-trip conversion # Generate a fresh FEEN string from the parsed data canonical_feen = dump(**parsed_data) # Compare the canonical form with the original string canonical_feen == feen_string end |