Class: Qi

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(captures_hash, squares_hash, turns, **state) ⇒ Qi

Returns a new instance of Qi.

Examples:

captures = Hash.new(0)
north_captures = %w[r r b g g g g s n n n n p p p p p p p p p p p p p p p p p]
south_captures = %w[S]
(north_captures + south_captures).each { |piece| captures[piece] += 1 }
squares = { 3 => "s", 4 => "k", 5 => "s", 22 => "+P", 43 => "+B" }
Qi.new(captures, squares, [0, 1])

Parameters:

  • captures_hash (Hash<Object, Integer>)

    a hash of captured pieces

  • squares_hash (Hash<Object, Object>)

    A hash where the keys represent square identifiers and the values represent the piece that will occupy each square. Both the keys and values can be any type of Ruby object, such as integers, strings, symbols, etc.

  • turns (Array<Object>)

    a rotation of turns

  • state (Hash<Symbol, Object>)

    a hash of game states



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_hashObject (readonly)

Returns the value of attribute captures_hash.



# File 'lib/qi.rb', line 7

#squares_hashObject (readonly)

Returns the value of attribute squares_hash.



# File 'lib/qi.rb', line 12

#stateObject (readonly)

Returns the value of attribute state.



# File 'lib/qi.rb', line 19

#turnsObject (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_arrayArray<Object>

Return an array of captures containing piece names.

Examples:

["S", "b", "g", "g", "g", "g", "n", "n", "n", "n", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "r", "r", "s"]

Returns:

  • (Array<Object>)

    an array of captures



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.

Examples:

qi0.commit([], [], { D43: nil, B13: "+B" }, in_check: true)

Parameters:

  • add_captures_array (Array<Object>)

    an array of pieces to be added to captures

  • del_captures_array (Array<Object>)

    an array of pieces to be deleted from captures

  • edit_squares_hash (Hash<Object, Object>)

    A hash where the keys represent square identifiers and the values represent the piece that will occupy each square. Both the keys and values can be any type of Ruby object, such as integers, strings, symbols, etc.

  • state (Hash<Symbol, Object>)

    a hash of new game states

Returns:

  • (Qi)

    a new Qi object representing the updated game state



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.

Parameters:

  • other (Qi)

    another Qi object

Returns:

  • (Boolean)

    returns true if the captures_hash, squares_hash, turn, and state of both Qi objects are equal, false otherwise



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

#turnObject

Get the current turn.

Returns:

  • (Object)

    the current turn



100
101
102
# File 'lib/qi.rb', line 100

def turn
  turns.fetch(0)
end