forked from ivogeorg/rpssl-game
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrpssl_game.py
More file actions
205 lines (153 loc) · 5.35 KB
/
rpssl_game.py
File metadata and controls
205 lines (153 loc) · 5.35 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
After
@ Clever Programmer
Rock paper scissors
"""
import random
# Global variables that all functions know about.
# DO NOT EDIT THESE GLOBAL VARIABLES
# OR YOUR GAME WILL BREAK.
COMPUTER_SCORE = 0
HUMAN_SCORE = 0
human_choice = ""
computer_choice = ""
def choice_to_number(choice):
"""Convert choice to number."""
# TODO: Implement
# NOTE
# A dictionary-based solution (see Clever Programmer tutorial and assignment README) will be preferred.
# Evaluation will be as follows:
# 1. Dictionary-based solution: 100%
# 2. Chain-of-if-statements solution: 80%
raise NotImplementedError
def number_to_choice(number):
"""Convert number to choice."""
# TODO: Implement
# NOTE
# A dictionary-based solution (see Clever Programmer tutorial and assignment README) will be preferred.
# Evaluation will be as follows:
# 1. Dictionary-based solution: 100%
# 2. Chain-of-if-statements solution: 80%
raise NotImplementedError
def random_computer_choice():
"""Choose randomly for computer."""
# TODO: Implement (Hint: Look up random.choice())
raise NotImplementedError
def choice_result(human_move, computer_move):
"""Return the result of who wins.
:param human_move: A string representing a move. One of
{'rock', 'paper', 'scissors', 'spock', 'lizard'}.
:param computer_move: A string representing a move.
:returns None. Modifies globals. Prints out result of last game.
"""
# DO NOT REMOVE THESE GLOBAL VARIABLE LINES.
global COMPUTER_SCORE
global HUMAN_SCORE
# TODO: Implement
# Based on the given human_choice and computer_choice,
# determine who won and increment their score by 1.
# In case of tie, don't increment anyone's score.
# NOTE
# A modulo-based solution (see Clever Programmer tutorial and assignment README) will be preferred.
# Evaluation will be as follows:
# 1. Modulo-based solution: 100%
# 2. Chain-of-if-statements solution: 80%
raise NotImplementedError
# DO NOT REMOVE THESE TEST FUNCTIONS.
# They will test your code.
def test_choice_to_number():
assert choice_to_number('rock') == 0
assert choice_to_number('paper') == 1
assert choice_to_number('scissors') == 2
assert choice_to_number('spock') == 3
assert choice_to_number('lizard') == 4
def test_number_to_choice():
assert number_to_choice(0) == 'rock'
assert number_to_choice(1) == 'paper'
assert number_to_choice(2) == 'scissors'
assert number_to_choice(3) == 'spock'
assert number_to_choice(4) == 'lizard'
def test_computer_choice():
choice = random_computer_choice()
assert choice in ['rock', 'paper', 'scissors', 'spock', 'lizard']
assert 0 <= choice_to_number(choice) <= 4
def test_all():
test_choice_to_number()
test_number_to_choice()
test_computer_choice()
# Code for handling human player moves
# DO NOT EDIT THESE FUNCTIONS
def rock():
global human_choice, computer_choice
global HUMAN_SCORE, COMPUTER_SCORE
human_choice = 'rock'
computer_choice = random_computer_choice()
choice_result(human_choice, computer_choice)
def paper():
global human_choice, computer_choice
global HUMAN_SCORE, COMPUTER_SCORE
human_choice = 'paper'
computer_choice = random_computer_choice()
choice_result(human_choice, computer_choice)
def scissors():
global human_choice, computer_choice
global HUMAN_SCORE, COMPUTER_SCORE
human_choice = 'scissors'
computer_choice = random_computer_choice()
choice_result(human_choice, computer_choice)
def spock():
global human_choice, computer_choice
global HUMAN_SCORE, COMPUTER_SCORE
human_choice = 'spock'
computer_choice = random_computer_choice()
choice_result(human_choice, computer_choice)
def lizard():
global human_choice, computer_choice
global HUMAN_SCORE, COMPUTER_SCORE
human_choice = 'lizard'
computer_choice = random_computer_choice()
choice_result(human_choice, computer_choice)
# Game play functions
# DO NOT EDIT THESE FUNCTIONS
def greet():
print('Welcome to the game of RPSSL')
print('Commands: (r)ock\n' +
' (p)aper\n' +
' (s)cissors\n' +
' sp(o)ck\n' +
' (l)izard\n' +
' (q)uit\n')
def get_user_input():
valid = False
while not valid:
print('Please, make a choice: ', end='')
s = input().strip().lower()
if len(s) == 0 or len(s) > 1 or s not in 'rpsolq':
print('Invalid input. Valid inputs are {r, p, s, o, l, q}.')
continue
else:
return s
def play_rps():
global HUMAN_SCORE, COMPUTER_SCORE
moves = {'r': rock,
'p': paper,
's': scissors,
'o': spock,
'l': lizard}
greet()
while True:
user_input = get_user_input()
if user_input == 'q':
print('Final score: Human {} : Computer {}'.format(HUMAN_SCORE, COMPUTER_SCORE))
print('Thanks for playing. Good bye!')
break
else:
move = moves.get(user_input)
assert move is not None
move()
print('Score: Human {} : Computer {}'.format(HUMAN_SCORE, COMPUTER_SCORE))
# main function
if __name__ == '__main__':
# Uncomment to test your functions.
# test_all()
play_rps()