Created
December 3, 2019 00:05
-
-
Save williamcanin/0ec3a157d88c8229fa95dd2bca4c72d0 to your computer and use it in GitHub Desktop.
Python script to play Rock-Paper-Scissors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# by William C. Canin <https://williamcanin.github.io> | |
# Date: 02 dec, 2019 | |
# Version: 1.0.1 | |
# Description: Game Rock-Paper-Scissors. | |
from random import choice, random | |
from textwrap import dedent | |
class Jankenpon: | |
def __init__(self): | |
self.user_ponts = 0 | |
self.machine_ponts = 0 | |
self.options = ['rock', 'paper', 'scissors'] | |
def user_choise(self): | |
msg = """ | |
Choose an option: | |
0 - Rock | |
1 - Paper | |
2 - Scissors""" | |
print(dedent(msg)) | |
try: | |
opt = int(input('>> Option: ')) | |
except ValueError: | |
print('### Error: Choose only numbers. Game tackled!') | |
exit(1) | |
return opt | |
def show_round(self, best_of_one, rounds): | |
if best_of_one: | |
round_info = 'Best of one!' | |
else: | |
round_info = rounds | |
msg = f""" | |
{'=' * 20} | |
Round: {round_info} | |
{'=' * 20}""" | |
return print(dedent(msg)) | |
def log(self, user_choice, machine_choice): | |
if self.user_win: | |
win_round = 'You won this round!!! :)' | |
elif self.machine_win: | |
win_round = 'The machine won this round! :(' | |
else: | |
win_round = 'This round was a draw.' | |
msg = f""" | |
-=-=-=-=-=-=-=-=-=- Log -=-=-=-=-=-=-=-=-=- | |
>>> You chose: {self.options[user_choice].title()} | |
>>> Machine chose: {self.options[machine_choice].title()} | |
>>> {win_round} | |
[Scoreboard] | |
You {self.user_ponts} x {self.machine_ponts} Machine | |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | |
""" | |
print(dedent(msg)) | |
def printf (self, text): | |
print(text, end="", flush=True) | |
def countdown(self): | |
from time import sleep | |
lst = ['Jan', 'Ken', 'Pon!'] | |
for items in lst: | |
self.printf(items) | |
sleep(0.5) | |
def welcome(self): | |
msg = """ | |
Welcome game Rock-Paper-Scissors! Best of three.""" | |
print(dedent(msg)) | |
def win_gamer(self): | |
if self.user_ponts > self.machine_ponts: | |
print('>>> You have won the game!!! :) ### Congratulations!!! ###') | |
elif self.user_ponts < self.machine_ponts: | |
print('>>> Machine won the game. :(') | |
else: | |
print('>>> The game ended in a draw.') | |
self.main(rounds=3, best_of_one=True) | |
def main(self, rounds=1, best_of_one=False): | |
try: | |
while rounds <= 3: | |
self.show_round(best_of_one, rounds) | |
machine_choice = choice(range(len(self.options))) | |
user_choice = self.user_choise() | |
self.user_win = False | |
self.machine_win = False | |
if machine_choice == 0: | |
if user_choice == 0: | |
pass | |
elif user_choice == 1: | |
self.user_ponts += 1 | |
self.user_win = True | |
elif user_choice == 2: | |
self.machine_ponts += 1 | |
self.machine_win = True | |
elif machine_choice == 1: | |
if user_choice == 0: | |
self.machine_ponts += 1 | |
self.machine_win = True | |
elif user_choice == 1: | |
pass | |
elif user_choice == 2: | |
self.user_ponts += 1 | |
self.user_win = True | |
elif machine_choice == 2: | |
if user_choice == 0: | |
self.user_ponts += 1 | |
self.user_win = True | |
elif user_choice == 1: | |
self.machine_ponts += 1 | |
self.machine_win = True | |
elif user_choice == 2: | |
pass | |
self.countdown() | |
self.log(user_choice, machine_choice) | |
rounds += 1 | |
self.win_gamer() | |
except IndexError: | |
print('### Invalid option. Game tackled!') | |
exit(1) | |
except KeyboardInterrupt: | |
print('\n>>> Aborted by user.') | |
if __name__ == '__main__': | |
Jankenpon().welcome() | |
Jankenpon().main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment