Class: Qi
- Inherits:
-
Object
- Object
- Qi
- Defined in:
- lib/qi.rb
Overview
The Qi class provides a consistent representation of a game state and supports changes in the game state through the commit method. It is designed to be used in board games such as chess, makruk, shogi, xiangqi.
Instance Attribute Summary collapse
-
#captures_hash ⇒ Object
readonly
Returns the value of attribute captures_hash.
-
#squares_hash ⇒ Object
readonly
Returns the value of attribute squares_hash.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#turns ⇒ Object
readonly
Returns the value of attribute turns.
Instance Method Summary collapse
-
#captures_array ⇒ Array<Object>
Return an array of captures containing piece names.
-
#commit(add_captures_array, del_captures_array, edit_squares_hash, **state) ⇒ Qi
Commit a change to the game state and return a new Qi object.
-
#eql?(other) ⇒ Boolean
(also: #==)
Check if the current Qi object is equal to another Qi object.
-
#initialize(captures_hash, squares_hash, turns, **state) ⇒ Qi
constructor
A new instance of Qi.
-
#turn ⇒ Object
Get the current turn.
Constructor Details
#initialize(captures_hash, squares_hash, turns, **state) ⇒ Qi
Returns a new instance of Qi.
45 46 47 48 49 50 51 52 |
# File 'lib/qi.rb', line 45 def initialize(captures_hash, squares_hash, turns, **state) @captures_hash = ::Hash.new(0).merge(captures_hash.select { |_, v| v > 0 }) @squares_hash = squares_hash.compact @turns = turns @state = state.transform_keys(&:to_sym) freeze end |
Instance Attribute Details
#captures_hash ⇒ Object (readonly)
Returns the value of attribute captures_hash.
|
# File 'lib/qi.rb', line 7
|
#squares_hash ⇒ Object (readonly)
Returns the value of attribute squares_hash.
|
# File 'lib/qi.rb', line 12
|
#state ⇒ Object (readonly)
Returns the value of attribute state.
|
# File 'lib/qi.rb', line 19
|
#turns ⇒ Object (readonly)
Returns the value of attribute turns.
29 |
# File 'lib/qi.rb', line 29 attr_reader :captures_hash, :squares_hash, :state, :turns |
Instance Method Details
#captures_array ⇒ Array<Object>
Return an array of captures containing piece names.
59 60 61 |
# File 'lib/qi.rb', line 59 def captures_array captures_hash.flat_map { |piece, count| ::Array.new(count, piece) }.sort end |
#commit(add_captures_array, del_captures_array, edit_squares_hash, **state) ⇒ Qi
Commit a change to the game state and return a new Qi object.
74 75 76 77 78 79 80 81 |
# File 'lib/qi.rb', line 74 def commit(add_captures_array, del_captures_array, edit_squares_hash, **state) self.class.new( edit_captures_hash(add_captures_array.compact, del_captures_array.compact, **captures_hash), squares_hash.merge(edit_squares_hash), turns.rotate, **state ) end |
#eql?(other) ⇒ Boolean Also known as: ==
Check if the current Qi object is equal to another Qi object.
87 88 89 90 91 92 93 94 |
# File 'lib/qi.rb', line 87 def eql?(other) return false unless other.captures_hash == captures_hash return false unless other.squares_hash == squares_hash return false unless other.turn == turn return false unless other.state == state true end |
#turn ⇒ Object
Get the current turn.
100 101 102 |
# File 'lib/qi.rb', line 100 def turn turns.fetch(0) end |