Skip to content

Instantly share code, notes, and snippets.

@1328
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save 1328/913f589186f59e1c019c to your computer and use it in GitHub Desktop.

Select an option

Save 1328/913f589186f59e1c019c to your computer and use it in GitHub Desktop.
another take on rps
import random
class Throw(object):
choices = {1:'Rock',2:'Paper', 3:'Scissors'}
victory = {
(1,3):'Rock smashes Scissors',
(2,1):'Paper covers Rock',
(3,2):'Scissors cut Paper',
}
def __init__(self, choice = None):
if choice is None:
choice = random.randrange(1,4)
self.choice = choice
def __str__(self):
return self.choices[self.choice]
def __eq__(self,other):
return self.choice == other.choice
def __gt__(self,other):
if self == other:
return False
return (self.choice, other.choice) in self.victory
def victory_message(self,other):
return self.victory.get((self.choice,other.choice))
class Game(object):
def __init__(self, name = 'you', rounds = 3):
self.name = name
self.score_human = 0
self.score_computer = 0
self.rounds = rounds
def play_round(self):
human = Throw() # just pick randomly
computer = Throw()
print('Human: {}, Computer: {}'.format(human,computer))
if human == computer:
print('\tTie')
elif human > computer:
print('\t Human wins: {}'.format(human.victory_message(computer)))
self.score_human += 1
else:
print('\t computer wins: {}'.format(computer.victory_message(human)))
self.score_computer += 1
def play_game(self):
for i in range(self.rounds):
print('-----')
self.play_round()
print('Game over...')
if self.score_human > self.score_computer:
print('\t{} won the game'.format(self.name))
elif self.score_computer > self.score_human:
print('\tComputer won the game')
else:
print("it's a tie!")
def main():
game = Game(name='steve', rounds = 5)
game.play_game()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment