20 CS 112 – Fundamentals of Programming
Spring 2009
Laboratory 3a:Simple Data Structureand random generatorwith MATLAB
tive
Last week’s labs were to introduce you to simple math formula translation and plotting with MATLAB.
The goals of this week’s laboratory are to create some data structures to organize data and to make use of random generators in a simulation application. We will continue to use baseball as the backdrop for our examples.
Assuming you have beeninvited to be a virtualmanager (VM) for a MLB team for a day. You get to make up the lineup for the game and make roster adjustments as necessary as the game goes on.
With an Internet connection, you have access to any information from a data bank of the baseball world (e.g., msn.foxsports.com/mlb), where you can find all the information regarding MLB teams, players, stats and standings, and much much more. Your task is then to write a program to simulate the game of baseball and gain experience of managing a team by using the available data.
Part A.Familiarization with the world of baseball.
1) First, choose a MLB team and retrieve the info related to this team of your interest.
2)Look up recent game results to see how the data are reported.
3) Study the team roster and stats of the active players.
Part B. Rationalization of the rules of the game.
Games are played by two teams of nine players each. A (home) team first takes the field in various positions: the outfielders: left (LF), center(CF), and right (RF), the infielders: first base (1B), second base (2B), short stop (SS), and third base (3B), and a pitcher (P) on the center mound, and a catcher (C) at the home plate. The other team (visiting team) goes on offensive by sending a batter (ab) at a time to the batter’s plate.
The key concept of the game consists of the pitcher throwing the ball over the home plate and the batter can opt to watch it goes by or choose to swing the wooden bat and tries to hit the ball. The possible outcomes for each pitch include the following: -strike (S), ball (B), foul ball (F), 1base-hit (G), 2basehit (D), 3bhit (T), homerun (H), infield-out (I), and outfield-flyout (Y).
Teams take turns to hit and defend. When both teams complete a turn is an inning, and there is a total of nine innings. For each inning, there is a running count to keep the number of tries to a limit: 3 batters out per inning, and 3 strike-outs per player at the plate. Every player who hits the ball and the ball drops within the confine of the field boundary without being caught is entitles to run to the first base, and subsequent bases without being tagged by the ball, thus, becoming a base runner. Four balls can send batter to the first base safely, a walk. Foul balls are the hits that fall beyond the sidelines or behind the home plate. When a ball falls beyond outfield it is a home run.Base runners are allowed to run up the bases when the ball is active or hit. Normally, the following scenario takes place:
1basehit (single) would move the base runner(s) up one base, from 1b to 2b, 2b to 3b, and 3b to run.
2basehit (double) would move the base runner(s) two bases: from 1b to 3b, and 2b to run.
3basehit (triple) would move all the base runner(s) home. When there is a walk, current first base runner is forced to move up one base, so are the other base runners accordingly.
The defensive team can throw a runner out, by tossing the ball toward the bases before the runner reaches the base. Infield-out will normally keep all base runners put. Outfield fly-out can usually keep all runners put, except the 3b to home, as sacrifice-fly.
Part C. Play the role of a manager.
C. 1) Generate the lineup and output it to the press.
C. 2) Let the game begin and keep track of the flow of the game: who is at bat, what is the count, who is up, etc.
C. 2) Make substitution as necessary: determine the situation of the pitcher based on the number of pitches (e.g., keep the pitch count around a hundred) and the ER (earned run) rendered by your pitcher becomes intolerable. Other pitcher in the Bull-pen can be deployed any time of the game, and your batting order has to be adjusted dynamically.
PROGRAMMING TASKS:
Lab 3(a)
Your program will create for each team several data structures (vectors and arrays) in preparation for the game simulation: (a) the lineup, (b) the batting order for each inning, with a pointer for the current batter and the next batter, (d) base runner list, (e) the running pitch count for the batter, (f) batters count for the inning, and (g) the score.Create other structures as necessary.
Then, the simulation of the game begins with the first pitch and ends when 9 innings are played.
The game can be simulated by randomly generate one of these outcomes for each pitch.
This means that a random number generator can be used to create a value with a range, then map this number to one of the possible outcomes. For instance, a given range of [0,1] can be divided into, say, 20 equally spaced intervals. Each interval can be encoded to one of the outcomes of your choice, say, [B, S, S, F, G, G, D, T, H, I, I, Y, Y, Y, B, S, S, H, D,S], if the possible outcomes for each pitch are defined as follows: -strike (S), ball (B), foul ball (F), 1base-hit (G), 2basehit (D), 3bhit (T), homerun (H), infield-out (I), and outfield-flyout (Y).. A pitch equivalent of 0.78 would be mapped to a Strike.
Note that a random generator can be ‘controlled’ by the seed used, and the norm is to use a time based number to guarantee that seed used every time is different.
Lab 3(b)
For each play, the game data are updated by the rules of the game, which are now encoded in the logic of the program.
Make a roster change during the game.
The program should be able to generate the current batter’s stats, as well as a score board and team performance report on demand. The final stats will be generated for the press and uploaded to thebaseball data bank.
Cincinnati Reds’ 2009 Opening Day Stats