Last active
August 29, 2015 13:58
-
-
Save felipecruz/10168906 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
#coding: utf-8 | |
ROCK = 1 | |
PAPER = 2 | |
SCISSORS = 3 | |
DRAW = (0, 0) | |
FIRST_WIN = (1, -1) | |
SECOND_WIN = (-1, 1) | |
RESULTS = { | |
(ROCK, ROCK): DRAW, | |
(PAPER, PAPER): DRAW, | |
(SCISSORS, SCISSORS): DRAW, | |
(ROCK, PAPER): SECOND_WIN, | |
(PAPER, ROCK): FIRST_WIN, | |
(ROCK, SCISSORS): FIRST_WIN, | |
(SCISSORS, ROCK): SECOND_WIN, | |
(PAPER, SCISSORS): SECOND_WIN, | |
(SCISSORS, PAPER): FIRST_WIN | |
} | |
class Player: | |
def play(self): | |
pass | |
def receive(self, result): | |
pass | |
def new_game(self, games): | |
pass | |
class AllPaper(Player): | |
@property | |
def name(self): | |
return "AllPaper" | |
def play(self): | |
return PAPER | |
class AllScissors(Player): | |
@property | |
def name(self): | |
return "AllScissors" | |
def play(self): | |
return SCISSORS | |
class AllRocks(Player): | |
@property | |
def name(self): | |
return "AllRocks" | |
def play(self): | |
return ROCK | |
class RandomPlayer(Player): | |
@property | |
def name(self): | |
return "Random" | |
def play(self): | |
from random import randint | |
return randint(1, 3) | |
class Roshambo: | |
def __init__(self, players): | |
self.players = players | |
self.wins = [0] * len(players) | |
self.total = 0 | |
def play(self, p1, p2): | |
try: | |
return RESULTS[(p1, p2)] | |
except KeyError: | |
raise Exception("Jogo inválido") | |
def run(self, games): | |
for i, p1 in enumerate(self.players): | |
for j, p2 in enumerate(self.players): | |
if p1 == p2: | |
continue | |
p1.new_game(games) | |
p2.new_game(games) | |
for k in range(games): | |
self.total+=1 | |
p1_play = p1.play() | |
p2_play = p2.play() | |
result = self.play(p1_play, p2_play) | |
p1.receive(p2_play) | |
p2.receive(p1_play) | |
self.wins[i] += result[0] | |
self.wins[j] += result[1] | |
return self.wins | |
def main(): | |
p1 = AllPaper() | |
p3 = AllScissors() | |
p4 = AllRocks() | |
p2 = RandomPlayer() | |
players = [p1, p2, p3, p4] * 5 | |
roshambo = Roshambo(players) | |
wins = roshambo.run(10000) | |
print("Total games: {}".format(roshambo.total)) | |
for i, player in enumerate(players): | |
print("{}: {}".format(player.name, wins[i])) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment