I would like to develop a library that:
- receive n players in input
- response with the tournaments brackets in output.
I would ask you what kind of algorithm I have to use to choose the correct tree that have to be generated for each input.
I am interested on single elimination tournaments, and players of the same club you have to challenge as late as possible.
I found this basic algorithm:
$players = range(1, 3);
$count = count($players);
// Order players.
$max = log($count / 2, 2);
echo "max ".$max."<br>";
for ($i = 0; $i < log($count / 2, 2); $i++) {
$out = array();
foreach ($players as $player) {
$splice = pow(2, $i);
$out = array_merge($out, array_splice($players, 0, $splice));
$out = array_merge($out, array_splice($players, -$splice));
}
$players = $out;
}
// Print match list.
for ($i = 0; $i < $count; $i++) {
printf('%s vs %s<br />%s', $players[$i], $players[++$i], PHP_EOL);
}
What do you think? Is it a good solution?
The second question is how I can draw the bracket tree, any suggestion?
I have to use html/javascript.
Thank you.
2