-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock-paper-scissors.py
59 lines (51 loc) · 1.86 KB
/
rock-paper-scissors.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
import random
from enum import IntEnum
#Defines the classes and names for the game.
class Action(IntEnum):
Rock = 0
Paper = 1
Scissors = 2
Lizard = 3
Spock = 4
#Establish the winning-losing relations between classes.
victories = {
Action.Scissors: [Action.Lizard, Action.Paper],
Action.Paper: [Action.Spock, Action.Rock],
Action.Rock: [Action.Lizard, Action.Scissors],
Action.Lizard: [Action.Spock, Action.Paper],
Action.Spock: [Action.Scissors, Action.Rock]
}
#Get the user selection.
def get_user_selection():
choices = [f'{action.name}[{action.value}]' for action in Action]
choices_str = ', '.join(choices)
selection = int(input(f'Enter a choice ({choices_str}): '))
action = Action(selection)
return action
#Chose the computer selection at random.
def get_computer_selection():
selection = random.randint(0, len(Action) - 1)
action = Action(selection)
return action
#Determine the winner and loser of each game.
def determine_winner(user_action, computer_action):
defeats = victories[user_action]
if user_action == computer_action:
print(f'Both players selected {user_action.name}. It\'s a tie')
elif computer_action in defeats:
print(f'{user_action.name} beats {computer_action.name}. You win! :D')
else:
print(f'{computer_action.name} beats {user_action.name}. You lose :(')
#Create the loop to repeat the game.
while True:
try:
user_action = get_user_selection()
except ValueError as e:
range_str = f"[0, {len(Action) - 1}]"
print(f"Invalid selection. Enter a value in range {range_str}")
continue
computer_action = get_computer_selection()
determine_winner(user_action, computer_action)
play_again = input("Play again? (y/n): ")
if play_again.lower() != "y":
break