Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created September 21, 2016 02:24
Show Gist options
  • Save cviebrock/f5955250821ccc95f6cd853e7fbe4ffc to your computer and use it in GitHub Desktop.
Save cviebrock/f5955250821ccc95f6cd853e7fbe4ffc to your computer and use it in GitHub Desktop.
Quick script to calculate the change in Scrabble rating between yourself and a field of opponents
<?php
if ($argc < 3) {
echo <<< EOB
USAGE: {$argv[0]} myRating opp1rating [opp2rating...]
EOB;
exit(1);
}
array_shift($argv);
$rating = array_shift($argv);
$lessThanFiftyGames = false;
$k = calculateK($rating, $lessThanFiftyGames);
$winKeys = [
'Win' => 1.0,
'Tie' => 0.5,
'Loss' => 0.0,
];
echo <<< EOB
Rating: {$rating} K: {$k}
EOB;
printf('%-10s', 'OppR');
printf('%-10s', 'Win%');
foreach(array_keys($winKeys) as $desc) {
printf('%-10s', $desc);
}
echo "\n";
while($oppRating = array_shift($argv)) {
$expectation = winExpectation($rating, $oppRating);
printf('%5d ', $oppRating);
printf('%0.5f ', $expectation);
foreach($winKeys as $result) {
$excess = $result - $expectation;
$points = convertExcessToPoints($excess, $k);
printf('%- 10d', round($points));
}
echo "\n";
}
function calculateK($rating, $lessThanFiftyGames = false) {
$k = $rating < 1800 ? 20 : ($rating < 2000 ? 16 : 10);
if ($lessThanFiftyGames) {
$k *= 1.5;
}
return $k;
}
function winExpectation($a, $b) {
return 1 - 1/(1+exp(0.0031879*($a-$b)));
}
function convertExcessToPoints($excess, $k) {
return $excess*$k;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment