PHP生成足球赛程

时间:2014-09-09 10:41:12

标签: php

我有38天和20个俱乐部(EPL)。

如何在这几天(日程表)为这些俱乐部产生不重复的比赛?

例如:


第1天:

club1 - club2

club3 - club4

...

俱乐部19 - 俱乐部20


第2天:

club1 - club3

club2 - club4

...

club20 - club18


每个俱乐部都会参加其他两场比赛(主场和客场比赛)。分别不和自己玩。


我的想法:

    $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?

        }
    }

3 个答案:

答案 0 :(得分:1)

你只需要一个球杆阵列

1)删除$ clubs2
2)将$ clubs1重命名为$ club
3)去除整体结构

    //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>';
    }

答案 1 :(得分:0)

这是我的解决方案,用数据库中的俱乐部ID结果集替换$ clubs数组。现实世界EPL唯一的微小不准确之处在于赛季后半段将完全反映上半场。

看看http://board.phpbuilder.com/showthread.php?10300945-Round-Robin-Generator的这种适应性是否更好:) - 仅适用于本赛季的前半段。

$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;

答案 2 :(得分:0)

    $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];
        }

    }