Class: Sashite::Feen::Hands
- Inherits:
-
Object
- Object
- Sashite::Feen::Hands
- Defined in:
- lib/sashite/feen/hands.rb
Overview
Immutable representation of pieces held in hand by each player.
Stores captured pieces that players hold in reserve, available for placement back onto the board in games that support drop mechanics (such as shogi, crazyhouse, etc.).
Instance Attribute Summary collapse
-
#first_player ⇒ Array
readonly
Array of pieces held by first player.
-
#second_player ⇒ Array
readonly
Array of pieces held by second player.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare two hands for equality.
-
#empty? ⇒ Boolean
Check if both hands are empty.
-
#hash ⇒ Integer
Generate hash code for hands.
-
#initialize(first_player, second_player) ⇒ Hands
constructor
Create a new immutable Hands object.
-
#to_s ⇒ String
Convert hands to their FEEN string representation.
Constructor Details
#initialize(first_player, second_player) ⇒ Hands
Create a new immutable Hands object.
32 33 34 35 36 37 |
# File 'lib/sashite/feen/hands.rb', line 32 def initialize(first_player, second_player) @first_player = first_player.sort_by(&:to_s).freeze @second_player = second_player.sort_by(&:to_s).freeze freeze end |
Instance Attribute Details
#first_player ⇒ Array (readonly)
Returns Array of pieces held by first player.
14 15 16 |
# File 'lib/sashite/feen/hands.rb', line 14 def first_player @first_player end |
#second_player ⇒ Array (readonly)
Returns Array of pieces held by second player.
17 18 19 |
# File 'lib/sashite/feen/hands.rb', line 17 def second_player @second_player end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compare two hands for equality.
64 65 66 67 68 |
# File 'lib/sashite/feen/hands.rb', line 64 def ==(other) other.is_a?(Hands) && first_player == other.first_player && second_player == other.second_player end |
#empty? ⇒ Boolean
Check if both hands are empty.
45 46 47 |
# File 'lib/sashite/feen/hands.rb', line 45 def empty? first_player.empty? && second_player.empty? end |
#hash ⇒ Integer
Generate hash code for hands.
75 76 77 |
# File 'lib/sashite/feen/hands.rb', line 75 def hash [first_player, second_player].hash end |
#to_s ⇒ String
Convert hands to their FEEN string representation.
56 57 58 |
# File 'lib/sashite/feen/hands.rb', line 56 def to_s Dumper::PiecesInHand.dump(self) end |