#!/usr/bin/env python from random import * CONTESTANTS = 15 class Player() : """ A player with a name who knows how many times he/she has won and lost """ def __init__(self, name) : self.name = name def wins(self, this_tourney) : """ Determines how many times the player has won """ win_count = 0 for game in this_tourney.games : if game[0] == self.name : win_count += 1 return win_count def losses(self, this_tourney) : """ Determines how many times the player has lost """ loss_count = 0 for game in this_tourney.games : if game[1] == self.name : loss_count += 1 return loss_count class RoundRobin() : """ A round robin tournament in which everyone plays everyone else once games are kept track of in a list of tuples: [ (a, b), (b, c), (c, a) ] means: a beats b, b beats c, c beats a """ def __init__(self, contestants) : self.valid = True self.wins_per_player = (contestants - 1) / 2 self.contestants = [] for i in range(contestants) : name = "player"+str(i) self.contestants.append(Player(name)) self.games = [] def run(self) : """ Runs the tournament """ for player1 in self.contestants : for player2 in self.contestants : two_players = player1 != player2 not_played = not self.played(player1.name, player2.name) if two_players and not_played : self.playRandom(player1, player2) def played(self, player1, player2) : """ Determines whether two players with the given names have played before """ for game in self.games : if game == (player1, player2) or game == (player2, player1) : return True return False def playRandom(self, player1, player2) : """ Determines the winner of a game randomly unless a player has won or lost 6 times unfortunately this sometimes fails and thus the result must be check for validity """ #print player1.name+" has "+str(player1.wins(self))+" wins and "+player2.name+" has "+str(player2.wins(self)) if player1.wins(self) == self.wins_per_player or player2.losses(self) == self.wins_per_player : game = (player2.name, player1.name) #print player2.name+" wins" else : if player2.wins(self) == self.wins_per_player or player1.losses(self) == self.wins_per_player : game = (player1.name, player2.name) #print player1.name+" wins" else : #print "winner chosen randomly" if random()*2 < 1 : game = (player1.name, player2.name) else : game = (player2.name, player1.name) self.games.append(game) if player2.wins(self) > self.wins_per_player or player1.wins(self) > self.wins_per_player or player2.losses(self) > self.wins_per_player or player1.losses(self) > self.wins_per_player : self.valid = False def playMax6(self, player1, player2) : """ Determines the winner of a game given the rule that no player may win more than 6 times """ if player1.wins(self) == 6 : game = (player2.name, player1.name) else : game = (player1.name, player2.name) self.games.append(game) def playEven(self, player1, player2) : """ Determines the winner of a game given the rule that if a player has won more than lost they must lose """ if player1.wins(self) > player1.losses(self) or player1.wins(self) == 6 : game = (player2.name, player1.name) else : game = (player1.name, player2.name) self.games.append(game) def print_standings(self) : """ Outputs the number of wins and losses each player has """ for player in self.contestants : print player.name+":" print " -wins: "+str(player.wins(self)) print " -losses: "+str(player.losses(self)) def findRPS(games) : """ Given a set of games finds the number of rock-paper-scissors loops """ RPSs = 0 for game1 in games : for game2 in games : for game3 in games : if game1[0] == game2[1] and game2[0] == game3[1] and game3[0] == game1[1] : RPSs += 1 return RPSs/3 # The result must be divided by three because each loop is detected 3 times if __name__ == "__main__" : tournies = [] for i in range(10) : tourney = RoundRobin(CONTESTANTS) tourney.run() if tourney.valid : tournies.append(findRPS(tourney.games)) print str(tournies) #print "players: "+str(tourney.contestants) #print "games: "+str(tourney.games) #games = [("rrrr","cdd"),("a","b"),("b","c"),("c","a"),("f","a"),("s","rt"),("cdd","ass"),("ass","rrrr")] #print str(findRPS(tourney.games)) #print str(tourney.valid)