-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrock_paper_scissors.py
More file actions
141 lines (120 loc) Β· 4.92 KB
/
Copy pathrock_paper_scissors.py
File metadata and controls
141 lines (120 loc) Β· 4.92 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
# Script: rock_paper_scissors.py
# Description: Rock, Paper, Scissors Game
# Author: Mohammed Abdul Raqeeb
# Date: 22/06/2024
try:
from colorama import Fore, Style
except ImportError:
print("This script requires the 'colorama' module.\nPlease install it using 'pip install colorama' and try again.")
exit(1)
from random import choice
from time import sleep
# Define colors and styles
blue = Fore.BLUE
cyan = Fore.CYAN
green = Fore.GREEN
magenta = Fore.MAGENTA
red = Fore.RED
yellow = Fore.YELLOW
bright = Style.BRIGHT
reset = Style.RESET_ALL
# Play the round
def play_round():
"""Play a single round of Rock, Paper, Scissors."""
# Choices available for the game
game_choices = ['Rock', 'Paper', 'Scissors', 'R', 'P', 'S']
user_input = input(
f"\n{yellow}{bright}" + f" Player ".center(22, "-") + f"{reset}{cyan}\n Choose from the following: \n\'Rock\'πͺ¨ (r) , \'Paper\'π (p) , \'Scissors\'βοΈ (s){reset} ['q' to Quit]\n\n{blue} ---> {reset}").strip().title()
print()
if user_input in ['Q']:
return "Q", None, None
if user_input not in game_choices:
return None, None, None
if user_input in ['R', 'Rock']:
user_choice = "Rock"
elif user_input in ['P', 'Paper']:
user_choice = "Paper"
elif user_input in ['S', 'Scissors']:
user_choice = "Scissors"
# Define which choice beats which
beats = {
'Rock': 'Scissors',
'Paper': 'Rock',
'Scissors': 'Paper'
}
# Random choice by computer
computer_choice = choice(["Rock", "Paper", "Scissors"])
print(f"{green}Player's choice : {magenta}{user_choice}\n{green}Computer's choice: {magenta}{computer_choice}")
if computer_choice == user_choice:
sleep(0.5)
print(f"\n{bright}{magenta}||| It's a Draw ||| π{reset}")
return 0, 0, 1
else:
if beats[user_choice] == computer_choice:
print(f"\n{red} {user_choice} ----β {computer_choice}{reset}")
sleep(0.5)
print(f"{bright}{blue}\n!!! You Win !!! π{reset}")
return 1, 0, 0
else:
print(f"\n{red} {computer_choice} ----β {user_choice}{reset}")
sleep(0.5)
print(f"{bright}{yellow}\n||| You Lose ||| π\nBetter Luck Next time{reset}")
return 0, 1, 0
# Display the scoreboard
def display_scoreboard(player_score, computer_score, draw_count):
"""
Display the final scoreboard.
Prints the final scores of the player, the computer,
and the number of draws in a neatly formatted table.
Parameters:
player_score (int): The score representing the number of rounds won by the player.
computer_score (int): The score representing the number of rounds won by the computer.
draw_count (int): The count of rounds that ended in a draw.
"""
# Print the final scoreboard
print(f"{cyan}{bright}----------------------------------")
print("| S C O R E B O A R D |")
print("----------------------------------")
print(f"| {magenta}Player{cyan} | {yellow}Computer{cyan} | {green}Draw{cyan} |")
print("----------------------------------")
print(f"|{magenta}{str(player_score).center(10)}{cyan}|{yellow}{str(computer_score).center(12)}{cyan}|{green}{str(draw_count).center(8)}{cyan}|")
print(f"----------------------------------{reset}")
def main():
"""Main function to run the Rock, Paper, Scissors game."""
print(f"\n{magenta}{bright}" + f" ROCK - PAPER - SCISSORS ".center(50, "*"))
print(f"{magenta}{bright}" + f" NEW GAME ".center(50, "*"))
player_score, computer_score, draw_count = 0, 0, 0
while True:
player_win, computer_win, draw = play_round()
if player_win is None:
print(f"{red}Invalid Input!\n{blue}Please enter \"Rock\"(r), \"Paper\"(p), or \"Scissors\"(s){reset}")
sleep(1)
print()
continue
elif player_win in ['Q']:
print(f"{magenta}!!! Game Over !!!\nHOPE YOU ENJOYED π{reset}\n")
sleep(0.5)
display_scoreboard(player_score, computer_score, draw_count)
break
player_score += player_win
computer_score += computer_win
draw_count += draw
sleep(0.5)
play_again = input(f"{blue}\nDo you want to play again (y/n) [default: y]: ").strip().lower()
if play_again in ["", "y", "yes"]:
print()
continue
elif play_again in ["n", "no"]:
sleep(0.5)
print(f"\n{magenta}!!! Game Over !!!\nHOPE YOU ENJOYED π{reset}\n")
sleep(0.5)
display_scoreboard(player_score, computer_score, draw_count)
break
else:
sleep(0.5)
print(f"\n{red}Unrecognized Input\n{magenta}!!! Game Over !!!{reset}\n")
sleep(0.5)
display_scoreboard(player_score, computer_score, draw_count)
break
if __name__ == '__main__':
main()