class Element: def __init__(self, name): self._name=name def name(self): return self._name def compareTo(self, Element): raise NotImplementedError("Not yet implemented") class Rock(Element): def compareTo(self, input): if input._name == 'Rock': return 'Rock equals rock', 'Round was a tie' if input._name == 'Paper': return 'Paper covers rock', 'Player 2 won the round' if input._name == 'Scissors': return 'Rock crushes scissors', 'Player 1 won the round' if input._name == 'Spock': return 'Spock vaporizes rock', 'Player 2 won the round' if input._name == 'Lizard': return 'Rock crushes lizard', 'Player 1 won the round' class Paper(Element): def compareTo(self, input): if input._name == 'Rock': return 'Paper covers rock', 'Player 1 won the round' if input._name == 'Paper': return 'Paper equals paper', 'Round was a tie' if input._name == 'Scissors': return 'Scissors cut paper', 'Player 2 won the round' if input._name == 'Spock': return 'Paper disproves Spock', 'Player 1 won the round' if input._name == 'Lizard': return 'Lizard eats paper', 'Player 2 won the round' class Scissors(Element): def compareTo(self, input): if input._name == 'Rock': return 'Rock crushes scissors', 'Player 2 won the round' if input._name == 'Paper': return 'Scissors cut paper', 'Player 1 won the round' if input._name == 'Scissors': return 'Scissors equals scissors', 'Round was a tie' if input._name == 'Spock': return 'Spock smashes scissors', 'Player 2 won the round' if input._name == 'Lizard': return 'Scissors decapitate lizard', 'Player 1 won the round' class Lizard(Element): def compareTo(self, input): if input._name == 'Rock': return 'Rock crushes lizard', 'Player 2 won the round' if input._name == 'Paper': return 'Lizard eats paper', 'Player 1 won the round' if input._name == 'Scissors': return 'Scissors decapitate lizard', 'Player 2 won the round' if input._name == 'Spock': return 'Lizard poisons Spock', 'Player 1 won the round' if input._name == 'Lizard': return 'Lizard equals lizard', 'Round was a tie' class Spock(Element): def compareTo(self, input): if input._name == 'Rock': return 'Spock vaporizes rock', 'Player 1 won the round' if input._name == 'Paper': return 'Paper disproves Spock', 'Player 2 won the round' if input._name == 'Scissors': return 'Spock smashes scissors', 'Player 1 won the round' if input._name == 'Spock': return 'Spock equals Spock', 'Round was a tie' if input._name == 'Lizard': return 'Lizard poisons Spock', 'Player 2 won the round' rock=Rock('Rock') paper=Paper('Paper') scissors=Scissors('Scissors') lizard=Lizard('Lizard') spock=Spock('Spock') moves = [rock, paper, scissors, lizard, spock] import random class Player: def __init__(self, name): self._name=name def name(self): return self._name def play(): raise NotImplementedError("Not yet implemented") class StupidBot(Player): def play(self): return lizard class RandomBot(Player): def play(self): randombit = random.randint(1,5) if randombit == 1: return rock elif randombit == 2: return scissors elif randombit == 3: return paper elif randombit == 4: return lizard elif randombit == 5: return spock class IterativeBot(Player): currentMove = 1 def play(self): if currentMove == 1: return rock elif currentMove == 2: return scissors elif currentMove == 3: return paper elif currentMove == 4: return lizard elif currentMove == 5: return spock currentMove++ class LastPlayBot(Player): pass class Human(Player): pass class MyBot(Player): def play(self): randombit = random.randint(1,12) if randombit == < 9: return rock elif randombit == 9: return scissors elif randombit == 10: return paper elif randombit == 11: return lizard elif randombit == 12: return spock class Main: print ("Welcome to Rock, Paper, Sissors, Lizard, Spock, implemented by Bruce Johnson.") print ("") print ("Please choose two players:") print (" (1) Human") print (" (2) StupidBot") print (" (3) RandomBot") print (" (4) IterativeBot") print (" (5) LastPlayBot") print (" (6) MyBot") print ("") p1choice = input("Select player 1: ") print ("") p2choice = input("Select player 2: ") print ("") if p1choice == 1: p1 = Human('Human') elif p1choice == 2: p1 = StupidBot('StupidBot') elif p1choice == 3: p1 = RandomBot('RandomBot') elif p1choice == 4: p1 = IterativeBot('IterativeBot') elif p1choice == 5: p1 = LastPlayBot('LastPlayBot') elif p1choice == 6: p1 = MyBot('MyBot') if p2choice == 1: p2 = Human('Human') elif p2choice == 2: p2 = StupidBot('StupidBot') elif p2choice == 3: p2 = RandomBot('RandomBot') elif p2choice == 4: p2 = IterativeBot('IterativeBot') elif p2choice == 5: p2 = LastPlayBot('LastPlayBot') elif p2choice == 6: p2 = MyBot('MyBot') print ("") print (p1.name()+" vs "+p2.name()+". Go!") for i in range(1, 5): print ("Round "+i+": ") print ("Player 1 chose " + p1.play.name()) print ("Player 2 chose " + p2.play.name()) print p1.play.compareTo(p2.play())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question