在php中计算玩家位置的最佳方法

在php中计算玩家位置的最佳方法,php,Php,我为每个玩家分配了如下点数: Highest Scorer Player2 Lowest Score player3 you are 2nd highest out of 5 players player4 // Array to be sorted $array = array('a' => 4, 'b' => 8, 'c' => -1,

我为每个玩家分配了如下点数:

Highest Scorer                 Player2                              Lowest Score
    player3       you are 2nd highest out of 5 players               player4
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

// Return index of the place in which your player is now.
$index = key_offset($array, "PlayerName");
player1=20,player2=30,player3=50,player4=10,player5=25,依此类推

这些分数随着分数的增加而变化。我想做的是,如果玩家2查看他的页面,他应该看到所有球员中哪个得分最高,他在所有球员中的位置,以及谁得分最低?在php中实现这一点的最佳方法是什么?因此,它将显示如下内容:

Highest Scorer                 Player2                              Lowest Score
    player3       you are 2nd highest out of 5 players               player4
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

// Return index of the place in which your player is now.
$index = key_offset($array, "PlayerName");

使用php uasort函数,然后您就可以拥有播放器所在的位置。摘自php.net:

function cmp($a, $b) {
if ($a == $b) {
    return 0;
}
    return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');
然后您可以使用它所在的位置:

function key_offset(&$array, $searchKey){
    if(is_array($array) && array_key_exists($searchKey, $array)){
        $counter = 0;
        foreach($array as $key => $value){
            if($searchKey == $key){
                return $counter;
            } else {
                $counter++;
            }
        }
    }
} 
所以现在看起来是这样的:

Highest Scorer                 Player2                              Lowest Score
    player3       you are 2nd highest out of 5 players               player4
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

// Return index of the place in which your player is now.
$index = key_offset($array, "PlayerName");

使用php uasort函数,然后您就可以拥有播放器所在的位置。摘自php.net:

function cmp($a, $b) {
if ($a == $b) {
    return 0;
}
    return ($a < $b) ? -1 : 1;
}

// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');
然后您可以使用它所在的位置:

function key_offset(&$array, $searchKey){
    if(is_array($array) && array_key_exists($searchKey, $array)){
        $counter = 0;
        foreach($array as $key => $value){
            if($searchKey == $key){
                return $counter;
            } else {
                $counter++;
            }
        }
    }
} 
所以现在看起来是这样的:

Highest Scorer                 Player2                              Lowest Score
    player3       you are 2nd highest out of 5 players               player4
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);

// Sort and print the resulting array
uasort($array, 'cmp');

// Return index of the place in which your player is now.
$index = key_offset($array, "PlayerName");
/$users是一个数组,格式为:playerID=>score
$minScore=0$minScoreUser;
$maxScore=-1$maxScoreUser;
$thiscore$这名球员;
foreach($player=>$score的用户)
{
如果($player==$queryPlayer)
{
$thiscore=$score;
$thisPlayer=$player;
}
如果($maxScore<0 | |$score>$maxScore)
{
$maxScore=$score;
$maxScorePlayer=$player;
}
如果($score<$minScore)
{
$minScore=$score;
$minScorePlayer=$player;
}
}
$higherCount=0;
foreach($player=>$score的用户)
如果($score>$thiscore)$higherCount++;
$minScore和$minScorePlayer是最低的玩家

$maxScore和$maxScorePlayer是最高的玩家

$thisScore和$thisPlayer是您感兴趣的玩家

$higherCount是得分高于您的玩家的玩家数

上述问题的复杂度为O(n)

排序解决方案的复杂性为O(n*logn)

/$users是一个数组,其形式为:playerID=>score
$minScore=0$minScoreUser;
$maxScore=-1$maxScoreUser;
$thiscore$这名球员;
foreach($player=>$score的用户)
{
如果($player==$queryPlayer)
{
$thiscore=$score;
$thisPlayer=$player;
}
如果($maxScore<0 | |$score>$maxScore)
{
$maxScore=$score;
$maxScorePlayer=$player;
}
如果($score<$minScore)
{
$minScore=$score;
$minScorePlayer=$player;
}
}
$higherCount=0;
foreach($player=>$score的用户)
如果($score>$thiscore)$higherCount++;
$minScore和$minScorePlayer是最低的玩家

$maxScore和$maxScorePlayer是最高的玩家

$thisScore和$thisPlayer是您感兴趣的玩家

$higherCount是得分高于您的玩家的玩家数

上述问题的复杂度为O(n)


排序解决方案的复杂性为O(n*logn)

我不确定现有答案是否足够

如果你只是想立刻找到每个人的分数和排名,那很简单:就这么做

SELECT * FROM TableOfScores ORDER BY score
这将按顺序给出分数,您可以在每次迭代时增加一个计数器以获得它们的排名。然而,我认为你想要的是简单地显示一个人的屏幕,从你的“你是第二高”信息判断。在这种情况下,它甚至更简单,因为你当然不需要为了得到一个人的排名而对整个列表进行排序

请注意,您应该根据分数建立索引。当您插入/删除玩家分数时,这会占用一点时间,但它会使This查询运行得更快

要找到所做测试的总数,只需执行以下操作

SELECT COUNT(*) AS TotalScores FROM TableOfScores
要查找当前用户的位置,请执行以下操作

SELECT COUNT(*) AS CurrentPersonsRank FROM TableOfScores WHERE score <= $currentPersonScore

选择COUNT(*)作为CurrentPersonsRank,从得分的得分表中选择,我不确定现有答案是否足够

如果你只是想立刻找到每个人的分数和排名,那很简单:就这么做

SELECT * FROM TableOfScores ORDER BY score
这将按顺序给出分数,您可以在每次迭代时增加一个计数器以获得它们的排名。然而,我认为你想要的是简单地显示一个人的屏幕,从你的“你是第二高”信息判断。在这种情况下,它甚至更简单,因为你当然不需要为了得到一个人的排名而对整个列表进行排序

请注意,您应该根据分数建立索引。当您插入/删除玩家分数时,这会占用一点时间,但它会使This查询运行得更快

要找到所做测试的总数,只需执行以下操作

SELECT COUNT(*) AS TotalScores FROM TableOfScores
要查找当前用户的位置,请执行以下操作

SELECT COUNT(*) AS CurrentPersonsRank FROM TableOfScores WHERE score <= $currentPersonScore

选择COUNT(*)作为CurrentPersonsRank,从得分表中选择,这样您就不需要找到中位数。不完全是中位数。。。player2代表什么。如果是,使用按分数排序,如果不是,使用PHP函数?永远不需要排序。只需对用户进行两次迭代。复杂度也更小。@George-你说的两次迭代用户是什么意思?你能用代码显示吗?所以你不需要找到中间值。不完全是中间值。。。player2代表什么。如果是,使用按分数排序,如果不是,使用PHP函数?永远不需要排序。只需对用户进行两次迭代。复杂度也更小。@George-你说的两次迭代用户是什么意思?你能在代码中显示吗?我同意排序算法更复杂,但它比这样看起来更直截了当。当日期集非常大时,复杂性会很明显。我并不是说排序解决方案有什么不好的地方。我也只是提到复杂性。事实上,我自己可能会选择排序解决方案:)我同意排序算法更复杂,但它比这样看更直接。当日期集非常大时,复杂性会很明显。我并不是说排序解决方案有什么不好的地方。我也只是提到复杂性。事实上,我自己可能会选择排序解决方案:)