Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

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

share|improve this question
    
If they can't express the requirements to you any more clearly than giving an explicit list of pairings, then there is no better solution than implementing it as an explicit list. Don't try to outguess a client too early. Once they see the complete tree you come up with, they may understand better what it is they want. –  Kilian Foth Sep 24 '14 at 7:03
    
First thing i did was created the arrays myself and did the pairing. So according to the No of players i choose the "right" array. That's the thing though, they can't understand that, their approach is more like we want exactly this. I guess i'll have to stick with premade arrays rather than producing them dyanamically. –  Teris L Sep 24 '14 at 7:15
1  
@TerisL So ask them what the array would be in the case of 8 players, 4 players.. 7 players.. Explain to them that it would be like providing a single number and asking them what the next number in the pattern is. Regardless of whether or not they understand, the reality is this. Don't write a single line of code until they provide better specifications. –  Neil Sep 24 '14 at 7:40
    
I agree with the others, in the absence of a general algorithm that scales with inputs it is not possible to implement this except by hard-coding values. In general, brackets are structured to give favorable matchups to the top seeds. This one appears to work differently, but I am at a loss to explain how. –  Snowman Sep 24 '14 at 17:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.