Skip to content

Commit

Permalink
ダイス目の偶奇のみを見るテーブルを実装
Browse files Browse the repository at this point in the history
  • Loading branch information
ViVi committed Jun 25, 2021
1 parent 6323f62 commit 4506747
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/bcdice/dice_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "bcdice/dice_table/d66_parity_table"
require "bcdice/dice_table/d66_range_table"
require "bcdice/dice_table/d66_table"
require "bcdice/dice_table/parity_table"
require "bcdice/dice_table/range_table"
require "bcdice/dice_table/sai_fic_skill_table"
require "bcdice/dice_table/table"
34 changes: 34 additions & 0 deletions lib/bcdice/dice_table/parity_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module BCDice
module DiceTable
# 出目の偶奇による場合分け機能をもつ表
class ParityTable
# @param [String] name 表の名前
# @param [String] type 項目を選ぶときのダイスロールの方法 '1D6'など
# @param [String] odd ダイス目が奇数のときの結果
# @param [String] even ダイス目が偶数のときの結果
def initialize(name, type, odd, even)
@name = name
@odd = odd.freeze
@even = even.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

# 表を振る
# @param [BCDice] bcdice ランダマイザ
# @return [String] 結果
def roll(bcdice)
value = bcdice.roll_sum(@times, @sides)
return RollResult.new(@name, value, value.odd? ? @odd : @even)
end
end
end
end
37 changes: 37 additions & 0 deletions test/test_parity_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require "test/unit"
require "bcdice"
require "bcdice/dice_table/parity_table"

require_relative "randomizer_mock"

class TestParityTable < Test::Unit::TestCase
# ダイス目の偶奇のみを見る
def test_parity
table = BCDice::DiceTable::ParityTable.new(
"テスト",
"1D6",
"odd",
"even"
)

randomizer = RandomizerMock.new([[1, 6]])
assert_equal("テスト(1) > odd", table.roll(randomizer).to_s)

randomizer = RandomizerMock.new([[2, 6]])
assert_equal("テスト(2) > even", table.roll(randomizer).to_s)

randomizer = RandomizerMock.new([[3, 6]])
assert_equal("テスト(3) > odd", table.roll(randomizer).to_s)

randomizer = RandomizerMock.new([[4, 6]])
assert_equal("テスト(4) > even", table.roll(randomizer).to_s)

randomizer = RandomizerMock.new([[5, 6]])
assert_equal("テスト(5) > odd", table.roll(randomizer).to_s)

randomizer = RandomizerMock.new([[6, 6]])
assert_equal("テスト(6) > even", table.roll(randomizer).to_s)
end
end

0 comments on commit 4506747

Please sign in to comment.