Advertisement
Chiddix

RatingSystem

Dec 14th, 2013
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package me.rabrg.kitpvp.rating;
  2.  
  3. /**
  4.  * A class which implements the Elo rating system.
  5.  * @author Ryan Greene
  6.  *
  7.  */
  8. public final class RatingSystem {
  9.  
  10.     /**
  11.      * The rating system instance.
  12.      */
  13.     private static final RatingSystem INSTANCE = new RatingSystem();
  14.  
  15.     /**
  16.      * Gets the rating system instance.
  17.      * @return The rating system instance.
  18.      */
  19.     public static RatingSystem getInstance() {
  20.         return INSTANCE;
  21.     }
  22.  
  23.     /**
  24.      * A private constructor to prevent initialization.
  25.      */
  26.     private RatingSystem() {
  27.        
  28.     }
  29.  
  30.     /**
  31.      * Gets the new rating of a player.
  32.      * @param rating The current rating of the player.
  33.      * @param opponentRating The current rating of the player's opponent.
  34.      * @param result The result of the match.
  35.      * @param kFactor The K-factor of the player.
  36.      * @return The new rating of the player.
  37.      */
  38.     public int getNewRating(final int rating, final int opponentRating, final Result result, final int kFactor) {
  39.         double expectedScore = getExpectedScore(rating, opponentRating);
  40.         int newRating = calculateNewRating(rating, result.getScore(), expectedScore, kFactor);
  41.         return newRating;
  42.     }
  43.  
  44.     /**
  45.      * Gets the expected score of a match.
  46.      * @param rating The rating of the player.
  47.      * @param opponentRating The rating of the player's opponent.
  48.      * @return The expected score of a match.
  49.      */
  50.     private double getExpectedScore(final int rating, final int opponentRating) {
  51.         return 1.0 / (1.0 + Math.pow(10.0, ((double) (opponentRating - rating) / 400.0)));
  52.     }
  53.  
  54.     /**
  55.      * Calculates the new rating of a player.
  56.      * @param oldRating The old rating of the player.
  57.      * @param score The score of the match.
  58.      * @param expectedScore The expected score of the match.
  59.      * @param kFactor The K-factor of the player.
  60.      * @return The new rating of the player.
  61.      */
  62.     private int calculateNewRating(final int oldRating, double score, double expectedScore, double kFactor) {
  63.         return oldRating + (int) (kFactor * (score - expectedScore));
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement