tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 1 level 4
thecoded.prof
5 months ago
62599897
ed475f62
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+92
1 changed file
expand all
collapse all
unified
split
python
oct1
level4
playThreeDiceYahtzee.py
+92
python/oct1/level4/playThreeDiceYahtzee.py
···
1
1
+
def handToDice(hand: int) -> tuple[int, int, int]:
2
2
+
"""Converts a hand to a tuple of dice."""
3
3
+
return hand // 100, (hand // 10) % 10, hand % 10
4
4
+
5
5
+
6
6
+
def diceToOrderedHand(a: int, b: int, c: int) -> int:
7
7
+
"""Converts a tuple of dice to an ordered hand."""
8
8
+
ordered = sorted([a, b, c])
9
9
+
return ordered[2] * 100 + ordered[1] * 10 + ordered[0]
10
10
+
11
11
+
12
12
+
def playStep2(hand: int, dice: int) -> tuple[int, int]:
13
13
+
"""
14
14
+
If you don't have 3 matching dice:
15
15
+
If you have a pair: keep the pair and reroll the third
16
16
+
Else: Roll all dice again
17
17
+
"""
18
18
+
(a, b, c) = handToDice(hand)
19
19
+
if a == b and b == c:
20
20
+
return hand, dice
21
21
+
if a == b:
22
22
+
c = dice % 10
23
23
+
dice //= 10
24
24
+
elif b == c:
25
25
+
a = dice % 10
26
26
+
dice //= 10
27
27
+
elif a == c:
28
28
+
b = dice % 10
29
29
+
dice //= 10
30
30
+
else:
31
31
+
b = dice % 10
32
32
+
dice //= 10
33
33
+
c = dice % 10
34
34
+
dice //= 10
35
35
+
return diceToOrderedHand(a, b, c), dice
36
36
+
37
37
+
38
38
+
def score(hand: int) -> int:
39
39
+
"""
40
40
+
Calculate the score of a hand
41
41
+
3 dice match: 20 + highest value * 3
42
42
+
2 dice match: 10 + highest value * 2
43
43
+
1 dice match: highest value
44
44
+
"""
45
45
+
(a, b, c) = handToDice(hand)
46
46
+
if a == b and b == c:
47
47
+
return 20 + a * 3
48
48
+
elif a == b:
49
49
+
return 10 + a * 2
50
50
+
elif b == c:
51
51
+
return 10 + b * 2
52
52
+
elif a == c:
53
53
+
return 10 + c * 2
54
54
+
else:
55
55
+
return a
56
56
+
57
57
+
58
58
+
def playThreeDiceYahtzee(dice: int) -> tuple[int, int]:
59
59
+
"""Play a game of Three Dice Yahtzee"""
60
60
+
a = dice % 10
61
61
+
dice //= 10
62
62
+
b = dice % 10
63
63
+
dice //= 10
64
64
+
c = dice % 10
65
65
+
dice //= 10
66
66
+
hand = diceToOrderedHand(a, b, c)
67
67
+
hand, dice = playStep2(hand, dice)
68
68
+
hand, dice = playStep2(hand, dice)
69
69
+
return hand, score(hand)
70
70
+
71
71
+
72
72
+
print("Testing playThreeDiceYahtzee()...", end="")
73
73
+
assert handToDice(123) == (1, 2, 3)
74
74
+
assert handToDice(214) == (2, 1, 4)
75
75
+
assert handToDice(422) == (4, 2, 2)
76
76
+
77
77
+
assert diceToOrderedHand(1, 2, 3) == 321
78
78
+
assert diceToOrderedHand(1, 4, 2) == 421
79
79
+
80
80
+
assert playStep2(413, 2312) == (421, 23)
81
81
+
assert playStep2(544, 23) == (443, 2)
82
82
+
assert playStep2(544, 456) == (644, 45)
83
83
+
84
84
+
assert score(432) == 4
85
85
+
assert score(443) == 10 + 4 + 4
86
86
+
assert score(633) == 10 + 3 + 3
87
87
+
assert score(555) == 20 + 5 + 5 + 5
88
88
+
89
89
+
assert playThreeDiceYahtzee(2312413) == (432, 4)
90
90
+
assert playThreeDiceYahtzee(2633413) == (633, 16)
91
91
+
assert playThreeDiceYahtzee(2333555) == (555, 35)
92
92
+
print("Passed!")