Class: BCDice::DiceTable::ChainTable

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/dice_table/chain_table.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type, items) ⇒ ChainTable

Returns a new instance of ChainTable.

Parameters:

  • name (String)

    表の名前

  • type (String)

    項目を選ぶときのダイスロールの方法 '1D6'など

  • items (Array<String, #roll>)

    表の項目の配列



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bcdice/dice_table/chain_table.rb', line 9

def initialize(name, type, items)
  @name = name
  @items = items.freeze

  m = /(\d+)D(\d+)/i.match(type)
  unless m
    raise ArgumentError, "Unexpected table type: #{type}"
  end

  @times = m[1].to_i
  @sides = m[2].to_i
end

Instance Method Details

#roll(randomizer) ⇒ String

表を振る

Parameters:

  • randomizer (#roll_sum)

    ランダマイザ

Returns:

  • (String)

    結果



25
26
27
28
29
30
31
32
# File 'lib/bcdice/dice_table/chain_table.rb', line 25

def roll(randomizer)
  value = randomizer.roll_sum(@times, @sides)
  index = value - @times
  chosen = @items[index]
  chosen = chosen.roll(randomizer) if chosen.respond_to?(:roll)

  return RollResult.new(@name, value, chosen)
end