package ELO_Rating_System;
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import java.util.*;
public class EloRating {
public static final int[] RATINGS = { 0, 800, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2400, 2600 };
public static final int INITIAL = 1400; //initial rating for all users
public static final int PROVISIONAL = 200; //may consider 200 points as a range for the ratings
/** This is the maximum number of points up for grabs in a game. */
public static final int K_MAX = 32; //ratings<2000
public static final int K_MID = 20; //ratings>=2000 && ratings<=2400
public static final int K_MIN = 10; //ratings>2400
public static final double WIN = 1;
public static final double LOSE = 0;
public static final double EQU = 0.5; //no one win or lose
public static String[] comName = {"Jimmy","Tom","Sherrly"};
public static int[] comRating = {2050,1270,1550};
public static double[] comScore = {EQU,WIN,WIN};
public static String[] compName = {"Jemmy","Lin","Fisher","Kobe","Yang","Aliy","Kitty",
"Quatras","Yums","China","Jason"};
public static int[] compRating = {2500,2480,2430,2400,2375,2365,2335,2320,2300,2280,2260};
public static double[] compScore = {LOSE,LOSE,LOSE,LOSE,LOSE,LOSE,WIN,WIN,WIN,WIN,WIN};
public static Competitor[] initialize(String[] str,int[] ratings,double[] scores){
Competitor[] com = new Competitor[str.length];
int i=0;
for(;i<str.length;i++)
com[i]=new Competitor(str[i],ratings[i], scores[i]);
return com;
}
/**
* P(ratings[1]-ratings[0])
* Expected chance player[1] will beat player[0]
*/
public static double expectancy(User one,Competitor[] opponents) {
int diff=0;
for(int i=0;i<opponents.length;i++){
double probability = 1.0/(1.0+Math.pow(10.0,(opponents[i].rating-one.rating)/400.0));
System.out.println("\n"+probability);
diff+=probability;
}
return diff;
}
/**
* Expected number of points won by { player[0], player[1] } if they were
* to win a match between the 2.
*/
public static double expectedRating(int k,User one,Competitor[] opponents) {
double ex = expectancy(one,opponents);
double totalScore=0;
for(int i=0;i<opponents.length;i++)
totalScore+=opponents[i].score;
return one.rating+k*(totalScore-ex);
}
public static void main(String[] args){
User pan = new User("pan",1650);
Competitor[] comp = initialize(comName, comRating, comScore);
System.out.println("Test one result: "+expectedRating(K_MIN,pan,comp));
User cris = new User("cris",2350);
Competitor[] comp2 = initialize(compName,compRating,compScore);
System.out.println("Test two result: "+expectedRating(K_MAX,cris,comp2));
}
}