0

首先,对不起标题,我不知道如何用几句话更好地解释这一点。

这里已经回答了一个类似的问题:PHP - Rearranging a string into order

但这(有点)对我不起作用。

我有一堆扑克玩家,我已经知道很多关于他们的信息,比如谁在按钮位置。我想根据按钮位置的谁为所有玩家分配不同的值。

这是我到目前为止所做的,但它没有按预期工作:

function playerPosition($numplayers, $BTNPlayer, $players) {

    switch ($numplayers) {
        case '3' :
            $gameStates = array("BTN", "SB", "BB");
            break;
        case '4' :
            $gameStates = array("BTN", "SB", "BB", "CO");
            break;
        case '5' :
            $gameStates = array("BTN", "SB", "BB", "MP", "CO");
            break;
        case '6' :
            $gameStates = array("BTN", "SB", "BB", "UTG", "MP", "CO");
            break;
        case '7' :
            $gameStates = array("BTN", "SB", "BB", "UTG", "UTG", "MP", "CO");
            break;
        case '8' :
            $gameStates = array("BTN", "SB", "BB", "UTG", "UTG", "MP", "MP", "CO");
            break;
        case '9' :
            $gameStates = array("BTN", "SB", "BB", "UTG", "UTG", "UTG", "MP", "MP", "CO");
            break;
        case '10' :
            $gameStates = array("BTN", "SB", "BB", "UTG", "UTG", "UTG", "MP", "MP", "MP", "CO");
            break;
    }
    $looped = false;
    $found = false;
    $foundAt = 0;
    $c = 0;

    for ($x = 0; $x < count($players); $x++) {

        if($players[$x]['name'] == $BTNPlayer && !$found) {
            $players[$x]['position'] = $gameStates[$c];
            $found = true;
            $foundAt = $x;

        } else {
            if($found) {
                if ($x != $foundAt)
                    $players[$x]['position'] = $gameStates[$c++];
            }

            if ($found && !$looped) {
                $x = -1;
                $looped = true;
            }
        }
    }

    return $players;
}

$players = array(array('seat' => '1','name' => 'Player1', 'stack' => '2.68', 'status' => '1'), 
                 array('seat' => '2','name' => 'Player2', 'stack' => '7.23', 'status' => '1'), 
                 array('seat' => '3','name' => 'Player3', 'stack' => '0.00', 'status' => '0'), 
                 array('seat' => '4','name' => 'Player4', 'stack' => '1.03', 'status' => '1'), 
                 array('seat' => '5','name' => 'Player5', 'stack' => '0.63', 'status' => '1'), 
                 array('seat' => '6','name' => 'Player6', 'stack' => '1.23', 'status' => '1'), 
                 array('seat' => '7','name' => 'Player7', 'stack' => '2.24', 'status' => '1'), 
                 array('seat' => '8','name' => 'Player8', 'stack' => '3.47', 'status' => '1'), 
                 array('seat' => '9','name' => 'Player9', 'stack' => '5.04', 'status' => '1')
                );

$BTNPlayer = 'Player4';
$positions = playerPosition(9, $BTNPlayer, $players);
echo '<pre>';
print_r($positions);

这导致:

Array
(
    [0] => Array
        (
            [seat] => 1
            [name] => Player1
            [stack] => 2.68
            [status] => 1
            [position] => SB
        )

    [1] => Array
        (
            [seat] => 2
            [name] => Player2
            [stack] => 7.23
            [status] => 1
            [position] => BB
        )

    [2] => Array
        (
            [seat] => 3
            [name] => Player3
            [stack] => 0.00
            [status] => 0
            [position] => UTG
        )

    [3] => Array
        (
            [seat] => 4
            [name] => Player4
            [stack] => 1.03
            [status] => 1
            [position] => BTN
        )

    [4] => Array
        (
            [seat] => 5
            [name] => Player5
            [stack] => 0.63
            [status] => 1
            [position] => UTG
        )

    [5] => Array
        (
            [seat] => 6
            [name] => Player6
            [stack] => 1.23
            [status] => 1
            [position] => UTG
        )

    [6] => Array
        (
            [seat] => 7
            [name] => Player7
            [stack] => 2.24
            [status] => 1
            [position] => MP
        )

    [7] => Array
        (
            [seat] => 8
            [name] => Player8
            [stack] => 3.47
            [status] => 1
            [position] => MP
        )

    [8] => Array
        (
            [seat] => 9
            [name] => Player9
            [stack] => 5.04
            [status] => 1
            [position] => CO
        )
)

如您所见,排序功能没有按预期工作。

4

1 回答 1

1

这应该会产生所需的结果:

function playerPosition($BTNPlayer, $players) {
    $gameStates = array("BTN", "SB", "BB", "UTG", "UTG", "UTG", "MP", "MP", "MP", "CO");
    if(count($players) > count($gameStates))
        throw new Exception('Max player number' .count($gameStates));

    //first find the player
    $btnPlayerIndex = getPlayerIndexByName($BTNPlayer, $players);

    //now we change the array so we start with the btn player
    $sortedPlayers = array_merge(
        array_slice($players, $btnPlayerIndex),
        array_slice($players, 0, $btnPlayerIndex)
    );

    //now we assign your game states
    foreach($sortedPlayers AS $index => &$player){
        $player['position'] = $gameStates[$index];
    }
    return $sortedPlayers;
    }

function getPlayerIndexByName($BTNPlayer, $players){
    foreach($players AS $index => $player){
        if($player['name'] == $BTNPlayer) return $index;
    }
    return false;
}

$players = array(array('seat' => '1','name' => 'Player1', 'stack' => '2.68', 'status' => '1'),
    array('seat' => '2','name' => 'Player2', 'stack' => '7.23', 'status' => '1'),
    array('seat' => '3','name' => 'Player3', 'stack' => '0.00', 'status' => '0'),
    array('seat' => '4','name' => 'Player4', 'stack' => '1.03', 'status' => '1'),
    array('seat' => '5','name' => 'Player5', 'stack' => '0.63', 'status' => '1'),
    array('seat' => '6','name' => 'Player6', 'stack' => '1.23', 'status' => '1'),
    array('seat' => '7','name' => 'Player7', 'stack' => '2.24', 'status' => '1'),
    array('seat' => '8','name' => 'Player8', 'stack' => '3.47', 'status' => '1'),
    array('seat' => '9','name' => 'Player9', 'stack' => '5.04', 'status' => '1')
);

$BTNPlayer = 'Player5';
$positions = playerPosition($BTNPlayer, $players);
echo '<pre>';
print_r($positions);
于 2013-09-28T14:55:40.787 回答