I'm in the making of a tournament for archery but i'm stuck because they want a specific match-matching for players. There is the usual 1-32,2-31,3-30 etc but the way they want the tree is a specific one.
For now i'm producing the array with this
$players = range(1, $range);
$count = count($players);
for ($i = 0; $i < log($count / 2, 2); $i++) {
echo $i;
echo '<br>';
print "<pre>";
print_r($players);
print "</pre>";
$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;
}
With an output of
Array
(
[0] => 1
[1] => 16
[2] => 8
[3] => 9
[4] => 4
[5] => 13
[6] => 5
[7] => 12
[8] => 2
[9] => 15
[10] => 7
[11] => 10
[12] => 3
[13] => 14
[14] => 6
[15] => 11
)
Everything works great except they want the tree like this
Array
(
[0] => 1
[1] => 16
[2] => 9
[3] => 8
[4] => 5
[5] => 12
[6] => 13
[7] => 4
[8] => 3
[9] => 14
[10] => 11
[11] => 6
[12] => 7
[13] => 10
[14] => 15
[15] => 2
)
You will notice some differences between those to. For example 2-15 match is at position 8 and 9 of the array while in the second one it's at 14-15.
I've been busting my head to find an algorithm for this but i'm stuck.
Thanks