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