开发者

Recursive Algorithm for Tournament Brackets

I am creating a tournament bracket creation script, and I am getting stuck with tournaments with the number of players Non - Power of 2. For instance, it is currently working great for 2, 4, 8, 16, 32,... etc. But for 3, 5, 6, 7... I am stumped!

Here is what I have so far:

$numrows = 8; // Currently for T开发者_开发知识库esting.
draw_bracket($numrows);

function draw_bracket($numplayers)
{
    draw_series_container();
    draw_bracket_layer($numplayers, 0);
    draw_series_container_end();
}

/* RECURSION, YO! */
function draw_bracket_layer($numplayers, $layer)
{
    $levels = floor(log($numplayers, 2));
    if ($layer == $levels - 1)
    {
        draw_player_box();
        draw_player_box();
    }
    else
    {
        draw_series_container();
            draw_bracket_layer($numplayers, $layer + 1);
        draw_series_container_end();

        draw_series_container();
            draw_bracket_layer($numplayers, $layer + 1);
        draw_series_container_end();
    }
}

I am assuming we need more 'base cases'. Or perhaps more if statements for these off cases.

The functions draw_series_container() and draw_player_box are just filled with HTML code. I can show them if we need to.

Could someone start me out with a bracket with 5 or 6 people?

Thanks!


Correct formula of levels is
$levels = intval(log($numplayers, 2))+1;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜