Created
June 17, 2017 05:08
-
-
Save kiasaki/08013f3a80bc1c2aa5ed0a78f9fde0ca to your computer and use it in GitHub Desktop.
Java Elo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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