Jump to content

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";
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.