比赛括号算法

时间:2009-08-18 14:54:18

标签: asp.net

我需要创建一个自动生成括号锦标赛网球风格的asp.net页面。 关于数据库中匹配的管理,这不是问题。

问题是括号的动态图形创建。 用户将能够通过2-4 ... 32名玩家创建锦标赛。 我不知道在html或gdi中创建图形括号......

1 个答案:

答案 0 :(得分:8)

使用Silverlight和Grid,您可以生成以下内容: bracket display

为此,请定义包含Grid的常规UserControl。 (这是使用Silverlight 3.0 SDK在VS2008中构建silverlight应用程序时的默认设置。)

然后,在用户控件的构造函数中添加对以下内容的调用:

private void SetupBracket(int n)
{
    var black = new SolidColorBrush(Colors.Gray);
    // number of levels, or rounds, in the single-elim tourney
    int levels = (int)Math.Log(n, 2) + 1;
    // number of columns in the Grid.  There's a "connector"
    // column between round n and round n+1.
    int nColumns = levels * 2 - 1;

    // add the necessary columns to the grid
    var cdc = LayoutRoot.ColumnDefinitions;
    for (int i = 0; i < nColumns; i++)
    {
        var cd = new ColumnDefinition();
        // the width of the connector is half that of the regular columns
        int width = ((i % 2) == 1) ? 1 : 2;
        cd.Width = new GridLength(width, GridUnitType.Star);
        cdc.Add(cd);
    }
    var rdc = LayoutRoot.RowDefinitions;

    // in the grid, there is one row for each player, and 
    // an interleaving row between each pair of players. 
    int totalSlots = 2 * n - 1;
    for (int i = 0; i < totalSlots; i++)
    {
        rdc.Add(new RowDefinition());
    }

    // Now we have a grid of the proper geometry.  
    // Next: fill it. 

    List<int> slots = new List<int>();
    ImageBrush brush = new ImageBrush();
    brush.ImageSource = new BitmapImage(new Uri("Bridge.png", UriKind.Relative));

    // one loop for each level, or "round" in the tourney.  
    for (int j = 0; j < levels; j++)
    {
        // Figure the number of players in the current round.
        // Since we insert the rounds in the reverse order, 
        // think of j as the "number of rounds remaining."
        // Therefore, when j==0, playersThisRound=1.  
        // When j == 1, playersThisRound = 2.  etc.
        int playersThisRound = (int)Math.Pow(2, j);

        int x = levels - j;
        int f = (int)Math.Pow(2, x - 1);

        for (int i = 0; i < playersThisRound; i++)
        {
            // do this in reverse order.  The innermost round is 
            // inserted first.
            var r = new TextBox();
            r.Background = black;
            if (j == levels - 1)
                r.Text = "player " + (i + 1).ToString();
            else
                r.Text = "player ??";

            // for j == 0, this is the last column in the grid.
            // for j == levels-1, this is the first column.
            // The grid column is not the same as the current
            // round, because of the columns used for the 
            // interleaved connectors.
            int k = 2 * (x - 1); 
            r.SetValue(Grid.ColumnProperty, k);

            int m = (i * 2 + 1) * f - 1;
            r.SetValue(Grid.RowProperty, m);
            LayoutRoot.Children.Add(r);

            // are we not on the last round? 
            if (j > 0)
            {
                slots.Add(m);
                // Have we just inserted two rows?  Then we need
                // a connector between these two and the next 
                // round (the round previously added).
                if (slots.Count == 2)
                {
                    string xamlTriangle = "<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' "+
                                          "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " +
                                          "Data='M0,0 L 100 50 0 100 Z' Fill='LightBlue' Stretch='Fill'/>";

                    Path path = (Path)System.Windows.Markup.XamlReader.Load(xamlTriangle);

                    path.SetValue(Grid.ColumnProperty, 2 * (x - 1) + 1);
                    path.SetValue(Grid.RowProperty, slots[0]);
                    path.SetValue(Grid.RowSpanProperty, slots[1] - slots[0] + 1);
                    this.LayoutRoot.Children.Add(path);
                    slots.Clear();
                }
            }

        }
    }
}

在上面,连接器只是一个等腰三角形,顶点指向右边。它由字符串上的XamlReader.Load()生成。

我想你也想要漂亮,用不同的颜色和字体来设计它。

您可以将此Silverlight“用户控件”插入任何HTML网页,例如将Flash应用程序嵌入到页面中。 IE,Firefox,Opera,Safari和Chrome都有Silverlight插件。

如果您不想使用Silverlight,可以使用类似的方法构建HTML表。