-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ViVi
committed
Jun 25, 2021
1 parent
6323f62
commit 4506747
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |