我目前正在制作一款足球在线经理游戏,我一直试图制作比赛。
这是我的SQL结构:
id: integer
home: integer (home team)
away: integer (away team)
date: integer
league: integer
我的联赛将包含许多球队,因此球队将无法与所有其他球队竞争。
到目前为止,我的解决方案是循环所有可能的匹配,然后给它一个唯一的日期(基于一定数量的给定日期)。但是,由于我在联赛中有太多球队,所以现在不行。
逻辑:
联赛:
我没有任何代码可以显示,因为我的代码变得无关紧要(因为我正在寻找一个不同的解决方案)。我不是在寻找完整的代码,只是指导我。
*更新*
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场主客场比赛。
答案 0 :(得分: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中更好地表达。