0

I have used some posts on here to create a tournament bracket system using JavaScript and Jquery to show the brackets.

I have 16 teams seeded 1-16. At the moment my sorted seeds are as follows:

[1,8],[4,5],[2,7],[3,6]

As you can see, it works to some extent but the semi-finals would then be 1v4 and 2v3, which is incorrect. I would like the semi finals to be 1v3 and 2v4, but no matter what I try I can't seem to get this to print out. So it should be:

[1,8],[3,6],[5,4],[7,2]

This also makes it so when you look at the brackets on paper, it is correct in the sense that 1 and 2 are at opposite ends of the bracket, but no matter what I try, it doesn't work.

EDIT: I would also like this to work for more teams, so a 16/32 team tournament, but I don't seem to ge t the correct seedings at all when I try to do so with this algorithm.

Here is my code:

var seeds = [ 1,2,3,4,5,6,7,8],
    num_rounds = Math.log(seeds.length) / Math.log(2);

// 2-dimensional array
// Each subarray holds the seeds active in that round in order of matches played
// Example:
// seeds in first match of 2nd round are: bracket_round[1][0] & bracket_round[1][1]
var bracket_round = [];

// Create empty arrays inside bracket_round
for(var i = 0; i < num_rounds; i++) {
  bracket_round[i] = [];
}

// Assuming no upsets
// Final is seed 1 and seed 2
bracket_round[num_rounds] = [ seeds[0], seeds[1] ];

// For each round in the bracket
for(var roundNum = num_rounds; roundNum > 0; roundNum--) {
  var round = bracket_round[roundNum];
  var prev_round = bracket_round[roundNum - 1];

  // For each seed in the round, work out who they defeated in previous round, storing result
  for(var m = 0; m < round.length; m++) {
    // round.length = number of matches in the round
    // number of teams in the round will be, number of matches * 2
    var num_teams_in_round = round.length * 2;

    // previous match team A = current match team "m"
    prev_round[m * 2] = round[m];

    // previous match team  B = (# teams in previous round + 1) - (current match seed "m")
    prev_round[(m * 2) + 1] = (num_teams_in_round + 1) - round[m];
  }
}

document.write(bracket_round[1]);

var singleElimData = {
    teams : [              // Matchups
        [ bracket_round[1][0], bracket_round[1][1] ],
    [ bracket_round[1][2], bracket_round[1][3] ],
    [ bracket_round[1][4], bracket_round[1][5] ],
    [ bracket_round[1][6], bracket_round[1][7] ]
  ],
  results : [[
      [ [1, 0], [1, 0], [1,0], [1, 0] ],
      [ [1, 0], [1, 0] ],
      [ [1, 0], [0, 1] ]
    ]
  ]
}

$(function() {
    $('#singleElim').bracket({
        init: singleElimData
    })
})

1 = 0001    0001 = 1
2 = 0010    0011 = 3
3 = 0011    0010 = 2
4 = 0100    0110 = 6
5 = 0101    0111 = 7
6 = 0110    0101 = 5
7 = 0111    0100 = 4
8 = 1111    1000 = 8
germainelol
  • 3,231
  • 15
  • 46
  • 82
  • so you always want the highest seed not already matched to pair off against the lowest seed not already matched? Or do you want random seeding? – Ryan Dec 19 '12 at 19:08
  • and why are you assuming no upsets? Are you going to use this in real life applications or are you doing this just to generate a 'bracket' that will always end the same way? This is important to know actually since it significantly changes the way in which you should approach the problem – Ryan Dec 19 '12 at 19:11
  • I'm unsure of the best way to go about it, I think the best way would be to send each seed to either side of the bracket, so first 1 and 2 are sent to either side of the brackets, then 3 would join 1's side, 4 would join 2's side etc. The other problem I have is that I cannot get 1 and 2 to start on opposite ends of the bracket to make it more readable like normal brackets. – germainelol Dec 19 '12 at 19:13
  • I'm assuming no upsets simply so I can re-create the 1st round brackets. I am trying to seed a tournament for the 1st round matches for 8/16/32 teams basically, any results after that I don't care about, I just filled in results for the sake of it there. – germainelol Dec 19 '12 at 19:14

1 Answers1

0

When you create the tree you can use a gray-code to determine which edge is the player. Basically it's reversing the bits and add 1. Read here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/229068.

Micromega
  • 12,486
  • 7
  • 35
  • 72
  • Sorry but I have no idea about coding in Ruby – germainelol Dec 19 '12 at 19:29
  • Just convert the number of the team to a gray-code and use the bits to traverse the tree. – Micromega Dec 19 '12 at 19:35
  • Doesnt help me I'm afraid, I've added the gray code equivalents in my original post at the bottom. How does this help me with creating a seeded first round bracket? Thanks for the help by the way – germainelol Dec 19 '12 at 19:46
  • You have an error in 8. It is in gray code 1100. But I'm lost either. – Micromega Dec 19 '12 at 20:28
  • Yep sorry my type mistake. But how does this help with putting them in their correct positions? – germainelol Dec 19 '12 at 20:33
  • I've ordere the bits like you but it doesn't seems to solve the problem. In the first round it would mean pair 1v3 but it's wrong. – Micromega Dec 19 '12 at 20:50
  • Usually a gray-code is a hilbert curve traversal of the surface. You can also see it at a quadtree. Read here: http://en.wikipedia.org/wiki/Hilbert_curve – Micromega Dec 20 '12 at 12:48