CS 1120 Spring 2013LA 5 (Part II)Baseball Simulation

Lab Assignment 5 (Part II)
Baseball Simulation

Note: This is a continuation of LA5, which is still due next week. This part will add a few additional requirements.

Concepts

  • GUI control methods
  • Random number generation
  • Linked Lists
  • Binary Input/Output

Problem Specification

You will be implementing the logic for a simplified baseball simulation. Your simulation will obey the following rules:

  • 9 innings, 3 outs per inning, 3 strikes per out
  • Each time a player bats, they roll a float between zero and one. If their roll is greater than their batting average, they get a strike
  • Each time a player runs to the next base (including first), they must similarly roll a number. If the roll is greater than their running average, they are out
  • If the batter is tagged out while running to first, no one else moves.
  • If a player reaches home, they score a point
  • Players are placed back on the bench in the order they are tagged 'out' or reach home.

Once this is complete, you will write methods to control and display your simulation via a GUI. Your GUI should display the following information:

  • Score, current inning (team colored, your choice), outs, and strikes
  • The button representing each base should be team-colored if there is a player present, or gray and disabled if there is no player present.
  • Clicking on a base should display a dialog box with the player's name, running average, and batting average. Home base should display the current batter's stats.
  • The current batting lineup for each team
  • Each time a player performs an action, a dialog box should inform the user of the outcome

You must seed the RNG (instantiate it with a number).

Design Phase

Work with your partner, following the design process outlined here. Together, you must submit a hard copy of the Design Phase of your lab report by the end of lab today (only one copy needed for each pair).

Basic Structure

As always, first think about basic structure. First, you must add methods to your BaseballPlayer class to generate the random numbers. These methods should use a single instance of the Random class in order to generate all numbers. Do not instantiate Random classes for each BaseballPlayer or for each call to these methods.

public boolean makesRun();

publicboolean hitsPitch();

Next, you will need to implement the logic of the simulation. Use the methods you designed above to implement the rules as described above. swapTeams should handle any cleanup, such as removing players from bases, required between innings or the top/bottom of an inning. The filenames of the two rosters to load should be provided in the constructor of the simulation. Modify the GUI provided as appropriate.

private void pitchBall();

private void runBases(BaseballPlayer batter);

private void swapTeams();

public BaseballGUI(String filenameA, String filenameB);

We have provided a simple GUI for you to display your simulation with. You must add methods to update the GUI and display information about players, as well advancing the simulation. You should also implement a “helper” method to display player information and updates on the simulation via dialog boxes as it progresses. You will also need to implement listener methods to provide interactivity.

private void updateGUI();

private void showPlayerStats(BaseballPlayer player);

private void showMessage(String msg); //could be used to print console output too!

In order to update the JList boxes, you will need to add a method to your RosterLinkedList to produce an array of player names. Pass this to the setListData() methods of lstTeamA and lstTeamB.

public String[] getPlayerNames();

Pseudocode

The steps below will ask you to write pseudocode or answer a question. Do this in your lab report.

  1. What keywords can be used to make a single instance of the Random class shared across all instances of a class? Why should this be done instead of multiple instances?
  1. Write pseudocode for the pitchBall() and runBases() methods as described above. Be sure to specify when each player is taken from or returned to the bench, as well as where they are stored in the meantime. If necessary, also write any code that should be used to “clean up” any players still on the field at the end of the inning.
  1. Explain what a “listener” method is and when your listener methods will be triggered.
  1. What method can be used to disable a component?

Implementation Phase

The implementation of GUI and game logic, along with the

original requirements from last week, will be due in one week at the beginning of your lab. Be sure to

include a complete copy of the code in your lab report.

Additional Notes:

All other requirements, including the Testing and Maintenance phases, are described in the first part of

this assignment. Provide screenshots demonstrating the functionality you have provided, and discuss

how you tested your application on your lab report. Each student must individually turn the assignment

in to their dropbox on eLearning, and each group must turn in one hard copy.

Pair Programming and Subversion:

For this assignment, you must work in a group. Each group member must make at least two substantive

check-ins to the Subversion repository.