-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLBELO.java
More file actions
177 lines (136 loc) · 3.47 KB
/
Copy pathMLBELO.java
File metadata and controls
177 lines (136 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.lang.Math;
import java.util.*;
import java.io.*;
public class MLBELO
{
//ARGS: [1] file to read
static int K_FACTOR = 200;
public static void main(String[] args)
{
int teamArrayTail = -1; //points to last initialized element of teams
int lineCounter = 0; //for error message purposes
int counter = 0; //counts number of consecutive unempty lines
Scanner scan;
LinkedList<Team> teams = new LinkedList<Team>();
try {
scan = new Scanner(new File(args[0]));
}
catch(FileNotFoundException e) {
System.out.println("File Not Found");
System.exit(1);
//to avoid "scan might not have been initialized" error
//this line should never run
scan = new Scanner(System.in);
}
//main loop
while(scan.hasNextLine()){
String currentLine = scan.nextLine();
double t1Score = 0; //team 1 score
double t2Score = 0; //team 2 score
lineCounter++;
if (currentLine.equals("")){
counter = 0;
continue;
}
//currentline is unempty
counter++;
if (counter==3){
//currentLine is game result
//format: 'AAA 0, BBB 0' or 'postponed'
if (currentLine.equals("Postponed")){
continue;
}
//PARSE LINE
Scanner scan2 = new Scanner(currentLine);
String name = scan2.next();
if(!existsInTeams(name, teams)){
//add to teams
teams.add(new Team(name));
}
try {
t1Score = scan2.nextInt();
} catch(NoSuchElementException e) {
System.out.println("no such element exception: " + currentLine +
": line " + lineCounter);
System.exit(2);
}
String name2 = scan2.next();
if(!existsInTeams(name2, teams)){
//add to teams
teams.add(new Team(name2));
}
t2Score = scan2.nextInt();
//find teams
//probably more work to sort array, so linear search
Team team1=null, team2=null;
for (int i = 0; i<teams.size(); i++){
if (teams.get(i).getName().equals(name))
team1 = teams.get(i);
else if (teams.get(i).getName().equals(name2))
team2 = teams.get(i);
}
//we only are concerned with win or lose, not runs
//we don't need to set t2score here since it is implicit
if(t1Score > t2Score){
t1Score = 1;
} else if ((int) t1Score == (int) t2Score) {
t1Score = 0.5;
} else {
t1Score = 0;
}
evaluateRatings(team1, team2, t1Score);
}
}
for (int i = 0; i < teams.size(); i++){
System.out.println(teams.get(i).getName() + " " +
(int) teams.get(i).getRating());
}
}
private static boolean existsInTeams(String name, LinkedList<Team> teams)
{
for (int i = 0; i < teams.size(); i++){
if (teams.get(i).getName().equals(name)){
return true;
}
}
return false;
}
private static void evaluateRatings(Team teamA, Team teamB, double scoreA)
{
double scoreB;
double ratingA;
double ratingB;
double expectedA;
double expectedB;
scoreB = 1-scoreA;
ratingA = teamA.getRating();
ratingB = teamB.getRating();
//elo algorithm
expectedA = 1/(1+ Math.pow(10, (ratingB - ratingA)/400));
expectedB = 1/(1+ Math.pow(10, (ratingA - ratingB)/400));
teamA.setRating(ratingA + K_FACTOR * (scoreA - expectedA));
teamB.setRating(ratingB + K_FACTOR * (scoreB - expectedB));
}
}
class Team
{
private double rating;
private String name;
Team(String inName)
{
name = inName;
rating = 1200;
}
String getName()
{
return name;
}
void setRating(double newRating)
{
rating = newRating;
}
double getRating()
{
return rating;
}
}