+1 (315) 557-6473 

Baseball Game Simulator Assignment using C#


Baseball Game Simulator

You will create a baseball game simulator program. The program should allow the user to simulate the entire game all at once or play the game simulating half an inning at a time. There are 9innings in a game of baseball. Each inning, both teams will take turns at-bat and try to score as many runs as possible. The game should simulate a half-inning by allowing the team at-bat to score as many runs as possible until they earn 3 outs. The program needs to generate a random number to determine the outcome of each player at-bat. For example, you could simply randomly generate a number between 0 and 4 where 0 indicates an out, 1 indicates a single base hit, 2 indicates a double (player reaches 2nd base), 3 indicates a triple (player reaches 3rd base), and 4 to indicate a home run. However, this would cause a large score since a homerun will occur 1 in 5 times. The best implementation would be to assign better chances of getting outs and base hits, and lesser chances of getting triples and home runs. You will need to devise a way to accomplish this. Hint: you need to use a random range of values (1 – 100) instead of using 5 values (0 -5).

This program requires keeping track of several variables like the number of outs, what bases are occupied, the team that is currently at-bat, and a number of other elements for each half of the inning. The key is to keep track of certain elements for the entire game, and others for just the half-inning.

The challenging part is devising a way of moving the players around the bases as the half-inning is played. For example, a player at first base requires 3 moves to score a run: moving from first base to second base, second base to third base, and third base to home. This must be done using an Array. You cannot use if statements to determine how players will move. Otherwise, you would have somewhere in the vicinity of 44 number of if statements, and this wouldn’t be practical, nor would it be good programming. Hint: use an array to represent the bases, store values in the array to indicate whether a base is occupied by a player or not, and shift values in the array to simulate moving the players around the bases. This is the best solution to this problem.

The program should display the necessary information for each of the 4 innings as the game is played. You need to display the number of runs scored for the inning by both teams. When the game is over, all 9 innings should be displayed as being played with the number of runs scored for each team.

You need a BaseballSimulator class that will handle simulating one half of an inning (one team’s opportunity to score runs before getting 3 outs). You can write the class to simulate an entire inning at a time, but I will leave that to you. This class needs at least one method to simulate either a half of an inning (which you can call 8 times to run the entire 4 innings) or an entire inning, it also needs some way of getting the runs scored for the half-inning or entire inning, so the program that uses this class can display the runs scored.

Runs Scored

Solution:

Baseball Game Simulator:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BaseballGameSimulator { // This will simulate a single inning. // In baseball, when 3 players are out then // the inning is over. An "out" happens // when any of the batters team had been tagged with a ball, // striked out, or when the batted ball has been caught class BaseballSimulator { // Name of the team who will do the batting // There will be 9 batters in the team because // baseball rule states that there are 9 players each team private string battingTeamName; // Name of the team who will throw a ball to the batter, // catch a hit ball, and tag any of the batter's team // in the base private string pitchersTeamName; // A counter to keep track how many of the batters team // had been "out" private int outs; // A counter that keeps track how many of the batter's team // were able to complete a round of the bases. private int runs; // This will keep track how many bases are occupied private bool[] baseOccupied; // A random generator object to be used to simulate the events private Random random; // Initialize the batters and pitchers public BaseballSimulator(string battersTeamName, string pitchersTeamName) { this.battingTeamName = battersTeamName; this.pitchersTeamName = pitchersTeamName; outs = 0; runs = 0; baseOccupied = new bool[] { false, false, false }; random = new Random(); } // Batting players will move one step from 1 base to another private void rotateBasePlayers() { Console.WriteLine(battingTeamName + " players on base attempting to do run around on the base..."); for (int i = baseOccupied.Length - 1; i >= 0; i--) { if (baseOccupied[i]) { if (i == baseOccupied.Length - 1) { // A player in the 3rd base goes back at home Console.WriteLine(battingTeamName + " player at base " + (i + 1) + " running to home base."); runs++; } else { // Other players at 1st base and 2nd base runs on the next base Console.WriteLine(battingTeamName + " player at base " + (i + 1) + " running to base " + (i + 2) + "."); baseOccupied[i + 1] = true; } baseOccupied[i] = false; } } } // Access to the runs property public int Runs { get { return runs; } } // Start the simulation where picthers and batting team play an inning // After he simulation it returns the runs of the batting team public void runSimulation() { int fouls = 0; int strikes = 0; while (outs < 3) { // Pitcter throws a ball Console.Write(pitchersTeamName + " player pitched a ball to a " + battingTeamName + " player... "); // There's a 10% chance of a fowl if (random.Next(100) < 10) { Console.WriteLine("foul!"); fouls++; // 4 fouls will make the batter walk to the first base, and another batter // will make a play if (fouls == 4) { Console.WriteLine(pitchersTeamName + " player made a total of 4 fouls, " + battingTeamName + " player will walk to the first base."); rotateBasePlayers(); baseOccupied[0] = true; fouls = 0; } } // There's a 30% chance of a strike which happens when the batter misses the ball else if (random.Next(100) < 30) { Console.WriteLine("strike!"); strikes++; // 3 strikes then the batter is out if (strikes == 3) { Console.WriteLine(battingTeamName + " player made 3 strikes he's out."); outs++; strikes = 0; } } // There's a 10% chance of hitting the homerun else if (random.Next(100) < 10) { Console.WriteLine("homerun!"); // Make all players reach the home base rotateBasePlayers(); baseOccupied[0] = true; Console.WriteLine("Batting player runs towards 1st base."); for (int i = 0; i < 3; i++) rotateBasePlayers(); strikes = 0; fouls = 0; } else { // It's a hit Console.WriteLine("hit!"); // There will be base rotations, runners will try their best to reach the home base Console.WriteLine(battingTeamName + " players on the bases are moving!"); int numRotations = random.Next(1, 3); for (int i = 0; i < numRotations; i++) { if (i == 0) { rotateBasePlayers(); baseOccupied[0] = true; Console.WriteLine("Batting player runs towards 1st base."); } // For each rotation, there's 30% chance a fielder catches the ball if (random.Next(100) < 30) { // Randomly select a fielder to catch the ball int index = random.Next(baseOccupied.Length); Console.WriteLine(pitchersTeamName + " fielder tags base " + (index + 1) + "!"); if (baseOccupied[index]) { Console.WriteLine(battingTeamName + " player running towards base " + (index + 1) + " is out!"); outs++; baseOccupied[index] = false; break; } } } strikes = 0; fouls = 0; } if (outs < 3) { Console.WriteLine("Moving on..."); Console.ReadKey(); } Console.WriteLine(); } // If there are 3 outs in the batter's team, the inning is done Console.WriteLine(battingTeamName + " team have 3 players out. Inning is done."); Console.ReadKey(); } } }