Baseball Program - Batter vs. Pitcher
There are 8 outcomes that could occur during an at bat:
1. Strike out
2. Walk
3. Single
4. Double
5. Triple
6. Home Run
7. Ground out
a. runners advance
b. double play/runners don't advance
8. Fly out
a. normal fly out
b. sacrifice fly out
To resolve this, we will make 2 classes: a Batter class and a Pitcher class. The pitcher class will be a subclass of Batter, since pitchers will be batting as well.
The Batter class will have the following skills: strikeout, walk, single, double, triple, homerun, groundout, and flyout.
The Pitcher class will have the following skills: pStrikeout, pWalk, pSingle, pDouble, pTriple, pHomerun, pGroundout, and pFlyout.
Each of these skills will be values 1.0 to 10.0. It will never be lower than 1.0, nor higher than 10.0. The higher the value, the better for the player.
To explain what we are about to do, let us create two players: a power hitter and ground ball pitcher.
A power hitter will strike out a lot(low stat), hit more homeruns(high stat), rarely walk(low stat), hit a lot of fly balls(low stat) and doubles(high stat).
While a ground ball pitcher would walk a bit more(low stat), give up more singles(low stat), get lots of ground balls(high stat), give up very little home runs(high stat) and fly balls(low stat).
Here's an example of what is described:
Power Hitter Batter
|
Category
|
Stat
|
strikeout
|
2
|
walk
|
2
|
single
|
5
|
double
|
7
|
triple
|
2
|
homerun
|
7
|
groundout
|
6
|
fly out
|
3
|
Ground Ball Pitcher |
Category |
Stat |
strikeout |
5 |
walk |
4 |
single |
3 |
double |
5 |
triple |
7 |
homerun |
7 |
groundout |
8 |
fly out |
3 |
We want to fill up a bag with possible outcomes based on the difference between these stats.
Outcomes that favor the pitcher should be decided by the pitcher stat minus the batter stat. Outcomes that favor the batter should be decided by the batter stat minus the pitcher stat. There will always be at least one strikeout, walk, single, groundout, and fly out in the Array List.
Here is how we would calculate the ArrayList
Power Hitter Batter vs Ground Ball Pitcher
|
Category
|
Favors
|
Batter
|
Pitcher
|
outcome added
|
strikeout
|
pitcher
|
2
|
5
|
5 - 2 = 3
|
walk
|
batter
|
2
|
4
|
2 - 4 = -2
|
single
|
batter
|
5
|
3
|
5 - 3 = 2
|
double
|
batter
|
7
|
5
|
7 - 5 = 2
|
triple
|
batter
|
2
|
7
|
2 - 7 = -5
|
homerun
|
batter
|
7
|
7
|
7 - 7 = 0
|
groundout
|
pitcher
|
6
|
8
|
8 - 6 = 2
|
fly out
|
pitcher
|
3
|
3
|
3 - 3 = 0
|
So, the arrayList built would hold:
SO
|
SO
|
SO
|
Walk
|
1B
|
1B
|
2B
|
2B
|
GO
|
GO
|
FO
|
So there are 3 strikeouts, 1 walk, 2 singles, 2 doubles, 2 ground outs, and 1 fly out, for a total of 11 outcomes. We would randomly draw one of these and that would be the atBats out come.
Based on the outcome, the benefactor would receive a +0.1 bump in their stat while the other player would receive a -0.1 drop in there stat.
So if the outcome was a strikeout, then the pitcher's strikeout stat would become 5.1 and the batter's strikeout would become 1.9.
If the outcome was a double, then the batter's double stat would become 7.1 and the pitcher's double would become 4.9.
This allows the simulation to alter based on previous events.
However, if we only stood with this, over time the stats would get out of balanced so we need to employ a method to punish/reward stats that are not being used over time.
Batter
|
Category
|
punish/reward condition
|
strikeout
|
6 at bats without a stikeout +0.1
|
walk
|
6 at bats that end in an out -0.1
|
single
|
6 at bats that end in an out -0.1
|
double
|
6 at bats without a double -0.1
|
triple
|
an entire season without a triple -0.1
|
homerun
|
12 at bats without a homerun -0.1
|
groundout
|
6 at bats without a ground out +0.1
|
fly out
|
6 at bats without a fly out +0.1
|
Pitcher
|
Category
|
punish/reward condition
|
strikeout
|
6 at bats without a stikeout -0.1
|
walk
|
6 at bats that end in an out +0.1
|
single
|
6 at bats that end in an out +0.1
|
double
|
6 at bats without a double +0.1
|
triple
|
an entire season without a triple +0.1
|
homerun
|
12 at bats without a homerun +0.1
|
groundout
|
6 at bats without a ground out -0.1
|
fly out
|
6 at bats without a fly out -0.1
|
This will hopefully balance out the bonuses.
Finally, let's talk about how to simulate a whole game:
There are 9 innings played in a game, each inning is split into a visitor half inning and a home half inning. Each respective inning ends when 3 outs are made. So the order of innings are
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
RUNS
|
VISITOR
|
|
|
|
|
|
|
|
|
|
|
HOME
|
|
|
|
|
|
|
|
|
|
|
The top of the first inning will have the visitors bat first, they get to continue batting until they get 3 outs. Then the home team will bat next, they get to continue batting until they get 3 outs. Then the visitor team will bat in the second inning. This continues until the end of the game.
The game ends when
1. The home team makes the final out of the ninth inning and the visiting team has more runs than the home team.
2. The visiting team makes the final out of their ninth inning and the home team has more runs than the visiting team.
3. The home scores a run in the ninth inning that gives them more runs than the visiting team has.
If none of those have happened by the end of the ninth inning, then the teams play another inning and continue to play innings until one of the 3 conditions are met for that particular inning. So we could have games that go for 10 innings or 14 innings.
How to score a run
In order score run, a batter/baserunner must go to first, then second, then third, then home. If the batter/baserunner goes to home a run has been scored. Here is how we advance runners based on at bat outcomes
Batter/Baserunner outcome
|
Category
|
Batter
|
Man at 1st
|
Man at 2nd
|
Man at 3rd
|
strikeout
|
out
|
no advance
|
no advance
|
no advance
|
walk
|
go to 1st
|
go to 2nd
|
go to 3rd if man at 1st
|
go home if man at 1st and 2nd
|
single
|
go to 1st
|
go to 2nd
|
go to 3rd
|
go home
|
double
|
go to 2nd
|
go to 3rd
|
go home
|
go home
|
triple
|
go to 3rd
|
go home
|
go home
|
go home
|
homerun
|
go home
|
go home
|
go home
|
go home
|
groundout - adv
|
out
|
go to 2nd
|
go to 3rd
|
go home
|
grondout - dp/na
|
out
|
out
|
if man at 2nd go to 3rd else no advance
|
if man at 1st and 2nd go home else no advance
|
fly out - na
|
out
|
no advance
|
no advance
|
no advance
|
fly out - sac
|
out
|
no advance
|
go to 3rd
|
go home
|
When a runner or batter goes home, then a run is scored. A batter gets a run batted in (RBI) for each run scored on his at bat unless he hit a double play (2 OUTS on an at bat). If an third out is recorded at the same time a run is scored, the runs scored on that at bat does not count.
Batting order
The batters are put in a batting order that is rotated through during the game. So no batter can bat another time until all the other batters have batted. You will have a batting order given to you. You will start at the first player and rotate through. There will be 9 batters. We will consider the pitcher to be a batter in the national league home game (our default teams will be national league) and the pitcher will have a designated hitter in our American league home games (a tenth batter with DH as his position will be among your list). You can randomize your batting order before any game or choose to use a default order from the file you read in the information from. Once you set a batting order you cannot change it. The positions that are given are C, 1B, 2B, 3B, SS, RF, CF, LF, DH, SP (where SP and DH are dependent on the game).
Pitching rotation
Unlike the position players, a pitcher cannot pitch every game, so there are 5 starting pitchers (SP). You will rotate a pitcher, much like a batting order, for each game. A pitcher cannot pitch again until the other 4 pitchers have pitched a game. A pitcher will pitch the entire game.
File
There are way too many variables to input through a keyboard each time, so you should make a file. Here is how a file should be held:
Team Name
League
Batter Name
strikeout walk single double triple homerun groundout fly pos
Pitcher Name
batting stats first
strikeout walk single double triple homerun groundout fly
pitching stats second
strikeout walk single double triple homerun groundout fly pos
Next Team Name
Plan out players stats at first so you can test out the game. Later you may want to write a program that randomly generates a batter and a pitcher and saves it into a file, so you can quickly create 30 teams rosters. Right now we are going to just use two teams to test the game.
Showing results
There are way too many outputs to print out on a keyboard so we should print to a file the game. We want to create a uniform line to print out so that we can easily see what is going on. Here is what we should do
Visting team(0-0) vs. Home team(0-0) //This is the header before every game
SCORE INNING BATTER ATBAT OUTCOME OUT 1B 2B 3B RUN //for every at bat
END OF HALF INNNING SCORE
SCORE INNING BATTER ATBAT OUTCOME OUT 1B 2B 3B RUN //for every at bat
ETC
END OF GAME FINAL SCORE
TEAM WINS(RECORD)
TEAM LOSES(RECORD)
VISITING STATS
BATTERS FIRST
PLAYER POS ATBATS HITS 2BS 3BS HR RUNS RBIS WALKS STRIKEOUTS AVERAGE
PITCHERS LAST
PLAYER (W/L) INN PITCHED HITS 2BS 3BS HR RUNS ERA WALKS STRIKEOUTS REC
HOME STATS
BATTERS FIRST
PLAYER POS ATBATS HITS 2BS 3BS HR RUNS RBIS WALKS STRIKEOUTS AVERAGE
PITCHERS LAST
PLAYER (W/L) INN PITCHED HITS 2BS 3BS HR RUNS ERA WALKS STRIKEOUTS REC
Here is an example:
Texas Rangers(72-83) vs Houston Astros(84-71)
3-0 T3 John Smith FLYOUT NO ADVANCE 2 outs Red Jones@2B
4-0 T3 Gil Beaumont DOUBLE 2 outs Gil Beaumont@2B Red Jones scores
4-0 T3 Louis Bree GROUNDOUT ADVANCE 3 outs
END OF T3 3-0
4-1 B3 Dill Pickles HOMERUN 0 outs Dill Pickles scores
4-1 B3 J.T. Sky STRIKEOUT 1 outs
...
END OF GAME 8-7
Texas Rangers wins (73-53)
Houston Astros lose (84-72)
Texas Rangers Stats
Batter POS AB H 2B 3B HR R RBI BB K AVG
J. Smith CF 4 0 0 0 0 0 0 0 2 0.254
G. Beaumont 1B 4 4 1 0 1 2 3 0 0 0.299
L. Bree SS 3 1 0 0 0 2 0 1 1 0.199
Pitcher IP H 2B 3B HR R ERA BB K W-L
A. Mort(W) 9 7 2 0 3 7 4.51 4 7 4-9
Houston Astros Stats
Batter POS AB H 2B 3B HR R RBI BB K AVG
D. Pickle 2B 4 2 0 0 1 1 1 0 2 0.339
J. Sky LF 4 0 0 0 0 0 0 0 4 0.101
Pitcher IP H 2B 3B HR R ERA BB K W-L
L. Cotton(L) 9 14 4 0 1 8 3.15 3 2 11-3
A notes about the stats:
1. when a player is walked, statistically it does not count as an at bat.
2. a hit is a single, double, triple, or a homerun. So when you see 4 hits and 1 double and 1 homerun, it means that the other two hits were singles.
3. a batting average is the total number of hits/the total number of at bats (this is a season stat, so the numbers are accumulated throughout every game they play)
4. ERA (earned run average) is calculated by the total number of runs/(number of innings pitched/9). It uses season totals like the batting average does. Why do we divide by 9? Because we are trying to see how many runs a pitcher gives up per game, since it is possible for a pitcher to only pitch 8 innings we can't just divide by number games. So we count the number of innings pitiched (IP) and divide by 9 to extrapolate a 9 inning performance.
Attachment:- Assignment File.rar