Class: FEEN::Dumper::Square

Inherits:
Object
  • Object
show all
Defined in:
lib/feen/dumper/square.rb

Overview

The square class.

Examples:

Dump an empty board of Xiangqi

Board.new([10, 9]).to_s # => "9/9/9/9/9/9/9/9/9/9"

Dump the Xiangqi starting position board

Board.new(
  [10, 9],
  {
     0 => "",
     1 => "",
     2 => "",
     3 => "",
     4 => "",
     5 => "",
     6 => "",
     7 => "",
     8 => "",
    19 => "",
    25 => "",
    27 => "",
    29 => "",
    31 => "",
    33 => "",
    35 => "",
    54 => "",
    56 => "",
    58 => "",
    60 => "",
    62 => "",
    64 => "",
    70 => "",
    81 => "",
    82 => "",
    83 => "",
    84 => "",
    85 => "",
    86 => "",
    87 => "",
    88 => "",
    89 => ""
  }
).to_s # => "車,馬,象,士,將,士,象,馬,車/9/1,砲,5,砲,1/卒,1,卒,1,卒,1,卒,1,卒/9/9/兵,1,兵,1,兵,1,兵,1,兵/1,炮,5,炮,1/9/俥,傌,相,仕,帥,仕,相,傌,俥"

Instance Method Summary collapse

Constructor Details

#initialize(indexes, board) ⇒ Square

Returns a new instance of Square.

Parameters:

  • indexes (Array)

    The shape of the board.

  • board (Hash)

    The index of each piece on the board.



51
52
53
54
# File 'lib/feen/dumper/square.rb', line 51

def initialize(indexes, board)
  @indexes = indexes
  @squares = Array.new(length) { |i| board.fetch(i, nil) }
end

Instance Method Details

#to_sString

Returns The string representing the board.

Returns:

  • (String)

    The string representing the board.



57
58
59
# File 'lib/feen/dumper/square.rb', line 57

def to_s
  unflatten(@squares, @indexes)
end