1

I'm having trouble thinking of a logical way to advance players to a new match in my tournament brackets. You can see the example here: http://www.stickyflames.com/brackets.php?id=3

Matches go from 1 to the number of players playing. (1-8, in the example). Use this first section of the question to see how my matches are set up: How to determinate round by element in tree (Tournament brackets)?

Here's a scenerio to demonstrate: Example1 wins his match, so he gets thrown into match 5, slot 1. While Example2 wins his, and goes into Match 5, slot 2. On the other hand, Example3 and Example4 gets put into M6 slots.

Do any of you know some mathematical algorithm that can calculate the match where they are supposed to end up? (slots can be determined using modulus so not worried about that)

Thanks a bunch.

Community
  • 1
  • 1
TurdPile
  • 976
  • 1
  • 7
  • 21

1 Answers1

1

EDIT: Here is the answer he was looking for. To calculate the rounds based on matches:

Math.round(match# / 2) + Round-1-match-count

Glad you could solve it yourself :]

Another way of looking at it:

Let's redesign the problem here. Right now it seems like you're adapting this sort of tree

1 
      5       
2
          7
3 
      6  
4   

Something like that and you kind of want an algorithm that will get 1/2 to 5, 3/4 to 6, etc. I bet you could make a formula for this but it might be a little more complicated than necessary. If you think about it, 1/2 never has to enter 6, 3/4 never has to enter 5, etc. In fact I think it'd be a lot easier, and accurate, to look at it like this:

0a 
      2a       
1a
          3a
0b 
      2b  
1b   

Idk how your code is formatted right now but if you wanted to differentiate them, you could do a's and b's .

Now the algorithm to move up is just

if (spot == 0) { 
    spot=spot+2;     //we'll move it to the next because 0 is the only exception
} else {             //if odd  
    spot=spot+1;     //ideally we only need to add one to move to the next bracket
}

You can use parseInt to get the number only. But wait! how will it know which slot to go to because they are the same number? That's why we can use the letters. The cool thing about strings is that they are are just an array of characters stringed together. Example:

var word = "Hello";
word[0] = "H";

So you can access the letter by doing spot[1] and that way you can make sure that a's stick with a's and b's stick with b's. Hope this helps.

Oh and one last thing. You might be wondering what about the 3a? There is no b there. Well I'm not entirely sure if you want a letter there anyways but if you did want it, you could just make your code to see if a b exists. If it doesn't, then put it in a because that's the only spot it can go.

aug
  • 11,138
  • 9
  • 72
  • 93
  • Any help is appreciated bud - I shall await final result. This has been on my head for the past couple days and I almost never need help when it comes to logic problems. :D – TurdPile Aug 27 '12 at 22:42
  • Okay I fixed it. I guess in my case there is always gonna be that awkward moment with the 0/1 case. After that you simply need to add 1 to move onto the next phase. I feel that should work. Hope this helps. If there is anything that you need clarifying, just comment! – aug Aug 27 '12 at 22:44
  • Okay, i liked the answer (if that was how I had the tournament set up :P). Unfortunately I don't. As it stands, the database holds: tourney_id | match | p1 | p2 | winner. So for match of 1, p1 would be Example5 and p2 would be Example1. Also - if JavaScript is your comfort language to demo with, that's fine - I am using PHP for this script, which doesn't differ much, and I can convert JS to PHP. – TurdPile Aug 27 '12 at 22:53
  • 1
    Actually I think I found a solution. However, would you know how to figure out the round (round = column1,2,3 etc) by the match number? (round count can vary anywhere between 2 to 64. – TurdPile Aug 27 '12 at 23:25
  • Just to clarify so "round" is like how you have quarter finals, semi-finals, and finals right? If it's for your scenario, you know there's 8 people so there will be 4 matches in the first round. I think after that it's all math and dividing by 2. Maybe you could somehow get how many players are still playing and then just divide by 2. i.e. you had 8 players starting and that's round 1. After people get eliminated there will be only 4 players hence round 2, then it'll be 2 players, round 3, etc. I'll update my answer with a code idea of it. – aug Aug 28 '12 at 00:01
  • 1
    I sat in my bed for the past hour and i found the answer the previous problem.. it's so simple hah. All it is: Math.round(match# / 2) + Round-1-match-count. IE, match 3: 3/2 = 1.5 (rounded = 2), 2+4 = 6. Thinking it was way more complicated than it actually was LOL. --Works globally. So now if i was match 6.. 6/2 = 3+4 = 7. Oh boy. – TurdPile Aug 28 '12 at 00:21
  • Also - you can edit your main answer with that formula and I'll accept the answer. – TurdPile Aug 28 '12 at 00:23
  • HAHA wow I can't believe I didn't think of that. I mean I had that idea but wasn't really sure how that would work and I thought there would be a better way to work around it. Glad you figured it out :] and I updated it. If you need any other help though, let us know! – aug Aug 28 '12 at 00:37
  • But isn't it the same as `next_match_id = (totalTeams/2) + ceil(match_id/2)` you said in your previous question? I wanted to solve it, but when I saw it in the other question I thought that I hadn't understood anything... – Oriol Aug 28 '12 at 00:53
  • Didn't even realize that Oriol LOL - but doesn't seem like it would work for odd numbers since he'd be adding X.5 to a whole number. – TurdPile Aug 28 '12 at 01:33
  • @TurdPile For odd numbers it should work the same because then `match_id/2` will have a decimal part of `.5`. Then `round` and `ceil` do the same because the nearest integrer (`round`) will be the higuer nearest integrer too (`ceil`). – Oriol Aug 28 '12 at 02:28