PHP - 循环赛和第 3 人(2 名球员和一名作家/难民)

PHP - Round Robin and 3rd person (2 players and one writer / refugee)

我有以下代码

poule = ['Jason', 'Raymond', 'Rupert', 'Mike', 'Simon', 'Jeremy'];

$games = [];
$cnt_players = count($poule);
$players = $poule;

if ($cnt_players % 2 != 0)
{
    array_push($players, ['name' => 'bye', 'uid' => FALSE, 'alias' => NumToChar($cnt_players + 1), TRUE]);
    $cnt_players++;
}

$away = array_splice($players, $cnt_players / 2);
$home = $players;

$write = [];

for ($i = 0; $i < count($home) + count($away) - 1; $i++)
{
    for ($j = 0; $j < count($home); $j++)
    {
        //Get the writer
        $writer = $this->GetWriter($home, $away, $j, $i);

        //Remove the dummy games (where one player is bye)
        if ($home[$j]['name'] !== 'bye' && $away[$j]['name'] !== 'bye')
        {
            $games[] = [['uid' => $home[$j]['uid'], 'name' => $home[$j]['name'], 'alias' => $home[$j]['alias']], ['uid' => $away[$j]['uid'], 'name' => $away[$j]['name'], 'alias' => $away[$j]['alias']], $writer];
        }   

        //echo 'I:' . $i . ' - J: ' . $j . ' - ' . $home[$j]['alias'] . ' : ' . $home[$j]['name'] . '  -  ' . $away[$j]['alias'] . ' : ' . $away[$j]['name'] . '  ==>  ' . $writer['alias'] . ' : ' . $writer['name'] . "\n\r";

        $write[$writer['name']][] = $writer['name'];
    }

    if (count($home) + count($away) - 1 > 2)
    {
        array_unshift($away, current(array_splice($home, 1, 1)));
        array_push($home, array_pop($away));
    }
}

//print_r($write);

return $games;

-- 函数 GetWriter 应该为我们提供记录该特定游戏分数的玩家。

private function GetWriter($home, $away, $j, $i)
        {
            if ($j > 0)
            {
                if ($j == 1)
                {
                    $writer = (isset($home[$j + 1]['alias']) ? $home[$j + 1] : $home[$j + 1]);
                }
                else
                {
                    $writer = (isset($home[$j - 1]['alias']) ? $home[$j - 1] : $home[$j + 1]);
                }

                //Check if writer is a bye, this is not possible
                if ($writer['name'] == 'bye')
                {
                    $writer = (isset($away[$j - 2]['alias']) ? $away[$j - 2] : $home[$j - 1]);
                }
            }
            else
            {
                $writer = (isset($home[$j + 1]['alias']) ? $home[$j + 1] : $away[$j]);

                if ($writer['name'] == 'bye')
                {
                    $writer = (isset($away[$j + 1]['alias']) ? $away[$j + 1] : $home[$j]);
                }
            }

            return $writer;
        }

以上代码为我提供了所有游戏,每个玩家对另一个玩家玩一次(循环赛)。但是我需要找到第三个玩家来担任作家/难民。就像在飞镖游戏中一样,1 名玩家是在记分牌上写分数的人。我确实有一个作家,但它并没有很好地分配给每个玩家。 是否有一个公式可以让正确的玩家成为作家/难民?

一些例子

杰森 vs 迈克雷蒙德 雷蒙德 vs 西蒙鲁伯特 鲁珀特 vs 杰里米迈克 等等

Jason 和 Mike 比赛,Raymond 记下了比分。 雷蒙德对阵西蒙,鲁珀特记下比分等等

我的GetWriter returns写了,但是分的不好。 例如:玩家 Jason 从不写,而 Raymond 写的分数最多。

我有以下解决方案,效果很好。 因为玩家 A 和玩家 B 总是开始,所以我们将密钥设置为默认索引 3。 现在我们使用 GetCounter 遍历所有玩家并获取第一个可用的作家/难民。

$games = [];
$cnt_players = count($poule);
$players = $poule;
$key = 3;

if ($cnt_players % 2 != 0)
{
    array_push($players, ['name' => 'bye', 'uid' => FALSE, 'alias' => NumToChar($cnt_players + 1, TRUE), TRUE]);
    $cnt_players++;
}

$away = array_splice($players, $cnt_players / 2);
$home = $players;

for ($i = 0; $i < count($home) + count($away) - 1; $i++)
{
    for ($j = 0; $j < count($home); $j++)
    {
        //Remove the dummy games (where one player is bye)
        if ($home[$j]['name'] != 'bye' && $away[$j]['name'] != 'bye')
        {
            //Get the writer
            $writer = $this->GetCounter($home, $away, $j, $poule, $key);

            $games[] = [['uid' => $home[$j]['uid'], 'name' => $home[$j]['name'], 'alias' => $home[$j]['alias']], ['uid' => $away[$j]['uid'], 'name' => $away[$j]['name'], 'alias' => $away[$j]['alias']], $writer['array']];
            $key = $writer['key'];
        }
        $key++;
    }

    if (count($home) + count($away) - 1 > 2)
    {
        array_unshift($away, current(array_splice($home, 1, 1)));
        array_push($home, array_pop($away));
    }
}


return $games;

和 GetCounter

private function GetCounter($home, $away, $j, $writers, $key)
{
    if (isset($writers[$key]['alias']))
    {
        $writer['array'] = $writers[$key];
        $writer['key'] = $key;

        if ($home[$j]['alias'] == $writer['array']['alias'] || $away[$j]['alias'] == $writer['array']['alias'])
        {
            $key++;
            return $this->GetCounter($home, $away, $j, $writers, $key);
        }
    }
    else {
        $key = 0;
        return $this->GetCounter($home, $away, $j, $writers, $key);
    }

    return $writer;
}