Skip to content

Instantly share code, notes, and snippets.

@AntoineAugusti
Created June 1, 2012 19:47
Show Gist options
  • Save AntoineAugusti/2854724 to your computer and use it in GitHub Desktop.
Save AntoineAugusti/2854724 to your computer and use it in GitHub Desktop.
Classement Elo
<?php
/*
// Classement Elo
// Antoine AUGUSTI - antoine@augusti.fr
*/
//************** CONFIGURATION ****************//
//**********************************************/
// cote initiale de P1
define('elo_1', 2300);
// cote initiale de P2
define('elo_2', 1700);
/*
// Score du match P1 VS P2
// 1 : P1 gagne
// 0 : P1 perd
// 0.5 : match nul
*/
define('SCORE',0);
//**********************************************/
//************** FONCTIONS ****************//
// Calcul de la probabilité de gagner du joueur P1
function estimation($elo_1, $elo_2)
{
$exp = ($elo_2 - $elo_1) / 400;
return 1/ (1 + pow(10,$exp));
}
// Calcul de la nouvelle cote de P1
function calcul_elo_p1($elo_1, $elo_2, $score)
{
$k = valeur_k($elo_1);
$estimation = estimation($elo_1, $elo_2);
$nouveau_rang = $elo_1 + $k * ($score - $estimation);
// On ne veut personne en dessous d'une cote elo de 300
if ($nouveau_rang < 300)
{
$nouveau_rang = 300;
}
return array($nouveau_rang, $estimation);
}
// Calcule la valeur de K en fonction de la cote du joueur
function valeur_k($elo)
{
if ($elo < 1000)
{
$k = 80;
}
if ($elo >= 1000 AND $elo < 2000)
{
$k = 50;
}
if ($elo >= 2000 AND $elo <= 2400)
{
$k = 30;
}
if ($elo > 2400)
{
$k = 20;
}
return $k;
}
/*
// Calcul des nouvelles cotes de P1 et P2
// score = 1 si P1 gagne
// score = 0 si P1 perd
// score = 0.5 s'il y a match nul
*/
function nouveau_rangs($elo_1, $elo_2, $score)
{
// Score pour P2 VS P1
$score_2 = 1 - $score;
$calcul_p1 = calcul_elo_p1($elo_1, $elo_2, $score);
$estimation_p1 = $calcul_p1[1];
$elo_p1 = round($calcul_p1[0]);
$calcul_p2 = calcul_elo_p1($elo_2, $elo_1, $score_2);
$estimation_p2 = $calcul_p2[1];
$elo_p2 = round($calcul_p2[0]);
return array($elo_p1, $elo_p2, $estimation_p1, $estimation_p2);
}
function afficher_difference($nb)
{
if ($nb >= 0)
{
$nb = '+'.$nb;
}
return $nb;
}
function convertir_pourcentage($nb)
{
return round($nb * 100, 2).' %';
}
function phrase_score($score)
{
if ($score == 1)
{
return 'P1 gagne';
}
if ($score == 0)
{
return 'P2 gagne';
}
if ($score == 0.5)
{
return 'Match nul';
}
}
// Traitement des résultats
$retour = nouveau_rangs(elo_1, elo_2, SCORE);
$elo_p1 = $retour[0];
$elo_p2 = $retour[1];
$estimation_p1 = $retour[2];
$estimation_p2 = $retour[3];
$difference_p1 = afficher_difference($elo_p1 - elo_1);
$difference_p2 = afficher_difference($elo_p2 - elo_2);
// Affichage + statistiques
echo '
<h3>cote initiale</h3>
P1 : '.elo_1.'<br>
P2 : '.elo_2.'<br>
<br />
<hr />
<br />
<h3>Après le match - '.phrase_score(SCORE).'</h3>
P1 : '.$elo_p1.' (variation : '.$difference_p1.'). Probabilité de gagner : '.convertir_pourcentage($estimation_p1).'.<br>
P2 : '.$elo_p2.' (variation : '.$difference_p2.'). Probabilité de gagner : '.convertir_pourcentage($estimation_p2).'.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment