From 4506747b4e44ac512589516eedca6d5e142d123d Mon Sep 17 00:00:00 2001 From: ViVi Date: Fri, 25 Jun 2021 20:37:41 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=80=E3=82=A4=E3=82=B9=E7=9B=AE=E3=81=AE?= =?UTF-8?q?=E5=81=B6=E5=A5=87=E3=81=AE=E3=81=BF=E3=82=92=E8=A6=8B=E3=82=8B?= =?UTF-8?q?=E3=83=86=E3=83=BC=E3=83=96=E3=83=AB=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/bcdice/dice_table.rb | 1 + lib/bcdice/dice_table/parity_table.rb | 34 ++++++++++++++++++++++++ test/test_parity_table.rb | 37 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 lib/bcdice/dice_table/parity_table.rb create mode 100644 test/test_parity_table.rb diff --git a/lib/bcdice/dice_table.rb b/lib/bcdice/dice_table.rb index 29b7840a9..9d925e865 100644 --- a/lib/bcdice/dice_table.rb +++ b/lib/bcdice/dice_table.rb @@ -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" diff --git a/lib/bcdice/dice_table/parity_table.rb b/lib/bcdice/dice_table/parity_table.rb new file mode 100644 index 000000000..6d4ca69ca --- /dev/null +++ b/lib/bcdice/dice_table/parity_table.rb @@ -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 diff --git a/test/test_parity_table.rb b/test/test_parity_table.rb new file mode 100644 index 000000000..cf896afa4 --- /dev/null +++ b/test/test_parity_table.rb @@ -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