reitking Posted September 25, 2009 Share Posted September 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/175454-assigning-winnings-in-a-tournament/ Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175454-assigning-winnings-in-a-tournament/#findComment-924599 Share on other sites More sharing options...
sasa Posted September 25, 2009 Share Posted September 25, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/175454-assigning-winnings-in-a-tournament/#findComment-924744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.