Class: Sashite::Feen::Position::Hand

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

Overview

Represents a single player’s hand.

Contains a collection of hand items (piece + count pairs).

Examples:

hand = Hand.new([{ piece: <Epin P>, count: 2 }, { piece: <Epin N>, count: 1 }])
hand.to_s  # => "2PN"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ Hand

Creates a new Hand instance.

Parameters:

  • items (Array<Hash>)

    Hand items



81
82
83
84
85
# File 'lib/sashite/feen/position/hands.rb', line 81

def initialize(items)
  @items = items

  freeze
end

Instance Attribute Details

#itemsArray<Hash> (readonly)

Returns Hand items with :piece and :count keys.

Returns:

  • (Array<Hash>)

    Hand items with :piece and :count keys



76
77
78
# File 'lib/sashite/feen/position/hands.rb', line 76

def items
  @items
end

Instance Method Details

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

Checks equality with another Hand.

Parameters:

  • other (Object)

    The object to compare

Returns:

  • (Boolean)

    true if equal



131
132
133
134
135
# File 'lib/sashite/feen/position/hands.rb', line 131

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

  items == other.items
end

#each {|item| ... } ⇒ Enumerator, self

Iterates over each hand item.

Yield Parameters:

  • item (Hash)

    A hand item with :piece and :count

Returns:

  • (Enumerator, self)


105
106
107
108
109
110
# File 'lib/sashite/feen/position/hands.rb', line 105

def each(&block)
  return items.each unless block

  items.each(&block)
  self
end

#empty?Boolean

Returns true if the hand is empty.

Returns:

  • (Boolean)


90
91
92
# File 'lib/sashite/feen/position/hands.rb', line 90

def empty?
  items.empty?
end

#hashInteger

Returns a hash code.

Returns:

  • (Integer)

    Hash code



142
143
144
# File 'lib/sashite/feen/position/hands.rb', line 142

def hash
  items.hash
end

#sizeInteger

Returns the number of distinct piece types.

Returns:

  • (Integer)


97
98
99
# File 'lib/sashite/feen/position/hands.rb', line 97

def size
  items.size
end

#to_sString

Returns the canonical string representation.

Items are sorted according to canonical ordering:

  1. By multiplicity — descending (larger counts first)

  2. By base letter — case-insensitive alphabetical order

  3. By letter case — uppercase before lowercase

  4. By state modifier — - before ‘+` before none

  5. By terminal marker — absent before present

  6. By derivation marker — absent before present

Returns:

  • (String)

    The hand string in canonical order



123
124
125
# File 'lib/sashite/feen/position/hands.rb', line 123

def to_s
  canonical_items.map { |item| hand_item_to_s(item) }.join
end