class BCDice::Randomizer

乱数生成器

Constants

DetailedRandResult

実行したダイスロールの詳細 @!attribute [rw] kind

@return [Symbol]

@!attribute [rw] sides

@return [Integer] ダイスロールしたダイスの面数

@!attribute [rw] value

@return [Integer] 値
UPPER_LIMIT_DICE_SIDES
UPPER_LIMIT_DICE_TIMES
UPPER_LIMIT_RANDS

Attributes

detailed_rand_results[R]

@return [Array<DetailedRandResult>]

rand_results[R]

@return [Array<Array<(Integer, Integer)>>] ダイスの出目一覧

Public Class Methods

new() click to toggle source
# File lib/bcdice/randomizer.rb, line 11
def initialize
  @rand_results = []
  @detailed_rand_results = []
end

Public Instance Methods

roll_barabara(times, sides) click to toggle source

複数個のダイスを振る

@param times [Integer] 振るダイスの個数 @param sides [Integer] ダイスの面数 @return [Array<Integer>] ダイスの出目一覧

# File lib/bcdice/randomizer.rb, line 36
def roll_barabara(times, sides)
  if @rand_results.size + times > UPPER_LIMIT_RANDS
    raise TooManyRandsError
  end

  if times <= 0 || times > UPPER_LIMIT_DICE_TIMES
    return []
  end

  Array.new(times) { roll_once(sides) }
end
roll_d66(sort_type) click to toggle source

D66のダイスロールを行う @param sort_type [Symbol] BCDice::D66SortType @return [Integer]

# File lib/bcdice/randomizer.rb, line 106
def roll_d66(sort_type)
  dice_list = Array.new(2) { roll_once(6) }

  case sort_type
  when D66SortType::ASC
    dice_list.sort!
  when D66SortType::DESC
    dice_list.sort!.reverse!
  end

  return dice_list[0] * 10 + dice_list[1]
end
roll_d9() click to toggle source

d10を0~9として扱うダイスロール @return [Integer] 0以上9以下の整数

# File lib/bcdice/randomizer.rb, line 96
def roll_d9()
  dice = rand_inner(10) - 1

  push_to_detail(:d9, 10, dice)
  return dice
end
roll_index(sides) click to toggle source

ダイス表などでindexを参照する用のダイスロール @param sides [Integer] @return [Integer] 0以上 sides 未満の整数

# File lib/bcdice/randomizer.rb, line 75
def roll_index(sides)
  roll_once(sides) - 1
end
roll_once(sides) click to toggle source

1回だけダイスロールを行う

@param sides [Integer] ダイスの面数 @return [Integer] 1以上 sides 以下の値のいずれか

# File lib/bcdice/randomizer.rb, line 61
def roll_once(sides)
  if sides <= 0 || sides > UPPER_LIMIT_DICE_SIDES
    return 0
  end

  dice = rand_inner(sides)
  push_to_detail(:normal, sides, dice)

  return dice
end
roll_sum(times, sides) click to toggle source

複数個のダイスを振って、その合計を求める

@param times [Integer] 振るダイスの個数 @param sides [Integer] ダイスの面数 @return [Integer] 出目の合計

# File lib/bcdice/randomizer.rb, line 53
def roll_sum(times, sides)
  roll_barabara(times, sides).sum()
end
roll_tens_d10() click to toggle source

十の位をd10を使って決定するためのダイスロール @return [Integer] 0以上90以下で10の倍数となる整数

# File lib/bcdice/randomizer.rb, line 81
def roll_tens_d10()
  # rand_innerの戻り値を10倍すればすむ話なのだが、既存のテストとの互換性の為に処理をする
  dice = rand_inner(10)
  if dice == 10
    dice = 0
  end

  ret = dice * 10

  push_to_detail(:tens_d10, 10, ret)
  return ret
end

Private Instance Methods

push_to_detail(kind, sides, value) click to toggle source

@param [Symbol] kind @param [Integer] sides @param [Integer] value

# File lib/bcdice/randomizer.rb, line 144
def push_to_detail(kind, sides, value)
  detail = DetailedRandResult.new(kind, sides, value)
  @detailed_rand_results.push(detail)
end
rand_inner(sides) click to toggle source

@param sides [Integer] @return [Integer] 1以上sides以下の整数

# File lib/bcdice/randomizer.rb, line 123
def rand_inner(sides)
  if @rand_results.size >= UPPER_LIMIT_RANDS
    raise TooManyRandsError
  end

  dice = random(sides)

  @rand_results << [dice, sides]
  return dice
end
random(sides) click to toggle source

モックで上書きする用 @param sides [Integer] @return [Integer] 1以上sides以下の整数

# File lib/bcdice/randomizer.rb, line 137
def random(sides)
  Kernel.rand(sides) + 1
end