duan1979768678 2016-12-09 19:05
浏览 8
已采纳

PHP - Round Robin和第3人(2名球员和1名作家/难民)

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'] . "
";

        $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.

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

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. Eg: Player Jason writes never and Raymond writes the most scores.

  • 写回答

1条回答 默认 最新

  • douqiang3768 2016-12-12 11:03
    关注

    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.

    $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

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器