-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRockpaperscissors.py
executable file
·105 lines (83 loc) · 2.33 KB
/
Rockpaperscissors.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
#!/usr/bin/env python3
# Rock, Paper, Scissor
from enum import Enum
import random
import time
#Delay when game is played, can be reduced for testing
DELAY = 1
class Throw(Enum):
Rock = 1
Paper = 2
Scissors = 3
def vs(self, throw):
RULES = {self.Rock: self.Scissors,
self.Paper: self.Rock,
self.Scissors: self.Paper
}
if self == throw:
return 0 # Tie
if RULES[self] == throw:
return 1 # Player wins
else:
return -1 # Computer wins
@staticmethod
# function for the computers random selection of R, P or S
def random():
return Throw(random.randint(1,3))
# For keeping score
SCORE = {"player": 0,
"computer": 0,
}
def start():
print ("Let's play a game of Rock, Paper, Scissors.")
while game():
pass
scores()
def game():
player = move()
computer = Throw.random()
play(player, computer)
return play_again()
def move():
# Loop until we get valid input
while True:
print()
player = input("Rock = 1\nPaper = 2\nScissors = 3\nMake a move: ")
try:
player = int(player)
return Throw(player) # Will throw a ValueError if not a valid choice
except ValueError:
pass
print("Choose 1, 2 or 3.")
def play(player, computer):
#delay and 1...2...3... simulates the 1,2,3 in a real game of RPS
print("1...")
time.sleep(DELAY)
print("2...")
time.sleep(DELAY)
print("3...")
time.sleep(0.5 * DELAY)
print("Computer threw {0}!".format(computer.name))
result = player.vs(computer)
if result == 0:
print("Tie.")
elif result == 1:
print("You win!")
SCORE["player"] += 1
else:
print("Computer wins!")
SCORE["computer"] += 1
# function to ask the player if they would like to play again
def play_again():
answer = input("Would you like to play again? y/n: ")
if answer.lower() in ("y", "yes"):
return answer
else:
print("Thank you for palying.")
# function for printing high scores when the player chooses to not play again
def scores():
print("HIGH SCORES")
print("Player: ", SCORE["player"])
print("Computer: ", SCORE["computer"])
if __name__ == '__main__':
start()