Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.rabrg.kitpvp.rating;
- /**
- * A class which implements the Elo rating system.
- * @author Ryan Greene
- *
- */
- public final class RatingSystem {
- /**
- * The rating system instance.
- */
- private static final RatingSystem INSTANCE = new RatingSystem();
- /**
- * Gets the rating system instance.
- * @return The rating system instance.
- */
- public static RatingSystem getInstance() {
- return INSTANCE;
- }
- /**
- * A private constructor to prevent initialization.
- */
- private RatingSystem() {
- }
- /**
- * Gets the new rating of a player.
- * @param rating The current rating of the player.
- * @param opponentRating The current rating of the player's opponent.
- * @param result The result of the match.
- * @param kFactor The K-factor of the player.
- * @return The new rating of the player.
- */
- public int getNewRating(final int rating, final int opponentRating, final Result result, final int kFactor) {
- double expectedScore = getExpectedScore(rating, opponentRating);
- int newRating = calculateNewRating(rating, result.getScore(), expectedScore, kFactor);
- return newRating;
- }
- /**
- * Gets the expected score of a match.
- * @param rating The rating of the player.
- * @param opponentRating The rating of the player's opponent.
- * @return The expected score of a match.
- */
- private double getExpectedScore(final int rating, final int opponentRating) {
- return 1.0 / (1.0 + Math.pow(10.0, ((double) (opponentRating - rating) / 400.0)));
- }
- /**
- * Calculates the new rating of a player.
- * @param oldRating The old rating of the player.
- * @param score The score of the match.
- * @param expectedScore The expected score of the match.
- * @param kFactor The K-factor of the player.
- * @return The new rating of the player.
- */
- private int calculateNewRating(final int oldRating, double score, double expectedScore, double kFactor) {
- return oldRating + (int) (kFactor * (score - expectedScore));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement