1

I have 38 days and 20 clubs (EPL).

How can I generate not repeated matches for this clubs in this days (schedule)?

For example:


Day 1:

club1 - club2

club3 - club4

...

club19 - club 20


Day 2:

club1 - club3

club2 - club4

...

club20 - club18


Each club plays with other two games (home and away). Respectively do not play with himself.


My thinks:

    $clubs1 = array();
    $clubs2 = array();
    $days = range(1, 38);
    $calendar = array();
    $pars = array();

    $rows = Yii::app()->db->createCommand()
        ->select('id')
        ->from('clubs')
        ->queryAll();

    foreach ($rows as $item) {
        $clubs1[] = $item['id'];
        $clubs2[] = $item['id'];
    }


    shuffle($clubs1);
    shuffle($clubs2);
    $total = (count($clubs1) * 2) - 2;

    for ($j = 1; $j <= $total; $j ++) {
        $day = $days[$j];
        for ($i = 0; $i < count($clubs1); $i++) {

          WHAT I SHOULD DO IN THIS BODY?

        }
    }
Bohdan Vorona
  • 685
  • 1
  • 13
  • 26
  • http://stackoverflow.com/questions/658727/how-can-i-generate-a-round-robin-tournament-in-php-and-mysql – Steve Sep 09 '14 at 10:47

3 Answers3

1

You need only one clubs array

1) remove $clubs2
2) rename $clubs1 to $clubs
3) remove whole for structure

    //for testing: $clubs=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
    $countofteams=count($clubs);
    $c=1;
    for($j=0;$j<2;$j++) //home/away
        for($i=1;$i<$countofteams;$i++){ //move teams
        echo '----DAY '.$c++.'----<br>';
            for($a=0;$a<$countofteams;$a++) //all teams are playing
                echo 'Match '.$clubs[$a].' vs '.$clubs[($a+$i)%$countofteams].'<br>';
    }
maskacovnik
  • 3,080
  • 5
  • 20
  • 26
1
    $team = array();
    $pars = array();

    $rows = Yii::app()->db->createCommand()
        ->select('id')
        ->from('clubs')
        ->queryAll();
    foreach ($rows as $k => $item) {
        $team[$k+1] = $item['id'];
    }

    $all_team = count($team);
    $k = $all_team/2;

    $days = range(7, 100, 2); // first halh of season
    $days2 = range(55, 100, 2); // second half

    // 1 tour
    for ($i=1; $i <= $k; $i++) {
        $pars[] = $days[0].'|'.$team[$i].'|'.$team[($all_team-$i+1)];
        $pars[] = $days2[0].'|'.$team[($all_team-$i+1)].'|'.$team[$i];
    }

    // Next tours
    for($i=2; $i<$all_team; $i++)
    {

        $team2 = $team[2];

        for($y=2;$y<$all_team;$y++)
        {
            $team[$y] = $team[$y+1];
        }
        $team[$all_team] = $team2;

        for($j=1;$j<=$k;$j++)
        {
            $pars[] = $days[$i - 1].'|'.$team[$j].'|'.$team[($all_team-$j+1)];
            $pars[] = $days2[$i - 1].'|'.$team[($all_team-$j+1)].'|'.$team[$j];
        }

    }
Bohdan Vorona
  • 685
  • 1
  • 13
  • 26
0

Here's my solution, replace the $clubs array with your result set of club IDs from database. The only slight inaccuracy with real-world EPL is that the second half of the season will mirror the first half exactly.

See if this adaption of http://board.phpbuilder.com/showthread.php?10300945-Round-Robin-Generator is any better :) - just for first half of the season.

$clubs = array(
    'che', 'swa', 'ast', 'manc', 'liv', 'tot', 'ars', 'sot', 'hul', 'stok', 'wham', 'qpr', 'sun', 'mutd', 'lei', 'new', 'eve', 'wba', 'cry', 'bur',
);
shuffle($clubs);

$num_players = count($clubs) - 1;

// Set the return value
$ret = '';

// Generate the pairings for each round.
for ($round = 0; $round < $num_players; $round++) {
    $ret .= '<h3>' . ($round + 1) . '</h3>';
    $players_done = array();

    // Pair each player except the last.
    for ($player = 1; $player < $num_players; $player++) {
        if (!in_array($player, $players_done)) {
            // Select opponent.
            $opponent = $round - $player;
            $opponent += ($opponent < 0) ? $num_players : 1;

            $playerName = $clubs[$player];
            $opponentName = $clubs[$opponent];

            // Ensure opponent is not the current player.
            if ($opponent != $player) {
                // Choose colours.
                if (($player + $opponent) % 2 == 0 xor $player < $opponent) {
                    // Player plays white.
                    $ret .= "$playerName - $opponentName $br";
                } else {
                    // Player plays black.
                    $ret .= "$opponentName - $playerName $br";
                }

                // This pair of players are done for this round.
                $players_done[] = $player;
                $players_done[] = $opponent;
            }
        }
    }

    // Pair the last player.
    if ($round % 2 == 0) {
        $playerName = $clubs[$num_players];
        $opponent = ($round + $num_players) / 2;
        $opponentName = $clubs[$opponent];
        // Last player plays white.
        $ret .= "$playerName - $opponentName $br";
    } else {
        $opponent = ($round + 1) / 2;
        // Last player plays black.
        $ret .= "$opponentName - $playerName $br";
    }
}

echo $ret;
LS11
  • 261
  • 1
  • 6
  • hul plays with mutd in 3, 6, 16 days =) – Bohdan Vorona Sep 09 '14 at 14:55
  • The code as is shows the following message: "Notice Undefined variable: br " For those that want to try the code, just declare the variable with: $br = "
    ";
    In the script even matchdays only have 9 games instead of 10, and game 9 has a team play against itself. That means 3 teams don't play that day.
    – Leveller Jul 28 '23 at 23:46