0

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.
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131

3 Answers3

2

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.

Community
  • 1
  • 1
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
0

Instead of using permutations, how about something like:

import copy
import random
# Setup:
opponents = dict()
for competitor in competitors:
    opponents[competitor] = copy.deepcopy(competitors)

def do_round():
    players = copy.deepcopy(competitors)
    while players:
        p1 = players.pop(0)
        p2 = random.choice(opponents[p1])
        opponents[p2].remove(p1)
        opponents[p1].remove(p2)
        players.remove(p2)
        play_match(p1, p2)

for _ in range(len(competitors)):
    do_round()

If the matches are not to be played in parallel, it is much easier to create a schedule

Community
  • 1
  • 1
Kimvais
  • 38,306
  • 16
  • 108
  • 142
  • wouldnt this stand the chance of let say picked for andy and getting bob, and then picking for bob and getting charlie... – Inbar Rose Aug 08 '12 at 07:52
  • gives an error `line 15, in do_round opponents[p1].remove(p2) ValueError: list.remove(x): x not in list` – Inbar Rose Aug 08 '12 at 08:34
0

You say "randomly decide who they must face at each round", but you could just follow the round robin algorithm described in the wikipedia article, that is not random, but ensures that for n players, after (n-1) rounds, each players has encountered all the others.

MatthieuW
  • 2,292
  • 15
  • 25
  • thats also fine, but how to implement that in code? how can i devide the distribution in matches so i can display each rounds line-up – Inbar Rose Aug 08 '12 at 08:51