Skip to content

Instantly share code, notes, and snippets.

@kiasaki
Created June 17, 2017 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiasaki/08013f3a80bc1c2aa5ed0a78f9fde0ca to your computer and use it in GitHub Desktop.
Save kiasaki/08013f3a80bc1c2aa5ed0a78f9fde0ca to your computer and use it in GitHub Desktop.
Java Elo
import java.lang.Math;
public class Elo {
public int k;
public Elo(int k) {
this.k = k;
}
public double expectedScoreForDifference(int difference) {
return 1 / (1 + Math.pow(10, (difference / 400)));
}
public double expectedScore(int playerElo, int opponentElo) {
return this.expectedScoreForDifference(opponentElo - playerElo);
}
public int update(int playerElo, int opponentElo, boolean playerWon) {
double actualScore = 0.0;
if (playerWon) {
actualScore = 1.0;
}
double expectedScore = this.expectedScore(playerElo, opponentElo);
return (int)Math.round(playerElo + this.k * (actualScore-expectedScore));
}
public static void main(String[] args) {
Elo e = new Elo(32);
System.out.println("Player[1200] vs. Opponent[1200], player wins: " +
e.update(1200, 1200, true));
System.out.println("Player[1200] vs. Opponent[1600], player wins: " +
e.update(1200, 1600, true));
System.out.println("Player[1600] vs. Opponent[1200], oponent wins: " +
e.update(1600, 1200, false));
System.out.println("Player[1200] vs. Opponent[2000], player wins: " +
e.update(1200, 2000, true));
}
}
Downloads:$ javac Elo.java && java Elo
Player[1200] vs. Opponent[1200], player wins: 1216
Player[1200] vs. Opponent[1600], player wins: 1229
Player[1600] vs. Opponent[1200], oponent wins: 1571
Player[1200] vs. Opponent[2000], player wins: 1232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment