Class: Sashite::Feen::Placement
- Inherits:
-
Object
- Object
- Sashite::Feen::Placement
- Defined in:
- lib/sashite/feen/placement.rb
Overview
Immutable representation of board piece placement.
Stores board configuration as a flat array of ranks with explicit separators, allowing representation of any valid FEEN structure including highly irregular multi-dimensional boards.
This design supports complete flexibility:
-
Any number of dimensions (1D to nD)
-
Irregular board shapes (different rank sizes)
-
Arbitrary separator patterns (different separator lengths)
Instance Attribute Summary collapse
-
#dimension ⇒ Integer
readonly
Board dimensionality Calculated as: 1 + (maximum consecutive “/” characters in any separator) Examples: - No separators → 1D - Only “/” → 2D - At least one “//” → 3D - At least one “///” → 4D.
-
#ranks ⇒ Array<Array>
readonly
Flat array of all ranks Each rank is an array containing piece objects and/or nils.
-
#separators ⇒ Array<String>
readonly
Separators between consecutive ranks separators is the separator between ranks and ranks Each separator is a string of one or more “/” characters Always has length = ranks.length - 1 (or empty for single rank).
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare two placements for equality.
-
#all_pieces ⇒ Array
Get all pieces from all ranks (flattened).
-
#hash ⇒ Integer
Generate hash code for placement.
-
#initialize(ranks, separators = [], dimension = nil) ⇒ Placement
constructor
Create a new immutable Placement object.
-
#inspect ⇒ String
Get a human-readable representation of the placement.
-
#one_dimensional? ⇒ Boolean
Check if the board is 1-dimensional (single rank, no separators).
-
#rank_count ⇒ Integer
Get total number of ranks across all dimensions.
-
#to_a ⇒ Array
Convert placement to array representation based on dimensionality.
-
#to_s ⇒ String
Convert placement to its FEEN string representation.
-
#total_squares ⇒ Integer
Get total number of squares across all ranks.
Constructor Details
#initialize(ranks, separators = [], dimension = nil) ⇒ Placement
Create a new immutable Placement object.
86 87 88 89 90 91 92 93 |
# File 'lib/sashite/feen/placement.rb', line 86 def initialize(ranks, separators = [], dimension = nil) @ranks = deep_freeze_ranks(ranks) @separators = separators.freeze @dimension = dimension || calculate_dimension(separators) validate! freeze end |
Instance Attribute Details
#dimension ⇒ Integer (readonly)
Returns Board dimensionality Calculated as: 1 + (maximum consecutive “/” characters in any separator) Examples:
- No separators → 1D
- Only "/" → 2D
- At least one "//" → 3D
- At least one "///" → 4D.
59 60 61 |
# File 'lib/sashite/feen/placement.rb', line 59 def dimension @dimension end |
#ranks ⇒ Array<Array> (readonly)
Returns Flat array of all ranks Each rank is an array containing piece objects and/or nils.
44 45 46 |
# File 'lib/sashite/feen/placement.rb', line 44 def ranks @ranks end |
#separators ⇒ Array<String> (readonly)
Returns Separators between consecutive ranks separators is the separator between ranks and ranks Each separator is a string of one or more “/” characters Always has length = ranks.length - 1 (or empty for single rank).
50 51 52 |
# File 'lib/sashite/feen/placement.rb', line 50 def separators @separators end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compare two placements for equality.
Two placements are equal if they have the same ranks, separators, and dimension.
187 188 189 190 191 192 |
# File 'lib/sashite/feen/placement.rb', line 187 def ==(other) other.is_a?(Placement) && ranks == other.ranks && separators == other.separators && dimension == other.dimension end |
#all_pieces ⇒ Array
Get all pieces from all ranks (flattened).
121 122 123 |
# File 'lib/sashite/feen/placement.rb', line 121 def all_pieces @ranks.flatten.compact end |
#hash ⇒ Integer
Generate hash code for placement.
Ensures that equal placements have equal hash codes for use in hash-based collections.
205 206 207 |
# File 'lib/sashite/feen/placement.rb', line 205 def hash [ranks, separators, dimension].hash end |
#inspect ⇒ String
Get a human-readable representation of the placement.
216 217 218 |
# File 'lib/sashite/feen/placement.rb', line 216 def inspect "#<#{self.class.name} dimension=#{dimension} ranks=#{rank_count} separators=#{separators.size}>" end |
#one_dimensional? ⇒ Boolean
Check if the board is 1-dimensional (single rank, no separators).
111 112 113 |
# File 'lib/sashite/feen/placement.rb', line 111 def one_dimensional? @dimension == 1 end |
#rank_count ⇒ Integer
Get total number of ranks across all dimensions.
101 102 103 |
# File 'lib/sashite/feen/placement.rb', line 101 def rank_count @ranks.size end |
#to_a ⇒ Array
Convert placement to array representation based on dimensionality.
The returned structure depends on board dimension:
-
1D boards: Returns single rank array (or empty array if no ranks)
-
2D+ boards: Returns array of ranks
158 159 160 161 162 |
# File 'lib/sashite/feen/placement.rb', line 158 def to_a return ranks.first || [] if one_dimensional? ranks end |
#to_s ⇒ String
Convert placement to its FEEN string representation.
Delegates to Dumper::PiecePlacement for canonical serialization.
173 174 175 |
# File 'lib/sashite/feen/placement.rb', line 173 def to_s Dumper::PiecePlacement.dump(self) end |
#total_squares ⇒ Integer
Get total number of squares across all ranks.
131 132 133 |
# File 'lib/sashite/feen/placement.rb', line 131 def total_squares @ranks.sum(&:size) end |