Class: Sashite::Feen::Position::PiecePlacement

Inherits:
Object
  • Object
show all
Defined in:
lib/sashite/feen/position/piece_placement.rb

Overview

Represents the Piece Placement field (Field 1).

Encapsulates board occupancy as segments of placement tokens with preserved separator groups for multi-dimensional boards.

Examples:

pp = PiecePlacement.new(
  segments: [[<Epin r>, 2, <Epin k>], [8]],
  separators: ["/"]
)
pp.to_s  # => "r2k/8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments:, separators:) ⇒ PiecePlacement

Creates a new PiecePlacement instance.

Parameters:

  • segments (Array<Array>)

    Segments of placement tokens

  • separators (Array<String>)

    Separators between segments



30
31
32
33
34
35
# File 'lib/sashite/feen/position/piece_placement.rb', line 30

def initialize(segments:, separators:)
  @segments = segments
  @separators = separators

  freeze
end

Instance Attribute Details

#segmentsArray<Array> (readonly)

Returns Segments containing Integer (empty count) or Epin::Identifier.

Returns:

  • (Array<Array>)

    Segments containing Integer (empty count) or Epin::Identifier



21
22
23
# File 'lib/sashite/feen/position/piece_placement.rb', line 21

def segments
  @segments
end

#separatorsArray<String> (readonly)

Returns Separator strings between segments.

Returns:

  • (Array<String>)

    Separator strings between segments



24
25
26
# File 'lib/sashite/feen/position/piece_placement.rb', line 24

def separators
  @separators
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Checks equality with another PiecePlacement.

Parameters:

  • other (Object)

    The object to compare

Returns:

  • (Boolean)

    true if equal



70
71
72
73
74
# File 'lib/sashite/feen/position/piece_placement.rb', line 70

def ==(other)
  return false unless self.class === other

  segments == other.segments && separators == other.separators
end

#each_segment {|segment| ... } ⇒ Enumerator, self

Iterates over each segment.

Yield Parameters:

  • segment (Array)

    A segment of placement tokens

Returns:

  • (Enumerator, self)


41
42
43
44
45
46
# File 'lib/sashite/feen/position/piece_placement.rb', line 41

def each_segment(&block)
  return segments.each unless block

  segments.each(&block)
  self
end

#hashInteger

Returns a hash code.

Returns:

  • (Integer)

    Hash code



81
82
83
# File 'lib/sashite/feen/position/piece_placement.rb', line 81

def hash
  [segments, separators].hash
end

#to_aArray

Returns all tokens as a flat array.

Returns:

  • (Array)

    All placement tokens



51
52
53
# File 'lib/sashite/feen/position/piece_placement.rb', line 51

def to_a
  segments.flatten
end

#to_sString

Returns the canonical string representation.

Returns:

  • (String)

    The piece placement string



58
59
60
61
62
63
64
# File 'lib/sashite/feen/position/piece_placement.rb', line 58

def to_s
  segments.map { |segment| segment_to_s(segment) }
          .zip(separators)
          .flatten
          .compact
          .join
end