I'm learning python and I want to model a single elimination tournament like they use in athletic events like tennis, basketball, etc. The idea would be to insert some meaningful metrics to determine the winner. I've already got a database that I plan to connect to to get the metrics, but I am hoping to find a better way to "process" games in the tournament to move the winner to the next round, and eventually find the tournament winner.
So far this is the best I have come up with, but it doesn't scale (at all easily) to 32, 64, 128 entries & so-on. The "primary key" that I need to retain is the "seed id" (1-16, in this case), which matches the unique identifier in the database, so that I can pull the correct metrics for deciding which entry will win a matchup. Suggestions?
## this list represents the round 1 seeding order on the tournament sheet ## this doesnt easily scale to 32, 64, 128 entries teamlist = [1,16,8,9,5,12,4,13,6,11,3,14,7,10,2,15] #In a single elim tournament with a full field, # of games is # of teams-1 totalgames = len(teamlist) - 1 #Set defaults gameid = 0 roundid = 0 nextround =  #simulate all of the games while gameid < totalgames: if gameid in [8,12,14]: ##this is a manual decision tree, doesn't scale at all #if a new round begins, reset the list of the next round print "--- starting a new round of games ---" teamlist = nextround nextround =  roundid = 0 #compare the 1st entry in the list to the 2nd entry in the list homeid = teamlist[roundid] awayid = teamlist[roundid + 1] #the winner of the match become the next entry in the nextround list #more realistic metrics could be substituted here, but ID can be used for this example if homeid < awayid: nextround.append(homeid) print str(homeid) + " vs " + str(awayid) + ": The winner is " + str(homeid) else: nextround.append(awayid) print str(homeid) + " vs " + str(awayid) + ": The winner is " + str(awayid) #increase the gameid and roundid gameid += 1 roundid += 2 print "next round matchup list: " + str(nextround) print nextround