Search code examples
pythonpython-itertools

competition code for round-robin style matches


i am trying to write a competition code that will get a list of competitors and then will randomly decide who they must face at each round, round-robin style without eliminations. wiki article about this type of competition

at the end of each round the winner of each match will receive a point.

when all possible fights are finished, the one with the most points wins.

but i am having some trouble, here is my code so far:

import itertools

# list of competitors (does not need to be even, in this case, 7)
# maybe this should be a dict with their wins and loses like:
# {'Andy': [2,['Bob','Charlie'],0,[]], ... }
# which means andy won 2 times, against bob and charlie, and lost 0 times
competitors = ['Andy', 'Bob', 'Charlie', 'Daniel', 'Eric', 'Ferdinand', 'Gabriel']
# the starting round number
round_number = 1
# holds the winner
winner = None

# start the competition
print "The competitors are: \n" + " ".join(competitors)

# round handler
def do_round():
    #round notifier
    print "Round " + str(round_number)

    #here is the problem
    matches = itertools.permutations(competitors,2)
    for match in matches: 
        print match

    # increase round number
    round_number += 1
    #return this rounds matches
    return matches

# gets the winners of each round for each match 
def get_winners(matches):
    winners = []
    for match in matches:
        winners.append(raw_input("who won?: " + " or ".join(match)))         

# decides if the games are over yet
def is_there_a_winner(winners):
    # this function should know how to get every rounds winners and add their points
    # and then decide if the game is over and there is a total winner
    winner = ??


# main loop
while not winner:
    matches = do_round()
    get_winners(matches)
    is_there_a_winner(winners)

Edit: sorry the question was asked before i could write this part for some reason.

my problem is that the permutations gives ALL POSSIBLE permutations, i just want to get permutations of the competitors for a single round, and then next time i run it, to be able to reference who they have already fought against and not have that match come up again.

Edit2: i decided to add a "desired result" to my post.

i want the output to be something like this:

The competitors are: 
Andy Bob Charlie Daniel Eric Ferdinand Gabriel
Round 1
Andy     vs   Bob
Charlie  vs   Daniel
Eric     vs   Ferdinand
Gabriel sits out
Round 1 Results:
Andy     beat Bob
Charlie  beat Daniel
Eric     beat Ferdinand
Round 2
Andy     vs   Daniel
Bob      vs   Gabriel
Eric     vs   Charlie
Ferdinand sits out

... etc etc ... until at the end the winner (highest score) is revealed.

Solution

  • i found an answer here:

    https://stackoverflow.com/a/11246261/1561176

    this does what i needed, just need to input my own values now. and modify the way it does the output.

    probably should have tried looking deeper, thanks for the help here everyone.