我正在创建一个游戏,它将每场比赛的前 2 个最高“分数”标记为获胜者。
如果两个分数相同,则必须进行决胜局(除非两个匹配的分数是第一和第二名)。
我怎样才能(有效地)创建一个函数来返回这些结果以实现以下可能性:
6 种不同游戏的可能游戏结果:
$a = array(20,19,18,17,16,15); // no tie breaker needed - [1], [2] win
$b = array(20,20,18,17,16,15); // no tie breaker needed - [1], [2] win
$c = array(20,19,19,17,16,15); // tie breaker needed for [2], [3] values
$d = array(20,20,20,17,16,15); // tie breaker needed for [1], [2], [3] values
$e = array(20,19,19,19,16,15); // tie breaker needed for [2], [3], [4] values
$f = array(20,20,20,20,20,20); // tie breaker needed for all values
编辑:解决方案:
<?php
$score = array("green"=>10, "pink"=>10, "orange"=>9, "blue"=>8, "yellow"=>7);
$count = 0;
foreach ($score as $value) {
$count++;
// if the count is 2
if ($count === 2) {
// save the value as a variable
$second = $value;
// if the count is 3
} else if ($count === 3) {
// if 2nd place and 3rd place have the same score - tiebreaker
if ($second === $value) {
// add matches to array for tiebreaker
$result = array_keys($score, $value);
// if 2nd place and 3rd place have different scores - no tiebreaker
} else {
// split first 2 places from the array
$result = array_slice($score, 0, 2);
}
}
}
?>