# Simple Rock-Paper-Scissors to get more comfortable with OOP

I'm trying to get a little more comfortable with using classes as they're meant to be used, instead of using them to decorate my code to make it look more legitimate. Can you see anything glaringly wrong/could be improved/possibly different implementations?

import random

class Player(object):

def __init__(self,player):
self.name = player
self.score = 0
self.choice = []

class Game(object):

player_one,player_two = Player('player_one'),Player('player_two')

def gameplay(self):
choices = ['rock','paper','scissors']
self.player_one.choice,self.player_two.choice = random.choice(choices),random.choice(choices)
if self.player_one.choice == self.player_two.choice:
print 'Tie!'
elif self.player_one.choice == 'rock' and self.player_two.choice == 'paper':
print '{0} is the winner with {1}!'.format(self.player_one.name,self.player_one.choice)
self.player_one.score += 1
elif self.player_one.choice == 'paper' and self.player_two.choice == 'rock':
print '{0} is the winner with {1}!'.format(self.player_two.name,self.player_two.choice)
self.player_two.score += 1
elif self.player_one.choice == 'scissors' and self.player_two.choice == 'rock':
self.player_two.score += 1
elif self.player_one.choice == 'scissors' and self.player_two.choice == 'paper':
self.player_one.score += 1
elif self.player_one.choice == 'paper' and self.player_two.choice == 'scissors':
self.player_two.score += 1
elif self.player_one.choice == 'rock' and self.player_two.choice == 'scissors':
self.player_one.score += 1
print '{0}\'s score is {1}, and {2}\'s score is {3}'.format(self.player_one.name,self.player_one.score,self.player_two.name,self.player_two.score)

g = Game()
for i in range(0,10000):
g.gameplay()

• I have rolled back the last edit. Please see What to do when someone answers. – Mast Mar 23 '16 at 18:36
• oops, my mistake! – n1c9 Mar 23 '16 at 18:38

I think that one another class Choice will be useful. You have two functionality to move to this class:

• make random choice. You can simple write def make_random(self):
• compare two choices. You can define standard operators def __lt__(self, oth): and def __gt__(self, oth):. If you want handle ties than __eq__ operator will be useful also.

Then your gameplay function could be like

def gameplay(self):
self.player_one.choice.make_random()
self.player_two.choice.make_random()
if self.player_one.choice > self.player_two.choice:
self.player_one.score += 1
elif self.player_one.choice < self.player_two.choice:
self.player_two.score += 1


Having choice a member of Player is a doubtful decision. The player is not interested in it at all - the choice is produced and forgotten. It is Game that uses it. On the other hand, Player may benefit from the play() method returning a choice:

    def gameplay(self):
choice1 = player1.play()
choice2 = player2.play()
decide_winner(choice1, choice2)
....


Supporting a human, or AI, or any other play strategy, becomes trivial.