PHP Chess Rank Algorithm from Facebook Movie
Last Updated on Wednesday, 28 December 2011 08:37pm Written by spunky Wednesday, 28 December 2011 08:29pm
Following on from the previous post, here is the algorithim used for ranking chess players as seen on The Social Network when Mark Zuckerberg is developing Facemash.
The algorithm shown in the movie looked a bit like this:
- Ea = 1 / (1 + 10 ^ ((Rb-Ra)/400))
- Eb = 1 / (1 + 10 ^ ((Ra-Rb)/400))
Here is something a bit more practical in the form of a PHP script:
/**
* Calculates odds of winning or new score.
* New players start on 2000 points.
*
* @param int $player1 Player 1 Score
* @param int $player2 Player 2 Score
* @param int $result Determine the result
* 0 = Calculate Odds (%)
* 1 = Player 1 Won
* 2 = Player 2 Won
* 3 = It's a Draw
*/
function calculateRank($player1, $player2, $result=0)
{
$odds_p1 = 1/(1+pow(10,(($player2-$player1)/400)));
$odds_p2 = 1/(1+pow(10,(($player1-$player2)/400)));
if ($result==0)
return array('player1' => $odds_p1*100, 'player2' => $odds_p2*100);
$result_p1 = ($result==1) ? 1 : (($result==2) ? 0 : 0.5);
$result_p2 = ($result==1) ? 0 : (($result==2) ? 1 : 0.5);
$score_p1 = $player1 + (32*($result_p1-$odds_p1));
$score_p2 = $player2 + (32*($result_p2-$odds_p2));
return array('player1' => $score_p1, 'player2' => $score_p2);
}
Enjoy!
Tags: chess algorithim, chess algorithm, chess player, chess rank formula, chess ranking, facebook movie, facesmash algorithm, php chess rank, the social network