Created in January and February of 2019 as Part 1 of a sport simulation project. GitHub Link
Implementation of Compete method
The following code is in theTeam.java
file.
public void compete(Team rival) {
double result = (Math.random()*(this.ranking + rival.ranking));
if (result < ranking ) {
this.wins++;
rival.losses++;
this.ranking += .2;
rival.ranking += -.2;
}
else {
this.losses++;
rival.wins++;
this.ranking += -.2;
rival.ranking +=.2;
}
}
Here, the ranking instance variable is exactly what is described above (WAR + 150). A random number is generated within the range from 0 to the sum of the two team rankings. Then, if this random number is less than the current team's ranking, then they are awarded the win. Otherwise, they are awarded the loss. Each game, a team's ranking is adjusted by ±.2 to allow for mid season shifts in ability. Given the compete method, pre-season the Yankees would have a 54% chance of beating the Twins in any individual game.
Implementation of Season Class
In the MLB, each time is assigned a league and a division. There are 2 leagues, and each league has 3 divisions, each with 5 teams. Each team plays 19 games against every other team in their division (76 total), 6/7 games against each of the other 10 opponents in their league but not their division (66 total), and 20 inter-league games. Division and League games were easy to program because they just required looping through and doing some randomization. However, for inter-league play, I had to develop 6 different schedules and then randomly pick one from those. This is because of the existence of rival teams and some teams have multiple rivals which rotate by season.Future Improvements + Criticism
Home Field Advantage
One aspect of baseball which is not implemented in my program is the existence of home field advantage. Home field advantage is certainly present in baseball, especially so in the postseason. Unfortunately, my program did not account for home field advantage, but this is an improvement I would want to make in the future.Win Streaks
As with home field advantage, win streaks have the potential to dramatically change a team's probability of winning any given game. Momentum is the driving factor here, and I am looking into a way to implement this in the future.Example Output
AL East | AL Central | AL West | NL East | NL Central | NL West |
---|---|---|---|---|---|
Yankees: 100-62 | Indians: 89-73 | Athletics: 98-64 | Nationals: 93-69 | Brewers: 93-69 | Dodgers: 87-75 |
Rays: 92-70 | Tigers: 80-82 | Astros: 87-75 | Braves: 87-75 | Cubs: 87-75 | Giants: 80-82 |
Red Sox: 83-79 | Twins: 79-83 | Angels: 72-90 | Mets: 85-77 | Cardinals: 81-81 | Rockies: 79-83 |
Blue Jays: 81-81 | Royals: 78-84 | Rangers: 71-91 | Marlins: 72-90 | Pirates: 78-84 | Diamondbacks: 78-84 |
Orioles: 63-99 | White Sox: 75-87 | Mariners: 69-93 | Phillies: 69-93 | Reds: 76-86 | Padres: 68-94 |