简体   繁体   English

PHP-知更鸟和第三人称(2名玩家和一名作家/难民)

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

I have the following code 我有以下代码

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;

-- The function GetWriter should give us the player who will note the scores for that particulair game. -功能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;
        }

Above code gives me all games with each player playing once to another player (round robin). 上面的代码为我提供了所有游戏,每个玩家只能与另一个玩家玩一次(循环赛)。 However I need to find a third player who will be the writer / refugee. 但是,我需要找到第三位将成为作家/难民的玩家。 Like in Darts, 1 player is the person who writes the scores on the scoreboard. 就像在“飞镖”中一样,一名玩家是在计分板上写下分数的人。 I do get a writer, but it isn't nicely divided per player. 我确实有一名作家,但每个球员的分成都没有。 Is there an formula to get the correct player who is writer / refugee ? 是否有一个公式可以得出正确的作家/难民玩家身份?

Some examples 一些例子

Jason vs Mike Raymond Raymond vs Simon Rupert Rupert vs Jeremy Mike etc 杰森vs麦克雷蒙德雷蒙德vs西蒙鲁珀特鲁珀特vs杰里米·迈克等

So Jason plays against Mike and Raymond notes the scores. 因此,杰森在与迈克和雷蒙德的比赛中得分。 Raymond plays against Simon and Rupert notes the scores etc etc 雷蒙德对阵西蒙和鲁珀特时记下比分等

My GetWriter returns writes, but not well divided. 我的GetWriter返回写入,但没有很好地划分。 Eg: Player Jason writes never and Raymond writes the most scores. 例如:玩家杰森(Jason)从不写,雷蒙德(Raymond)写最高分。

I have the following solution which works well. 我有以下效果很好的解决方案。 Because player A and player B always start, we set the key as default index 3. Now we loop with GetCounter though all players and get the writer / refugee which is first available. 因为玩家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;

And the GetCounter 还有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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM