Tournament bracket

Not sure how best to do this? I want to create a tournament bracket from groups 2,4,8,16,32, etc.

The winner of the first two will play the winner of the next 2, etc. Until there is a winner. Like this

Can anybody help me?

OK, so more info.

First I want to come up with a way to create a tournament with 2,4,8,16, etc. Then, when I have all the users in place, if they have 16 players, there are 8 fixtures. At this point I will send the instrument to the database.

When all the winning players go through to the next round, I want to request another sql query again for the 2 winners that meet.

Do you understand what I mean?

+4
source share
2 answers

I did something like this a few years ago. This was a long time ago, and I'm not sure that I will do it in different ways (in fact, it does not scale to a double exception or the like). How can you conclude, this may be another question. I resorted to tables, as it was in 2002-2003. Today, there are certainly better methods.

The number of rounds in the tournament is log2 (players) + 1 if the players are one of the numbers you have indicated. Using this information, you can count the number of rounds. The final round contains the final winner.

I saved player info something like this (tweek this is for best practices)

Tournament Name Size Players Tournament Name Position (0 to tournament.size - 1) Rounds Tournament Round Position (max halves for each round) Winner (player position) 

Please note that in all my queries I do not include "Tournament = [tournament]" to define a tournament. They need all this.

Quite simple to request this with a single request and split it as needed for different rounds. You could do something similar to get the next opponent (assuming he is). For the first round, you just need to get the next / previous player if he was even or odd:

 SELECT * FROM Players WHERE Position = PlayerPosition + 1 SELECT * FROM Players WHERE Position = PlayerPosition - 1 

In the next round, if the user was last Round.Position for the last time, you need to make it suer that the next up position is the winner: SELECT Player FROM Rounds WHERE Position = [playerRoundPosition] - 1

If not, the next player will not be resolved, or there is a space (do not allow spaces!)

If the last time Round.Position users were odd, you need to make sure that they have a user and that there is a lower winner, otherwise they should be automatically transferred to the next round (since there is no one to play)

 SELECT COUNT(*) FROM Players WHERE Position > [Player.Position] SELECT Player FROM Rounds WHERE Position = [playerRoundPosition] + 1 

In a final note, I'm sure you could use something like the following to reduce the queries you write using something like:

 SELECT Player FROM Rounds WHERE Position + Position % 2 = [playerRoundPosition] SELECT Player FROM Rounds WHERE Position - Position % 2 = [playerRoundPosition] 

Update:

After looking at my original post, I found that the Rounds table was a bit vague. In fact, it should be called matches. A match is a competition between two players with a winner. The final table should look bigger (only the name has changed): Matches Tournament Round Position (maximum half for each round) Winner (player position)

Hope this makes it more clear. When both players rise up against each other (in a match), you store this information in this “Matches” table. This particular implementation depends on the position of the match in order to know which players participated.

I started numbering rounds at 1 because it was more clear in my implementation. You can choose 0 (or even do something completely different, like backtracking) if you choose.

In the first round, match 1 means that players 1 and 2 participated. Match 2 involved 3-4 players. In fact, the first round is simply the position of the players and the position + 1 participation. You can also save this information in the round table if you need more access to it. Every time I used this data in the program, I still need all the information about the round and the players.

After the first round, you watch the last round of matches. In round 2, 1st, winners of matches 1 and 2 are participating. In the 2nd round, in match 2, winners from 3 and 4 matches are participating. It should look pretty familiar, except that it uses the correspondence table after round 1. I’m sure there is a more efficient way to accomplish this repetitive task, I just didn’t have enough time to reorganize this code (it was reorganized, just not so much).

+6
source

Use arrays and remove the losing teams from the main array. (But keep them in a separate array for reference and reuse purposes).

0
source

Source: https://habr.com/ru/post/1308297/


All Articles