├── README.md ├── src ├── Game.java ├── RatingSystem.java └── Player.java └── LICENSE /README.md: -------------------------------------------------------------------------------- 1 | # Elo :game_die: V1.0 2 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://badges.mit-license.org) 3 | ![alt text](https://img.shields.io/badge/calculate%20rating%20in%20competition-good-orange.svg " Calculate your rating ") 4 | 5 | The Elo rating system in Java currently supports 2 player matches. 6 | -------------------------------------------------------------------------------- /src/Game.java: -------------------------------------------------------------------------------- 1 | class Game 2 | { 3 | private String gameName; 4 | private ArrayList players; 5 | private int countPlayers; 6 | private int playerWon; 7 | 8 | Game() 9 | { 10 | countPlayers = 2; 11 | gameName = ""; 12 | players = new ArrayList(countPlayers); 13 | } 14 | 15 | public void setGameName(String Name) 16 | { 17 | gameName = Name; 18 | } 19 | 20 | public void setPlayers(int players) 21 | { 22 | countPlayers = players; 23 | } 24 | 25 | public void addPlayer(Player player) 26 | { 27 | players.add(player); 28 | } 29 | 30 | public void setWinner(int winner) 31 | { 32 | playerWon = winner; 33 | } 34 | 35 | 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Aditya Chatterjee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/RatingSystem.java: -------------------------------------------------------------------------------- 1 | class RatingSystem 2 | { 3 | // For 2 player game 4 | public static void setRate(int won, Player p1, Player p2) // Following Elo rating system 5 | { 6 | double r1 = p1.getCurrentRating(); 7 | double r2 = p2.getCurrentRating(); 8 | double R1 = Math.pow(10, r1/400.0); 9 | double R2 = Math.pow(10, r2/400.0); 10 | double E1 = R1 / (R1 + R2); 11 | double E2 = R2 / (R1 + R2); 12 | int S1 = won, S2 = won == 1?0:1;int k =40; 13 | 14 | double new_r1 = r1 + k * (S1 - E1); 15 | double new_r2 = r2 + k * (S2 - E2); 16 | 17 | p1.setCurrentRating(new_r1); 18 | p2.setCurrentRating(new_r2); 19 | 20 | } 21 | 22 | public static void main(String args[]) 23 | { 24 | Game game = new Game(); 25 | Player p1 = new Player(); 26 | p1.setUsername("a"); 27 | p1.addGameRating(game, 3199.0); 28 | Player p2 = new Player(); 29 | p2.setUsername("a"); 30 | p2.addGameRating(game, 3200.0); 31 | game.addPlayer(p1); 32 | game.addPlayer(p2); 33 | setRate(0,p1,p2); 34 | System.out.println(p1.getCurrentRating()); 35 | System.out.println(p2.getCurrentRating()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Player.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | /* 3 | * Current status: 4 | * Player can play multiple games 5 | * Player will have any initial rating before playing the current set of games 6 | * Player will have a current rating 7 | * For every game, outcome of the game for player will either be win, loss or draw. 8 | * 9 | */ 10 | class Player 11 | { 12 | private String username; 13 | private ArrayList gamesPlayed; 14 | private double initialRating; 15 | private double currentRating; 16 | private ArrayList newRating; 17 | private ArrayList gameStatus; 18 | 19 | Player() 20 | { 21 | gamesPlayed = new ArrayList(); 22 | newRating = new ArrayList(); 23 | gameStatus = new ArrayList(); 24 | currentRating = 1500.0; 25 | initialRating = 1500.0; 26 | username = ""; 27 | } 28 | 29 | public String getUsername() 30 | { 31 | return username; 32 | } 33 | 34 | public ArrayList getGamesPlayed() 35 | { 36 | return gamesPlayed; 37 | } 38 | 39 | public double getCurrentRating() 40 | { 41 | return currentRating; 42 | } 43 | 44 | public double getInitialRating() 45 | { 46 | return initialRating; 47 | } 48 | 49 | public double getNewRating(int i) 50 | { 51 | return newRating.get(i); 52 | } 53 | 54 | public int getGameStatus(int i) 55 | { 56 | return gameStatus.get(i); 57 | } 58 | 59 | public void getUserData(int userId) 60 | { 61 | System.out.println("Enter user data for user "+userId); 62 | Scanner sc = new Scanner(System.in); 63 | this.username = "AdiChat"; 64 | //this.currentRating = sc.nextDouble(); 65 | } 66 | 67 | public void setUsername( String username) 68 | { 69 | this.username = username; 70 | } 71 | 72 | private void addGame(Game game) 73 | { 74 | gamesPlayed.add(game); 75 | } 76 | 77 | private void addRating(double rating) 78 | { 79 | currentRating = rating; 80 | } 81 | 82 | public void addGameRating(Game game, double rating) 83 | { 84 | addGame(game); 85 | addRating(rating); 86 | } 87 | 88 | public void setCurrentRating() 89 | { 90 | // Result of other values - no user input 91 | } 92 | 93 | } 94 | --------------------------------------------------------------------------------