Rock, Paper, Scissors

Jorin Diemer, Jens Hahn - WS 2017


In [ ]:
import numpy as np

class RockPaperScissors:
    def __init__(self):
        self.rules = {'rock': {'paper': False, 'scissors': True},
                      'paper': {'rock': True, 'scissors': False},
                      'scissors': {'rock': False, 'paper': True}}
        self.score = [0,0]
        
    def computer_choice(self):
        choice = np.random.choice(list(self.rules))
        return choice

    def user_choice(self):
        choice = None
        while not choice in self.rules:
            choice = input('Choose "rock", "paper", or "scissors":')
        return choice

    def evaluate(self):
        human_choice = self.user_choice()
        pc_choice = self.computer_choice()
        if human_choice == pc_choice:
            print('Tied game.')
        elif self.rules[human_choice][pc_choice]:
            print('You won!')
            self.score[0] += 1
        else:
            print('You lost!')
            self.score[1] += 1
            
    def start(self, rounds=5):
        while self.score[0] < rounds//2 + 1 and self.score[1] < rounds//2 + 1:
            self.evaluate()

In [ ]:
game = RockPaperScissors()
game.start()