Last active
December 3, 2019 00:06
-
-
Save williamcanin/1f7a63a274cf83d552a839685dd5fb0f to your computer and use it in GitHub Desktop.
Python script para jogar Pedra Papel Tesoura (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 | |
# por William C. Canin <https://williamcanin.github.io> | |
# Date: 02 dez, 2019 | |
# Versão: 1.0.1 | |
# Descrição: Jogo Pedra Papel Tesoura. | |
from random import choice, random | |
from textwrap import dedent | |
class Jankenpon: | |
def __init__(self): | |
self.user_ponts = 0 | |
self.machine_ponts = 0 | |
self.options = ['pedra', 'papel', 'tesoura'] | |
def user_choise(self): | |
msg = """ | |
Escolha uma opção: | |
0 - Pedra | |
1 - Papel | |
2 - Tesoura""" | |
print(dedent(msg)) | |
try: | |
opt = int(input('>> Opção: ')) | |
except ValueError: | |
print('### Erro: Escolha somente números. Jogo abordado!') | |
exit(1) | |
return opt | |
def show_round(self, best_of_one, rounds): | |
if best_of_one: | |
round_info = 'Melhor de um!' | |
else: | |
round_info = rounds | |
msg = f""" | |
{'=' * 20} | |
Partida: {round_info} | |
{'=' * 20}""" | |
return print(dedent(msg)) | |
def log(self, user_choice, machine_choice): | |
if self.user_win: | |
win_round = 'Você VENCEU está partida!!! :)' | |
elif self.machine_win: | |
win_round = 'A máquina VENCEU está partida! :(' | |
else: | |
win_round = 'Essa partida empatou.' | |
msg = f""" | |
-=-=-=-=-=-=-=-=-=- Log -=-=-=-=-=-=-=-=-=- | |
>>> Você escolheu: {self.options[user_choice].title()} | |
>>> A máquina escolheu: {self.options[machine_choice].title()} | |
>>> {win_round} | |
[Placar] | |
Você {self.user_ponts} x {self.machine_ponts} Máquina | |
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- | |
""" | |
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 = """ | |
Bem-Vindo ao jogo Pedra Papel Tesoura! Melhor de três.""" | |
print(dedent(msg)) | |
def win_gamer(self): | |
if self.user_ponts > self.machine_ponts: | |
print('>>> Você VENCEU o jogo!!! :) ### Parabéns!!! ###') | |
elif self.user_ponts < self.machine_ponts: | |
print('>>> A máquina VENCEU o jogo. :(') | |
else: | |
print('>>> O jogo terminou empatado.') | |
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('### Opção inválida. Jogo abordado!') | |
exit(1) | |
except KeyboardInterrupt: | |
print('\n>>> Abordado pelo usuário.') | |
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