Skip to content

Instantly share code, notes, and snippets.

@GShwartz
Created February 11, 2020 19:46
Show Gist options
  • Save GShwartz/bf333b75f1881d797ffd18d07cda53c0 to your computer and use it in GitHub Desktop.
Save GShwartz/bf333b75f1881d797ffd18d07cda53c0 to your computer and use it in GitHub Desktop.
Rock Paper Sccissors with Python
from termcolor import colored
import sys
import random
import time
class rps():
def __init__(self):
self.user = user
self.rstart = rstart
self.max_rounds = max_rounds
self.counter = counter
self.rndnum = rndnum
self.rgame = rgame
self.player = player
self.moves = moves
self.comp = comp
def show_welcome():
print(colored("Welcome to RPS !!!", 'blue'))
print(colored("RULES:", 'yellow'), colored("*", 'red'), "Rock Beats Scissors", colored("*", 'red'))
print(colored(" *", 'grey'), "Scissors beats Paper", colored("*", 'grey'))
print(colored(" *", 'blue'), "Paper beats Rock", colored("*", 'blue'))
print(colored("!---=== GOOD LUCK ===---! \n", 'green'))
def rounds(max_rounds=10):
try:
user = int(input(colored("[???]Rounds? [Max 10]: ", 'blue')))
except:
print(colored("[!!!] Invalid Input! Numbers Only! [!!!]", 'red'))
rps.play()
if user <= 0 or user > 10:
print(colored("[!!!] Invalid Input! [Max 10] [!!!]", 'red'))
rps.play()
else:
return user
def restart(rstart=0):
try:
rstart = str(input(colored("Would You Like To Restart? [Y/n]: ", 'grey')))
except:
print(colored("[---] Wrong Input! try [y] or [n] [---]", 'red'))
if rstart.lower() == "y":
rps.play()
elif rstart.lower() == "n":
print(colored("[###] Thanks For Playing! :) [###]", 'grey'))
sys.exit()
else:
print(colored("[---] Wrong Input! try [y] or [n] [---]", 'red'))
rps.restart()
def play():
while True:
rps.show_welcome()
rndnum = rps.rounds()
counter = 1
print("\n###########################")
print(colored(f"[***]Number Of Rounds: {rndnum}", 'green'))
print("###########################")
while counter < rndnum + 1:
print(colored(f"\n[@@@]Round #: {counter} [@@@] ", 'magenta'))
rps.game(counter)
counter += 1
if counter > rndnum:
print("\n########################")
print(colored("[***]", 'yellow'), colored("Game Over!", 'grey'), colored("[***]", 'yellow'))
print("########################\n")
rgame = rps.restart()
if rgame == True:
continue
else:
print("[### Thanks For Playing! :)")
break
def game(counter):
player = 0
moves = ['Rock', 'Paper', 'Scissors']
comp = random.choice(moves)
print(colored("Choose:", 'grey'), colored("1)Rock", 'red'), colored("2)Paper", 'blue'),
colored("3)Scissors", 'yellow'), colored("0)[E]xit", 'magenta'))
try:
player = int(input(colored("What's Your Guess? ", 'grey')))
except:
print(colored("[!!!] Try [1-3] or [exit] ", 'red'))
if counter > 0:
counter -= 1
rps.game(counter)
if (player == 1 and comp == 'Rock') or (player == 2 and comp == 'Paper') \
or (player == 3 and comp == 'Scissors'):
p = {moves[player - 1]}
print(colored(f"Player Choose: {p}", 'green'))
print(colored(f"Computer Choose: {comp}", 'yellow'))
print(colored("[***] Result: It's A Draw !! [***] ", 'blue'))
elif (player == 1 and comp == 'Paper') or (player == 2 and comp == 'Scissors') \
or (player == 3 and comp == 'Rock'):
p = {moves[player - 1]}
print(colored(f"Player Choose: {p}", 'green'))
print(colored(f"Computer Choose: {comp}", 'yellow'))
print(colored("[***] Result: You Lose !! [***] ", 'blue'))
elif (player == 1 and comp == 'Scissors') or (player == 2 and comp == 'Rock') \
or (player == 3 and comp == 'Paper'):
p = {moves[player - 1]}
print(colored(f"Player Choose: {p}", 'green'))
print(colored(f"Computer Choose: {comp}", 'yellow'))
print(colored("[***] Result: You Win !! [***] ", 'blue'))
elif player == 0:
print(colored("Thanks for playing! Goodbye :) ", 'yellow'))
sys.exit()
else:
print(colored("[!!!] Try [1-3] or [99] --- [!!!] Round Lost [!!!]", 'green'))
rps.game(counter=counter-1)
rps.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment