我目前正在制作足球在线经理游戏,但我一直在尝试生成比赛。
这是我的 SQL 结构:
id: integer
home: integer (home team)
away: integer (away team)
date: integer
league: integer
我的联赛将包含许多球队,因此一个球队将无法与所有其他球队竞争。
到目前为止,我的解决方案是遍历所有可能的匹配,然后给它一个唯一的日期(基于一定数量的给定日期)。但是,现在我在联赛中有太多球队,这行不通。
逻辑:
- home 和 away 不能包含相同的 id。
- 如果找不到具有给定日期的比赛和其中一支参赛球队,则比赛具有唯一的日期。
- 所有球队必须有相同数量的比赛(为了公平起见)
- 球队必须有相同数量的主场和客场比赛。
联盟:
我没有任何代码要显示,因为我的代码已经变得无关紧要(因为我正在寻找不同的解决方案)。我不是在寻找完整的代码,只是指导我的方式。
* 更新 *
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 场主客场比赛。