-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpsls_game.py
103 lines (92 loc) · 2.81 KB
/
rpsls_game.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
# TODO: Develop a console-based Rock Paper Scissors Lizard Spock game in Python
# Game should be modular, allowing for easy updates or rule changes
# Implement game rules:
# - Scissors decapitate lizard
# - Scissors cuts paper
# - Paper covers rock
# - Rock crushes lizard
# - Lizard poisons Spock
# - Spock smashes scissors
# - Lizard eats paper
# - Paper disproves Spock
# - Spock vaporizes rock
# - Rock crushes scissors
# Include user input for selecting options and display game results
import random
# Define global variables
# Define dictionery of game options
options = {
1: "rock",
2: "paper",
3: "scissors",
4: "lizard",
5: "spock"
}
# Define dictionery of game rules
rules = {
'rock': ['scissors', 'lizard'],
'paper': ['rock', 'spock'],
'scissors': ['paper', 'lizard'],
'lizard': ['spock', 'paper'],
'spock': ['scissors', 'rock']
}
# Define dictionary of game results
results = {
'win': {
'rock': ['scissors', 'lizard'],
'paper': ['rock', 'spock'],
'scissors': ['paper', 'lizard'],
'lizard': ['spock', 'paper'],
'spock': ['scissors', 'rock']
},
'lose': {
'rock': ['paper', 'spock'],
'paper': ['scissors', 'lizard'],
'scissors': ['rock', 'spock'],
'lizard': ['rock', 'scissors'],
'spock': ['paper', 'lizard']
},
'tie': {
'rock': ['rock'],
'paper': ['paper'],
'scissors': ['scissors'],
'lizard': ['lizard'],
'spock': ['spock']
}
}
# Define function to get user input
def get_user_input():
user_choice = input('Enter your choice (rock, paper, scissors, lizard, spock): ').lower()
return user_choice
# Define function to get computer input
def get_computer_input():
computer_choice = random.choice(options)
return computer_choice
# Define function to determine the winner
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return 'tie'
elif computer_choice in rules[user_choice]:
return 'win'
else:
return 'lose'
# Define function to display the results
def display_results(user_choice, computer_choice, result):
if result == 'tie':
print(f'It\'s a tie! You both chose {user_choice}.')
elif result == 'win':
print(f'You win! {user_choice} beats {computer_choice}.')
else:
print(f'You lose! {computer_choice} beats {user_choice}.')
# Define function to play the game
def play_game():
user_choice = get_user_input()
computer_choice = get_computer_input()
result = determine_winner(user_choice, computer_choice)
display_results(user_choice, computer_choice, result)
while True:
play_game()
play_again = input("Do you want to play again? (yes/no): ")
if play_again.lower() != "yes":
break
print("Thanks for playing!")