Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """This program plays a game of Rock, Paper, Scissors between two Players,
- and reports both Player's scores each round."""
- from random import choice
- moves = ['rock', 'paper', 'scissors']
- hand = ['rock', 'paper', 'scissors']
- number_of_rounds = 10
- class Player:
- '''The Player class is the parent class for all of the Players
- in this game'''
- def __init__(self):
- self.score = 0
- def move(self):
- return 'rock'
- def learn(self, my_move, their_move):
- pass
- def beats(one, two):
- return ((one == 'rock' and two == 'scissors') or
- (one == 'scissors' and two == 'paper') or
- (one == 'paper' and two == 'rock'))
- def winner(p1, p2, move1, move2): # p1 is self
- if beats(move1, move2):
- return p1
- elif beats(move2, move1):
- return p2
- else:
- return None
- class RandomPlayer(Player):
- '''Makes moves based on random choices'''
- def move(self):
- return choice(hand)
- class Game:
- def __init__(self, p1, p2):
- self.p1 = p1
- self.p2 = p2
- self.round = 0
- def play_round(self):
- move1 = self.p1.move()
- move2 = self.p2.move()
- print(f"Player 1: {move1} Player 2: {move2}")
- self.p1.learn(move1, move2)
- self.p2.learn(move2, move1)
- win = self.p1.winner(self.p2, move1, move2)
- if not win:
- print('Draw')
- elif win is self.p1:
- print('Player 1 wins')
- self.p1.score += 1
- else:
- print('Player 2 wins')
- self.p2.score += 1
- def play_game(self):
- print("Game start!")
- while self.round < number_of_rounds:
- self.round += 1
- print(f"Round {self.round}:")
- self.play_round()
- print("Game over!")
- if __name__ == '__main__':
- game = Game(RandomPlayer(), RandomPlayer())
- game.play_game()
- game.play_round()
Add Comment
Please, Sign In to add comment