-
Notifications
You must be signed in to change notification settings - Fork 0
/
hog_ui.py
133 lines (107 loc) · 4.4 KB
/
hog_ui.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#####################################
# Optional: Adding a User Interface #
#####################################
from hog import *
########################
# Printing game events #
########################
def play_and_print(strategy0, strategy1):
"""Simulate a game and print out what happened during the simulation."""
final0, final1 = play(printing_strategy(0, strategy0),
printing_strategy(1, strategy1),
square_update_and_print, 0, 0,
printing_dice(six_sided))
print('The final score is Player 0:', final0, 'vs Player 1:', final1)
def printing_strategy(who, strategy):
"""Return a strategy that also prints the player's score and choice.
(This could print "rolls 1 dice..." which is ungrammatical, but that's ok.)
>>> strategy0 = printing_strategy(0, always_roll_5)
>>> strategy0(10, 20)
The score is 10 to 20 and Player 0 rolls 5 dice...
5
>>> strategy1 = printing_strategy(1, always_roll_5)
>>> strategy1(8, 16)
The score is 16 to 8 and Player 1 rolls 5 dice...
5
"""
assert who == 0 or who == 1, 'the player must be 0 or 1'
def choose_and_print(score, opponent_score):
"A strategy function that also prints."
if who == 0:
score0, score1 = score, opponent_score
else:
score0, score1 = opponent_score, score
num_rolls = strategy(score, opponent_score)
print('The score is', score0, 'to', score1, 'and Player', who,
'rolls', num_rolls, 'dice...')
return num_rolls
return choose_and_print
def printing_dice(dice):
"""Return a dice function that also prints the outcome and a space."""
def dice_and_print():
"A dice function that also prints."
outcome = dice()
print(outcome, end=' ')
return outcome
return dice_and_print
def square_update_and_print(num_rolls, player_score, opponent_score, dice):
"""Return the updated score, print out the score update, and print when
Square Swine is triggered.
>>> d = printing_dice(make_test_dice(4, 5, 2))
>>> square_update_and_print(3, 9, 99, d)
[ 4 5 2 ] => 11; 9 + 11 = 20
20
>>> square_update_and_print(3, 25, 99, d)
[ 4 5 2 ] => 11; 25 + 11 = 36 triggering **Square Swine**, increasing to 49
49
"""
print(' [', end=" ")
turn_score = take_turn(num_rolls, opponent_score, dice) # Prints dice outcomes
print('] =>', turn_score, end='; ')
print(player_score, '+', turn_score, '=', player_score + turn_score, end='')
score = turn_score + player_score
if perfect_square(score):
score = next_perfect_square(score)
print(' triggering **Square Swine**, increasing to', score)
else:
print() # This print completes the line without adding any additional text
return score
########################
# Accepting User Input #
########################
def get_int(prompt, lower, upper):
"""Return an integer i such that i >= lower and i <= upper."""
choice = input(prompt)
while not choice.isnumeric() or int(choice) < lower or int(choice) > upper:
print('Please enter an integer from', lower, 'to', upper)
choice = input(prompt)
return int(choice)
def interactive_strategy(who):
"""Return a strategy for which the user provides the number of rolls."""
def strategy(score, opponent_score):
print('Player', who, ', you have', score, 'and your opponent has', opponent_score)
choice = get_int('How many dice will you roll? ', 0, 10)
return choice
return strategy
####################
# Playing the game #
####################
def play_with(num_players):
"""Play a game with NUM_PLAYERS interactive (human) players."""
if num_players == 0:
play_and_print(always_roll_5, always_roll_5)
elif num_players == 1:
play_and_print(interactive_strategy(0), always_roll_5)
elif num_players == 2:
play_and_print(interactive_strategy(0), interactive_strategy(1))
else:
print('num_players must be 0, 1, or 2.')
@main
def run(*args):
"""Select number of players and play a game."""
import argparse
parser = argparse.ArgumentParser(description="Play Hog")
parser.add_argument('--num_players', '-n', type=int, default=0,
help='How many interactive players (0, 1, or 2)')
args = parser.parse_args()
play_with(args.num_players)