tags:

views:

476

answers:

2

I need to generate sequences of games using the round robin algorithm. I have the php page where the user can input the tournament name which will be inserted into the database and it has got a drop down menu up to 32 teams (select number of teams).

So if I select 4 teams in the page, so it will be from team 1 to team 4 which would be 6 matches because every team plays the other team once. I know how the algorithm works but I am not quite sure how to write the query for that.

I created the table team:

Team_id    01     02     03     etc
Team_name  Team1  Team2  Team3  etc.

What should I do from here?

+5  A: 

There is a fairly simple algorithm for doing round-robin matchups, my solution would be as follows (in pseudo-code):

  • fetch all the teams out of the database into an array, in any order
  • for (i = 1; i < number of teams; i++)
    • print matchups for Round #i:
    • the teams in the first half of the array are matched up in the same order with the teams in the last half of the array. That is, the team at any index [n] is matched up with the team at index [n + half the number of teams]. If you have 32 teams, [0] is matched with [16], [1] with [17], etc up to [15] and [31].
    • Now, "rotate" the teams through the array, but leave the first one in the array in place. That is, [1] becomes [2], [2] becomes [3], ..., up to [31] becomes [1], but do not move the team at index [0].

And that's it, that will produce all the matchups you need.

An example, with 4 teams:

First half of the array is on top, second half is on the bottom, match-ups are numbers above/below each other. Array indexes (to illustrate what I mean exactly):

[0] [1]
[2] [3]

Round 1:

1 2
3 4

Round 2:

1 4
2 3

Round 3:

1 3
4 2
Chad Birch
Is it possible that you can give me small piece of coding....please???
Siva
My deadline is on friday....I am new to this programming...Can you help me out please????
Siva
I'd rather not. The attempt at code you put in your "answer" shows that you really don't understand what you're doing at all, and just handing you the solution to your homework won't help your situation. All the steps of the algorithm are simple, go read some basic PHP tutorials.
Chad Birch
please CHAD Birch
Siva
hey chad can you just write down the array you explained me, please!
Siva
i think i am on the righ track after your advice: mysql_fetch_array...right
Siva
A: 

Thanks Chad Birch. Any other ideas??? Can someone give a example of the coding please????

Siva
This is not an answer, use the comments or edit your question.
Geoffrey Chetwood
Well, he can't leave a comment with less than 50 rep, I've always thought that was a weird restriction for exactly this reason.
Chad Birch
@Chad: Yes he can, and he did.
Geoffrey Chetwood
Oh, that's strange, is the FAQ just out-of-date then?
Chad Birch
@Chad: You can leave comments on your own posts and on answers to your questions without 50 rep.
Michael Myers
@Chad: Perhaps, but this has been true for a long time.
Geoffrey Chetwood
Alrighty then, The More You Know ~~~~~~~*
Chad Birch