Skip to content
Snippets Groups Projects
Commit d42c2a0e authored by ulif's avatar ulif
Browse files

Do working version of choice().

The new real dice random source now works on a very plain level.
parent f802c1aa
No related branches found
No related tags found
No related merge requests found
......@@ -133,13 +133,13 @@ class RealDiceRandomSource(object):
if 6 ** num_rolls < len(sequence):
print("Warning: entropy is reduced!")
print(
"You have to roll %s dice (or a single dice %s times)." % (
"Please roll %s dice (or a single dice %s times)." % (
num_rolls, num_rolls))
result = 0
for i in range(num_rolls):
for i in range(num_rolls, 0, -1):
rolled = None
while rolled not in ["1", "2", "3", "4", "5", "6"]:
rolled = input_func(
"Please number shows dice number %s? " % (i + 1))
result += ((6 ** i) * int(rolled) - 1)
"What number shows dice number %s? " % (num_rolls - i + 1))
result += ((6 ** (i - 1)) * (int(rolled) - 1))
return sequence[result]
......@@ -174,7 +174,7 @@ class TestRealDiceRandomSource(object):
self.fake_input_values(["1", "2"], monkeypatch)
src = RealDiceRandomSource(None)
sequence = list(range(6 ** 2))
expected_index = 1 * 2 - 1 # = roll_1 x roll_2 - 1
expected_index = 6 * (1 - 1) + (2 - 1) # = 6 x roll_1 + roll_2 - 1
assert src.choice(sequence) == sequence[expected_index]
def test_choice_num_of_dice_for_seq_len216(self, monkeypatch):
......@@ -182,7 +182,9 @@ class TestRealDiceRandomSource(object):
self.fake_input_values(["1", "2", "3"], monkeypatch)
src = RealDiceRandomSource(None)
sequence = list(range(6 ** 3)) # 216
expected_index = 1 * 2 * 3 - 1 # = roll_1 x roll_2 x roll_3 - 1
expected_index = 0 + 6 + 3 - 1 # = 6^2 x (roll_1 - 1)
# + 6^1 x (roll_2 - 1)
# + roll_3 - 1
assert src.choice(sequence) == sequence[expected_index]
def test_hint_if_entropy_is_decreased(self, monkeypatch, capsys):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment