Elo Rating Algorithm in C++


Elo Rating Algorithm is a rating algorithm used to rank players in competitive games. The ranking of player of the competition is based on ranting which changes based on the performance of the player as follows,

For a game between two players of different ratings. Let’s say there are two players competing against each other−

Player1                          Player2

Rating of player1 is greater than player2.

If player1 wins the game some player will be transferred from player1 to player2 and vice versa if player2 wins.

But the amount of rating to be transferred for victory is not constant. Instead it is dependent on the person winning the game,

If player1 wins the game, points transferred is less.
If player2 wins the game, points transferred is more. 

The amount of points transferred is dependent on the formula,

For player1,

New rating = old Rating + ratingConstant * (successProb - P1)

For player2,

New rating = old Rating + ratingConstant * (successProb - P2)

Here,

ratingConstant is a constant, it is decided by the gaming community.

P1, probability of player1 winning.
P2, probability of player2 winning.

Rating1 is the rating of player1.
Rating2 is the rating of player2.

If a player wins the successProb is 1 otherwise its 0.

Let’s take an example to understand the working of ELO rating algorithm,

Input: rating1 = 782, rating2 = 1432,
ratingConstant = 100, player1 wins the game.

Output: rating1 = 780, rating2 = 1434

Explanation −

Player1 wins,

For player1,

successProb = 1

New rating = 782 + 100*(1 - 0.98) = 782 + 100*(0.02) = 782 + 2 = 784

For player2,

successProb = 0

New rating = 1432 + 100*(0 - 0.02) = 1432 - 2 = 1430

Program to illustrate the working of ELO rating Algorithm,

Example

Live Demo

#include <bits/stdc++.h>
using namespace std;

void updateRatingUsingELoRating(float rating1, float rating2, int ratingConstant, bool player1SuccessProb) {

   float P1, P2;
   if(rating1 > rating2){
      P1 = (1.0 / (1.0 + pow(10.0, ((rating1 - rating2) / 400.0)) ) );
      P2 = 1 - P1;
   }
   else {
      P2 = (1.0 / (1.0 + pow(10.0, ((rating2 - rating1) / 400.0)) ) );
      P1 = 1 - P2;
   }

   if (player1SuccessProb == 1) {
      rating1 = rating1 + ratingConstant * (1 - P1);
      rating2 = rating2 + ratingConstant * (0 - P2);
   }
   else {
      rating1 = rating1 + ratingConstant * (0 - P1);
      rating1 = rating1 + ratingConstant * (1 - P2);
   }

   cout<<"Ratings After the game\n";
   cout<<"Player 1 : "<<rating1<<"\t Player 2 : "<<rating2;
}

int main()
{
   float rating1 = 782, rating2 = 1432;
   int ratingConstant = 100;
   bool player1SuccessProb = 1;
   cout<<"Ratings before the game: \n";
   cout<<"Player 1 : "<<rating1<<"\t Player 2 : "<<rating2<<endl;
   if(player1SuccessProb)
      cout<<"Player 1 wins the game!\n";
   else
      cout<<"Player 2 wins the game!\n";
   updateRatingUsingELoRating(rating1, rating2, ratingConstant, player1SuccessProb);

   return 0;
}

Output −

Ratings before the game:
Player 1 : 782       Player 2 : 1432
Player 1 wins the game!
Ratings After the game
Player 1 : 784.316 Player 2 : 1429.68

Updated on: 22-Jan-2021

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements