Skip to content

Instantly share code, notes, and snippets.

/rps_example.py Secret
Created Sep 13, 2016

Embed
What would you like to do?
from enum import Enum
class Game(object):
choices = Enum("Game.choices", "rock paper scissors")
results = Enum("Game.results", "win lose draw")
result_table = {choices.rock: {choices.paper: results.lose,
choices.scissors: results.win},
choices.paper: {choices.rock: results.win,
choices.scissors: results.lose},
choices.scissors: {choices.rock: results.lose,
choices.paper: results.win}}
@classmethod
def result_of(cls, a, b):
return result_table[a].get(b, cls.results.draw)
@classmethod
def choose(cls, prompt):
while True:
tmp = input(prompt)
try:
result = cls.choices[tmp.lower()]
except KeyError:
continue # failed validation
return result
def __init__(self):
"""Maybe set up high score tracking here? I don't know"""
pass
def play(self):
p1 = self.choose("Player 1: ")
p2 = self.choose("Player 2: ")
p1_wins = self.result_of(a, b)
if p1_wins == self.results.win:
# celebrate
elif p1_wins == self.results.lose:
# mourn
else: # self.results.draw
# play again?
g = Game()
g.play()
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.