Jump to content

Assigning winnings in a tournament


reitking

Recommended Posts

I'm completely stuck on assigning "winnings" to players in a tournament.

 

I have two arrays $score, $payout.  $score includes the total score of all users ranked from highest to lowest and $payout includes the players' prize assuming all $score are different.   

 

The problem I am having is ties.  If two players have the same score (e.g. $score[m]==$score[m-1]), both players should receive the same payout equal to ($payout[m]+$payout[m-1])/2.  That's easy to handle.  But sometimes 3 players could have the same score, or 4,5,....,n. In all cases of ties with n players, all tied players should receive the sum of all the payouts for those n players/n.

 

Can anyone think of a good algorithm to handle this?  Thank you for your help.

Link to comment
https://forums.phpfreaks.com/topic/175454-assigning-winnings-in-a-tournament/
Share on other sites

assuming that the array is already pre-sorted, and it will ALWAYS be presorted,

 

/*will return an array of the keys that had a ties.
this function assumes that the first key $array[0] is the high score
also assumes its a numeric array
Does include the first array($array[0])'s key. will always
*/
function getTies($array){
$highest = $array[0];
$keys = array();
foreach($array as $key = > $arr){
if ($arr == $highest){
$keys[] = $key;
}
}
return $keys;
}

 

Haven' tested it though, so it may take some tweaking

 

try

<?php
$users_score = array(
'user1' => 123,
'user2' => 123,
'user3' => 123,
'user4' => 124,
'user5' => 124,
'user6' => 125,
'user7' => 125,
'user8' => 125,
'user9' => 125,
'user10' => 125,
'user11' => 125,
'user12' => 126,
'user13' => 127,
'user14' => 128,
'user15' => 129
);
$pey_out = range(10, 150, 10);
rsort($pey_out);
//print_r($pey_out);
$x = array_count_values($users_score);
krsort($x, 0);
$new_pey= array();
$y = $pey_out;
foreach ($x as $k => $v){
$s = 0;
for ($i = 0; $i < $v; $i++) $s +=array_shift($y);
$new_pey[$k] = $s / $v;
}
foreach ($users_score as $us => $sc) echo $us, ' -> ', $new_pey[$sc], "<br />\n";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.