-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrockpaperscissors.py
More file actions
156 lines (129 loc) · 4.79 KB
/
rockpaperscissors.py
File metadata and controls
156 lines (129 loc) · 4.79 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
__author__ = 'Ben'
import random
computerwins = 0
playerwins = 0
ties = 0
spockdeaths = 0
def play():
global computerwins
global playerwins
global ties
bank = {1: "rock", 2: "paper", 3: "scissors"}
playermove = askplayermove()
computermove = getcomputermove()
if determinewinner(playermove, computermove) == 1:
playerwins += 1
print("Player won with throwing " + bank[playermove] + " against " + bank[computermove])
elif determinewinner(playermove, computermove) == 2:
computerwins += 1
print("Player lost with throwing " + bank[playermove] + " against " + bank[computermove])
else:
ties += 1
print("Tie with both throwing " + bank[playermove])
def playrpsls():
global computerwins
global playerwins
global ties
global spockdeaths
bank = {1: "rock", 2: "paper", 3: "scissors", 4: "lizard", 5: "Spock"}
playermove = askplayermoverpsls()
computermove = getcomputermoverpsls()
if determinewinnerrpsls(playermove, computermove) == 1:
if computermove == 5:
spockdeaths += 1
playerwins += 1
print("Player won with throwing " + bank[playermove] + " against " + bank[computermove])
print(winhow(playermove, computermove))
elif determinewinnerrpsls(playermove, computermove) == 2:
if playermove == 5:
spockdeaths += 1
computerwins += 1
print("Player lost with throwing " + bank[playermove] + " against " + bank[computermove])
print(winhow(computermove, playermove))
else:
ties += 1
print("Tie with both throwing " + bank[playermove])
def askplayermove():
choice = 0
while choice not in [1, 2, 3]:
choice = input("Please choose 1 for rock, 2 for paper, and 3 for scissors")
if choice.isnumeric():
choice = int(choice)
if choice in [1, 2, 3]:
break
print("That is not a valid choice, please try again!")
return choice
def askplayermoverpsls():
choice = getpositiveintupto("Please choose 1 for rock, 2 for paper, 3 for scissors, 4 for lizard, 5 for Spock", 5)
clearscreen()
return choice
def getcomputermove():
return random.randint(1, 3)
def getcomputermoverpsls():
return random.randint(1, 5)
def determinewinner(p, c):
if p - c == 0:
return 3
elif p - c == 1 or p - c == -2:
return 1
elif p - c == 2 or p - c == -1:
return 2
else:
return 3
def determinewinnerrpsls(p, c):
# Convention for the truth table is 3 is a tie, 1 indicates column player won and 2 indicates row player won
wintable = [["", "Rock", "Paper", "Scissors", "Lizard", "Spock"],
["Rock", 3, 2, 1, 1, 2],
["Paper", 1, 3, 2, 2, 1],
["Scissors", 2, 1, 3, 1, 2],
["Lizard", 2, 1, 2, 3, 1],
["Spock", 1, 2, 1, 2, 3]]
if p - c == 0:
return 3
else:
return wintable[p][c]
def winhow(p, c):
global spockdeaths
bank = {1: "rock", 2: "paper", 3: "scissors", 4: "lizard", 5: "Spock"}
verbtable = [["", "Rock", "Paper", "Scissors", "Lizard", "Spock"],
["Rock", 3, 2, "crushes", "crushes", 2],
["Paper", "covers", 3, 2, 2, "disproves"],
["Scissors", 2, "cuts", 3, "decapitates", 2],
["Lizard", 2, "eats", 2, 3, "poisons"],
["Spock", "vaporizes", 2, "smashes", 2, 3]]
return bank[p] + " " + verbtable[p][c] + " " + bank[c] + "\nSpock has died " + str(spockdeaths) + " times."
def printscores():
line = "=" * 45
print("Match complete! Who will win the next round?")
print(line)
print("Standings are currently:\nPlayer: " + str(playerwins) + " matches \nComputer: " + str(computerwins) \
+ " matches\n" + str(ties) + " ties")
print(line)
def clearscreen():
print("\n" * 50)
def getpositiveintupto(prompt, max):
n = -1
while n < 0:
print("*" * 45)
c = input(prompt+"\n")
if c.isnumeric() and int(c) > 0 and int(c) <= max:
n = int(c)
elif c.isalpha():
print("Please enter a number")
elif c.isnumeric() and int(c) <= 0:
print("Please enter a number higher than 0")
elif c.isnumeric() and int(c) > max:
print("Your number is too high, max selection is " + str(max))
else:
print("Invalid input, please try again")
return n
#Below this line is the main control loop for the game
n = getpositiveintupto("How many matches would you like to play?", 20)
game = getpositiveintupto("Pick 1 for regular rock paper scissors, pick 2 for rock paper scissors lizard spock:", 2)
for i in range(n):
if game == 1:
play()
elif game == 2:
playrpsls()
printscores()
print("Thanks for playing!")