Created
August 5, 2014 16:40
-
-
Save kennethlove/2dc504f96d6763b3319d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice | |
from moves import Rock, Paper, Scissors | |
class Game: | |
def __init__(self, rounds=3): | |
self.rounds = rounds | |
self.player = input("What's your name? ") | |
self.score = [0, 0] | |
def _convert_move(self, move): | |
if move == 'r': | |
return Rock() | |
elif move == 'p': | |
return Paper() | |
elif move == 's': | |
return Scissors() | |
def get_choice(self): | |
move = input("[R]ock, [P]aper, [S]cissors? ").lower() | |
if move not in list('rps'): | |
return self.get_choice() | |
return self._convert_move(move) | |
def game_round(self): | |
player_move = self.get_choice() | |
computer_move = self._convert_move(choice(list('rps'))) | |
if player_move > computer_move: | |
self.score[0] += 1 | |
print("You won that round, {}!".format(self.player)) | |
elif computer_move > player_move: | |
self.score[1] += 1 | |
print("You lost that round, {}!".format(self.player)) | |
else: | |
print("You tied!") | |
if __name__ == '__main__': | |
game = Game() | |
while 3 not in game.score: | |
game.game_round() | |
else: | |
if game.score[0] == 3: | |
print("{} wins!".format(game.player)) | |
else: | |
print("{} lost!".format(game.player)) | |
print("Final score: {}: {}; Computer: {}".format(game.player, game.score[0], game.score[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Move: | |
better_than = None | |
worse_than = None | |
def __gt__(self, other): | |
return other.__class__.__name__ in self.better_than | |
def __lt__(self, other): | |
return other.__class__.__name__ in self.worse_than | |
def __eq__(self, other): | |
return type(other) == self.__class__ | |
def __ne__(self, other): | |
return type(other) != self.__class__ | |
class Rock(Move): | |
better_than = ['Scissors'] | |
worse_than = ['Paper'] | |
class Paper(Move): | |
better_than = ['Rock'] | |
worse_than = ['Scissors'] | |
class Scissors(Move): | |
better_than = ['Paper'] | |
worse_than = ['Rock'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to type
ROCK
.