0

我目前正在制作足球在线经理游戏,但我一直在尝试生成比赛。

这是我的 SQL 结构:

id:     integer
home:   integer (home team)
away:   integer (away team)
date:   integer
league: integer

我的联赛将包含许多球队,因此一个球队将无法与所有其他球队竞争。

到目前为止,我的解决方案是遍历所有可能的匹配,然后给它一个唯一的日期(基于一定数量的给定日期)。但是,现在我在联赛中有太多球队,这行不通。

逻辑:

  • home 和 away 不能包含相同的 id。
  • 如果找不到具有给定日期的比赛和其中一支参赛球队,则比赛具有唯一的日期。
  • 所有球队必须有相同数量的比赛(为了公平起见)
  • 球队必须有相同数量的主场和客场比赛。

联盟:

  • 联盟中的球队数量是统一的。
  • 整个季节是一次生成的。
  • 赛季持续 1 周,该周内有 28 个特定日期,可用作比赛日期。


    我没有任何代码要显示,因为我的代码已经变得无关紧要(因为我正在寻找不同的解决方案)。我不是在寻找完整的代码,只是指导我的方式。

    * 更新 *

    foreach ($teams as $team_home) {
        foreach ($teams as $team_away) {
            if ($team_home === $team_away) continue;
    
            if (!isset($represented[$team_home->id]['home'])) $represented[$team_home->id]['home'] = 0;                 
            if (!isset($represented[$team_away->id]['away'])) $represented[$team_away->id]['away'] = 0;
    
            if ($represented[$team_away->id]['away'] == count($dates)/2) continue;
    
            $matches[] = array(
                'home' => $team_home->id,
                'away' => $team_away->id,
            );
    
            $represented[$team_home->id]['home']++;
            $represented[$team_away->id]['away']++;
    
            if ($represented[$team_home->id]['home'] == count($dates)/2) break;
        }
    }
    

    所代表的数组跟踪球队在主场和客场比赛的次数。请注意,一个球队最多可以有 50% 的日期作为主场比赛。

    示例输出:

    Array
    (
        [1] => Array
            (
                [home] => 14
                [away] => 14
            )
    
        [2] => Array
            (
                [away] => 14
                [home] => 14
            )
    
        ....
    
    
        ....
    
        [25] => Array
            (
                [away] => 9
                [home] => 9
            )
    )
    

    正如你所看到的,最后被循环的球队并没有像其他球队那样打 14 场主客场比赛。

  • 4

    1 回答 1

    1

    试图一口气做太多事情。联赛中有 24 支球队,意味着每支球队参加 46 场比赛(另外 23 支球队,主场一场,客场一场)。

    所以现在你有 46 个插槽可以放置火柴。

    Choose a team at random
    Generate those 46 matches ,assign into the slots in some random order.
    Randomly Choose the next team
    Generate their 46 matches, look for those that have already been assigned and set their slot
    Randomly fill the rest.
    Keep going until you have one team remaining, which should already be done...
    

    然后你可以指定日期...

    可以用 SQL 做到这一点,但几乎可以肯定用 PHP 更好地表达。

    于 2013-11-29T23:11:05.847 回答