6

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?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Siva
  • 69
  • 1
  • 1
  • 3
  • Isn't this duplicate of: http://stackoverflow.com/questions/631259/is-there-a-way-in-sql-mysql-to-do-a-round-robin-order-by-on-a-particular-field – vartec Mar 18 '09 at 15:53

2 Answers2

21

I created a roundrobin function from scratch as i thought it might be easier to get the same results and also allowing me to use arrays filled with strings directly instead of numbers.

Because i pull a list of names from a database and add into an array i can now schedule this directly with below function. No extra step needed to link numbers to names etc.

Please feel free to try it and if it works then leave a comment. I also have a version which allows for 2 way (home & return) schedule and or shuffle option. If somone is interested in that one then leave a coment as well.

<?php

/**
 * @author D.D.M. van Zelst
 * @copyright 2012
 */

function scheduler($teams){
    if (count($teams)%2 != 0){
        array_push($teams,"bye");
    }
    $away = array_splice($teams,(count($teams)/2));
    $home = $teams;
    for ($i=0; $i < count($home)+count($away)-1; $i++){
        for ($j=0; $j<count($home); $j++){
            $round[$i][$j]["Home"]=$home[$j];
            $round[$i][$j]["Away"]=$away[$j];
        }
        if(count($home)+count($away)-1 > 2){
            array_unshift($away,array_shift(array_splice($home,1,1)));
            array_push($home,array_pop($away));
        }
    }
    return $round;
}
?>

How to use, for example create an array like:

<?php $members = array(1,2,3,4); ?>

or

<?php $members = array("name1","name2","name3","name4"); ?>

then call the function to create your schedule based on above array:

<?php $schedule = scheduler($members); ?>

To display the resulted array schedule simply do like below or anyway you like: This little code displays the schedule in a nice format but use it anyway you like.

<?php
foreach($schedule AS $round => $games){
    echo "Round: ".($round+1)."<BR>";
    foreach($games AS $play){
        echo $play["Home"]." - ".$play["Away"]."<BR>";
    }
    echo "<BR>";
}
?>

Leave a note if it worked for you or if you are interested in the 2-way version with shuffle.

ronalchn
  • 12,225
  • 10
  • 51
  • 61
  • 1
    Worked perfect! +1 Due to an error, I ended up using array_unshift($visitTeams, current(array_splice($homeTeams,1,1)) ); Notice "current" instead of "array_shift" – Jesse C Feb 27 '15 at 08:03
  • 1
    Upvoted. Looks fine and tested, but it has a problem: one team plays home all the time. Is there any way to fix that? I would also be interested in the 2-way version. – Elias Kosunen Mar 22 '15 at 21:04
  • This is pretty cool!! I'd be interesting in seeing the version with home/away option as well if you are able to post that here too thanks :) – Zabs Jul 03 '16 at 23:46
  • it is perfect when number of team is even, but fails when number is odd – Juliatzin Jan 31 '17 at 21:40
9

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
  • 73,098
  • 23
  • 151
  • 149
  • My deadline is on friday....I am new to this programming...Can you help me out please???? – Siva Mar 18 '09 at 17:15
  • 7
    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 Mar 18 '09 at 17:30
  • hey chad can you just write down the array you explained me, please! – Siva Mar 18 '09 at 18:22
  • i think i am on the righ track after your advice: mysql_fetch_array...right – Siva Mar 18 '09 at 20:48
  • 3
    Good algorithm, but it has one problem - one team always plays at home. It would be nice, if one team won't participate more then 2 times away or at home. Is it possible? – maectpo Jan 21 '11 at 12:29