Skip to content

Instantly share code, notes, and snippets.

@AnotherTermina AnotherTermina/rps.py Secret
Created Nov 3, 2019

Embed
What would you like to do?
rock-paper-scissors
#!/usr/bin/env python3
import random
from enum import Enum
class Choice(Enum):
ROCK = 0
PAPER = 1
SCISSORS = 2
@classmethod
def from_str(cls, s: str) -> "Choice":
try:
return {
"r": cls.ROCK,
"rock": cls.ROCK,
"p": cls.PAPER,
"paper": cls.PAPER,
"s": cls.SCISSORS,
"scissors": cls.SCISSORS
}[s.strip().lower()]
except KeyError:
raise ValueError(f"{s!r} is not a valid {cls.__name__}")
def __str__(self) -> str:
return self.name.lower()
def beats(self, other: "Choice") -> bool:
return (self.value - other.value) % 3 == 1
class RockPaperScissors:
def __init__(self):
self.wins = 0
self.losses = 0
self.ties = 0
self.choices = list(Choice)
def random_choice(self) -> Choice:
return random.choice(self.choices)
def player_choice(self) -> Choice:
prompt = ("\nChoices are 'rock', 'paper', or 'scissors'.\n"
"Which do you choose? ")
while True:
try:
return Choice.from_str(input(prompt))
except ValueError:
print("Invalid input, try again!")
def check_win(self, player_choice: Choice, opponent_choice: Choice):
if player_choice == opponent_choice:
self.ties += 1
print("The game is a tie! You are a most worthy opponent!")
elif player_choice.beats(opponent_choice):
self.wins += 1
print("You win! My honor demands a rematch!")
else:
self.losses += 1
print("Haha, I am victorious! Dare you challenge me again?")
def print_score(self):
print(f"You have {self.wins} wins, {self.losses} losses, and "
f"{self.ties} ties.")
def play_round(self):
player_choice = self.player_choice()
opponent_choice = self.random_choice()
print(f"\nYou picked {player_choice}, and I picked {opponent_choice}.")
self.check_win(player_choice, opponent_choice)
self.print_score()
def player_wants_to_play_again(self) -> bool:
prompt = "\nDo you wish to play again? (y/n): "
valid_choices = ("y", "n")
while True:
user_choice = input(prompt).strip().lower()
if user_choice in valid_choices:
return user_choice == "y"
print("Invalid input!")
def run_game(self):
while True:
self.play_round()
if not self.player_wants_to_play_again():
print("You are weak!")
break
if __name__ == "__main__":
game = RockPaperScissors()
game.run_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.