Class: Sashite::Feen::Position::Hand
- Inherits:
-
Object
- Object
- Sashite::Feen::Position::Hand
- Defined in:
- lib/sashite/feen/position/hands.rb
Overview
Represents a single player’s hand.
Contains a collection of hand items (piece + count pairs).
Instance Attribute Summary collapse
-
#items ⇒ Array<Hash>
readonly
Hand items with :piece and :count keys.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks equality with another Hand.
-
#each {|item| ... } ⇒ Enumerator, self
Iterates over each hand item.
-
#empty? ⇒ Boolean
Returns true if the hand is empty.
-
#hash ⇒ Integer
Returns a hash code.
-
#initialize(items) ⇒ Hand
constructor
Creates a new Hand instance.
-
#size ⇒ Integer
Returns the number of distinct piece types.
-
#to_s ⇒ String
Returns the canonical string representation.
Constructor Details
#initialize(items) ⇒ Hand
Creates a new Hand instance.
81 82 83 84 85 |
# File 'lib/sashite/feen/position/hands.rb', line 81 def initialize(items) @items = items freeze end |
Instance Attribute Details
#items ⇒ Array<Hash> (readonly)
Returns 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.
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.
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.
90 91 92 |
# File 'lib/sashite/feen/position/hands.rb', line 90 def empty? items.empty? end |
#hash ⇒ Integer
Returns a hash code.
142 143 144 |
# File 'lib/sashite/feen/position/hands.rb', line 142 def hash items.hash end |
#size ⇒ Integer
Returns the number of distinct piece types.
97 98 99 |
# File 'lib/sashite/feen/position/hands.rb', line 97 def size items.size end |
#to_s ⇒ String
Returns the canonical string representation.
Items are sorted according to canonical ordering:
-
By multiplicity — descending (larger counts first)
-
By base letter — case-insensitive alphabetical order
-
By letter case — uppercase before lowercase
-
By state modifier —
-before ‘+` before none -
By terminal marker — absent before present
-
By derivation marker — absent before present
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 |