-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha07_stubs.py
231 lines (202 loc) · 10 KB
/
a07_stubs.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#################################################################################
# Author(s): Concepta Njolima, Immanuela Belaineh
# Username(s): belainehi, njolimac
#
# Assignment: A07 Rock paper scissors lizard spock
# Purpose: To create a game that plays a different version of rock paper scissor.
#################################################################################
# Acknowledgements:
# Liberty Mupotsa
#
#################################################################################
#################################################################################
import random
computer_score = 0
user_score = 0
rounds = 0
def player_choice(start):
"""
This function asks the user to choose between rock,paper,scissor,lizard, or spock
return: choice:The choice made by the player
"""
trials = 1
while start:
while trials < 6:
# print(trials)
# prompt the player to input their choice
choice = input(str(
"So, what do you choose(lower case letters only)? s for spock? l for lizard? r for rock? p for paper? sc for scissor?"))
if choice == "s" or choice == "l" or choice == "r" or choice == "p" or choice == "sc":
# if choice is the expected then return choice
choice_made = True
print(choice)
return choice
else:
# if an unexpected choice is entered
choice_made = False
print("only s,p,l,r, or sc are expected")
print("You have {0} entry(s) out of the 5 permitted wrong entries".format(trials))
trials += 1
break
pass
def begin_game():
"""
This function provides a user to begin or not begin the game
:return: begin: True if the user chooses y or False if the user uses n
"""
begin = False
wrong_trials = 1
while not begin:
# To limit the wrong trials to 6
while wrong_trials < 6:
print("Rock!Paper!Scissor!Spock!Lizard")
# Ask the user to choose y to begin the game or n to not begin the game
user_begin = str(input("Enter y to start a New game or n to Close the game"))
if user_begin == "y":
begin = True
return begin
elif user_begin == "n":
begin = False
return begin
else:
print("Wrong choice entered")
print("You have used up {0} entries out of the 5 permitted wrong entries".format(wrong_trials))
wrong_trials += 1
break
def comparison(playerchoice, computerchoice):
"""
Docstring for function_1
This function compares the player's and computer's choice to show who has won this particular game.
:param playerchoice: It is returned from the player_choice function.
:param computerchoice: It is returned from the computer_choice function.
:return: computernumber, playernumber
"""
global computer_score
global user_score
# We compare the computer's choice and the player's choice then award points according to the rules of rock-paper-spock-scissor-lizard
if playerchoice == computerchoice:
print("the computer's choice was {0} and Paper covers rock".format(computerchoice))
print("Your choice is similar to the computers")
print("Your score is:", user_score, "Computer's score is:", computer_score)
elif playerchoice == "r":
if computerchoice == "p":
computer_score = computer_score + 1
print("the computer's choice was {0} and Paper covers rock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "sc":
user_score += user_score
print("the computer's choice was {0} and Rock crushes scissors".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "l":
user_score = user_score + 1
print("the computer's choice was {0} and Rock crushes lizard".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "s":
computer_score = computer_score + 1
print("the computer's choice was {0} and Spock vaporizes rock ".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
elif playerchoice == "p":
if computerchoice == "r":
user_score = user_score + 1
print("the computer's choice was {0} and paper covers rock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "sc":
computer_score = computer_score + 1
print("the computer's choice was {0} and scissors cut paper ".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "l":
computer_score = computer_score + 1
print("the computer's choice was {0} and Lizard eats paper".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "s":
user_score = user_score + 1
print("the computer's choice was {0} and Paper disproves spock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
elif playerchoice == "sc":
if computerchoice == "r":
user_score = user_score + 1
print("the computer's choice was {0} and Rock crushes scissors".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "p":
user_score = user_score + 1
print("the computer's choice was {0} and Scissors cut paper".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "l":
user_score = user_score + 1
print("the computer's choice was {0} and Scissors decapitate Lizard ".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "s":
computer_score = computer_score + 1
print("the computer's choice was {0} and Spock smashes scissors".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
elif playerchoice == "l":
if computerchoice == "r":
computer_score = computer_score + 1
print("the computer's choice was {0} and Rock crushes lizard".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "p":
user_score = user_score + 1
print("the computer's choice was {0} and Lizard eats paper".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "sc":
computer_score = computer_score + 1
print("the computer's choice was {0} and Scissors decapitate Lizard".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "s":
user_score = user_score + 1
print("the computer's choice was {0} and Lizard poisons Spock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
elif playerchoice == "s":
if computerchoice == "r":
user_score = user_score + 1
print("the computer's choice was {0} and Spock vaporizes Rock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "p":
computer_score = computer_score + 1
print("the computer's choice was {0} and Paper disproves Spock ".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "sc":
user_score = user_score + 1
print("the computer's choice was {0} and Spock smashes Scissors ".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
if computerchoice == "l":
computer_score = computer_score + 1
print("the computer's choice was {0} and Lizard poisons Spock".format(computerchoice))
print("Your score is:", user_score, "Computer's score is:", computer_score)
def computer_choice():
"""
Docstring for function_2
This function let's the computer generate a random choice from "rock","paper","scissor","lizard" and"spock".
:return: Computer's choice
"""
options = ["r", "p", "sc", "l", "s"]
# To randomly select from the options list
computer_random = random.choice(options)
# print(computer_random)
return computer_random
def main():
"""
This is where the whole function comes together.
:return: None
"""
global rounds
start_game = begin_game()
if start_game:
while rounds < 5:
choice_computer = computer_choice() # Function call to computer_choice
choice_player = player_choice(start_game)
comparison(choice_player, choice_computer)
rounds += 1
# print(rounds)
# To limit the number of rounds for each play session to 5
if rounds == 5:
if computer_score > user_score:
print("Computer wins!!!")
elif user_score > computer_score:
print("User wins!!!")
elif user_score == computer_score:
print("You draw!!!This deserves a new game!")
elif not start_game:
print("Game closed")
pass
main()