Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created July 8, 2013 14:54
Show Gist options
  • Save bbengfort/5949539 to your computer and use it in GitHub Desktop.
Save bbengfort/5949539 to your computer and use it in GitHub Desktop.
A random bracket generator that we have used to create brackets for three hole washers.
#!/usr/bin/env python
from copy import copy
from random import randint
class Names(object):
def __init__(self, path="names.txt"):
self.path = path
def __iter__(self):
with open(self.path, 'r') as data:
for line in data:
line = line.strip()
yield line
def copy(self):
return copy(list(self))
class BracketGenerator(object):
def __init__(self, names=Names()):
self.teams = self.generate_teams(names)
self.rounds = self.generate_games(self.teams)
def generate_teams(self, names):
unmatched = names.copy()
matched = []
if len(unmatched) % 2 != 0:
raise Exception("Not an even number of players!")
while len(unmatched) > 0:
one = unmatched.pop(randint(0, len(unmatched)-1))
two = unmatched.pop(randint(0, len(unmatched)-1))
matched.append((one, two))
return matched
def generate_games(self, teams):
teams = copy(teams)
rounds = {}
game = 1
if len(teams) % 2 != 0:
raise Exception("Not an even number of teams!")
while len(teams) > 0:
one = teams.pop(randint(0, len(teams)-1))
two = teams.pop(randint(0, len(teams)-1))
players = tuple(one) + tuple(two)
rounds["Game %i" % game] = self.Round(*players)
game += 1
return rounds
def __str__(self):
output = []
for name, game in self.rounds.items():
output.append(name)
output.append(str(game))
return "\n".join(output)
class Round(object):
def __init__(self, *players):
"""
Note that player order matters, players 0 and 1 are on the
same team and players 2 and 3 are on the same team.
"""
self.winner = None
self.loser = None
if len(players) != 4:
raise Exception("Need four players!")
self.players = tuple(players)
@property
def team_red(self):
return self.players[0], self.players[1]
@property
def team_blue(self):
return self.players[2], self.players[3]
def __str__(self):
output = []
output.append("Red Team: %s and %s" % self.team_red)
output.append("Blue Team: %s and %s" % self.team_blue)
output.append("")
if self.winner:
output.append("%s wins!" % self.winner)
output.append("")
return "\n".join(output)
if __name__ == "__main__":
bracket = BracketGenerator()
print bracket
Andy Keller
Benjamin Bengfort
Randy Bengfort
Gene Hornback
Elaine Hornback
Bethany Bengfort
Tony Miller
John Bengfort
Anita Greden
Larry Greden
Betty Courtney
Wayne Courtney
Taylor Courtney
Lily Bengfort
Jaci Bengfort
Darlene Buchheit
@bbengfort
Copy link
Author

Bracket Generator

This quick bracket generator is used to generate random two person teams and assign the teams randomly to games to instantiate a bracket. This is especially useful for games like three-hole washers, horseshoes, etc. Anything where you would have two person teams.

Usage

  1. Download the bracket.py file and then in the same folder create a file called names.txt.
  2. In names.txt create a line-delimited list of names (e.g. put one name on each line)
  3. Run $ python bracket.py in the terminal to print out a list of games.

Tips

  1. Ensure you have an even number of players.
  2. Ensure that you have a number of players that is divisible by four.
  3. Be sportsmanlike when you start playing the game!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment