I\'m making a HTML/JS powered single/double elimination bracket web app. I am struggling to figure out how to assign the first round matches from a list of seeded teams/players.
Here's the algorithm I've developed. Step 1 is to draw a table with as many rows as there are teams (rounded up to a power of 2) and as many columns as needed to represent the number of teams in binary. Say, there are 8 teams. The table will initially look like this (the dots represent horizontal cell borders):
. . . | | | | . . . | | | | . . . | | | | . . . | | | | . . . | | | | . . . | | | | . . . | | | | . . . | | | | . . .
Columns are numbered from the left in ascending order. For each column put an asterisk at every 2^(column number) row. That is, every 2nd row in column 1, every 4th row in column 2 etc.
. . . | | | | . . . | | | | * . . | | | | . . . | | | | * * . | | | | . . . | | | | * . . | | | | . . . | | | |
Start with a 0 in each column in the 1st row. Thereafter, for successive rows in each column toggle from 0-1 and 1-0 unless there is an asterisk in that row. This is the result:
. . . |0|0|0| . . . |1|1|1| * . . |1|0|0| . . . |0|1|1| * * . |0|1|0| . . . |1|0|1| * . . |1|1|0| . . . |0|0|1|
The last step is to evaluate each row treating the string of 0's and 1's as a binary number. This will result in values from 0-7. Adding 1 to each results in values from 1-8. These correspond to the seedings.
. . . |0|0|0| + 1 = 1 . . . |1|1|1| + 1 = 8 * . . |1|0|0| + 1 = 5 . . . |0|1|1| + 1 = 4 * * . |0|1|0| + 1 = 3 . . . |1|0|1| + 1 = 6 * . . |1|1|0| + 1 = 7 . . . |0|0|1| + 1 = 2
Each pair of seeds are the matches to be played in order. ie. 1-8, 5-4, 3-6 and 7-2. This is extendable to any number of seeds. When byes are to be inserted due to the number of entries being less than a power of 2, they take the highest seed values. eg. if there were only 28 entries then byes take the positions assigned to 29, 30, 31 and 32.