-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz5-5.py
41 lines (35 loc) · 1.41 KB
/
quiz5-5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -----------------
# User Instructions
#
# Write the two action functions, hold and roll. Each should take a
# state as input, apply the appropriate action, and return a new
# state.
#
# States are represented as a tuple of (p, me, you, pending) where
# p: an int, 0 or 1, indicating which player's turn it is.
# me: an int, the player-to-move's current score
# you: an int, the other player's current score.
# pending: an int, the number of points accumulated on current turn, not yet scored
def hold(state):
"""Apply the hold action to a state to yield a new state:
Reap the 'pending' points and it becomes the other player's turn."""
p, me, you, pending = state
if p == 0:
return (1, you, me+pending, 0)
else:
return (0, you, me+pending, 0)
def roll(state, d):
"""Apply the roll action to a state (and a die roll d) to yield a new state:
If d is 1, get 1 point (losing any accumulated 'pending' points),
and it is the other player's turn. If d > 1, add d to 'pending' points."""
p, me, you, pending = state
if d != 1:
return (p, me, you, pending + d)
return hold((p, me, you, 1))
def test():
assert hold((1, 10, 20, 7)) == (0, 20, 17, 0)
assert hold((0, 5, 15, 10)) == (1, 15, 15, 0)
assert roll((1, 10, 20, 7), 1) == (0, 20, 11, 0)
assert roll((0, 5, 15, 10), 5) == (0, 5, 15, 15)
return 'tests pass'
print test()