• Tidak ada hasil yang ditemukan

ArrayList ArrayList ArrayList

What needs to change?

We have three classes that need to change: the Startup class (which is now called Startup instead of SimpleStartup), the game class (StartupBust), and the game helper class (which we won’t worry about now).

A

Startup class

Add a name variable

to hold the name of the Startup (“poniez,”

“cabista,” etc.) so each Startup can print its name when it’s killed (see the output screen on the opposite page).

StartupBust class continued...

Put the Startups on a grid rather than just a single row, and do it for all three Startups.

This step is now way more complex than before, if we’re going to place the Startups randomly. Since we’re not here to mess with the math, we put the algorithm for giving the Startups a location into the GameHelper (Ready-Bake Code) class.

Check each user guess with all three Startups, instead of just one.

Keep playing the game (i.e., accepting user guesses and checking them with the remaining Startups) until there are no more live Startups.

Get out of main. We kept the simple one in main just to...keep it simple. But that’s not what we want for the real game.

StartupBust The game class.

Makes Startups, gets user input, plays until all Start- ups are dead.

Startup The actual Startup objects.

Startups know their name, location, and how to check a user guess for a match.

Who does what in the StartupBust game

(and when) 1

detailed structure of the game

StartupBust

The game class.

StartupBust object

instantiates The main() method

in the StartupBust class instantiates the StartupBust object that does all the game stuff.

2

instantiates

The StartupBust (game) object instantiates an instance of GameHelper, the object that will help the game do its work.

helper

StartupBust object

GameHelper object

3

ArrayList object (to hold Startup objects)

The StartupBust object instantiates an ArrayList that will hold the three Startup objects.

helper startups StartupBust

object

GameHelper object

4

ArrayList object to hold Startup objects

The StartupBust object creates three Startup objects (and puts them in the ArrayList).

helper startups StartupBust

object

GameHelper object

Startup 0Startup

1 Startup

2

cells cells

cells

Startup objects

5

ArrayList object to hold Startup objects The StartupBust object asks the

helper object for a location for a Startup (does this three times, one for each Startup).

helper startups StartupBust

object

GameHelper object makelocation

Startup 0Startup

1 Startup

2

cells cells

cells Startup objects here it is

The StartupBust object gives each of the Startup objects a location (which the StartupBust got from the helper object) like

“A2,” “B2,” etc. Each Startup object puts his own three location cells in an ArrayList.

ArrayList object (to hold Startup cell locations)

cell 0 cell

1 cell2

ArrayList object

ArrayList object

cell 0 cell

1 cell2

cell0 cell 1 cell2

6

ArrayList object to hold Startup objects The StartupBust object asks the

helper object for a user guess (the helper prompts the user and gets input from the command line).

helper startups StartupBust

object

GameHelper object get user guess

Startup 0Startup

1 Startup

2

cells cells

cells Startup checkthisguess

here it is ArrayList object

(to hold Startup cell locations)

cell0 cell 1 cell2

ArrayList object

ArrayList object

cell0 cell 1 cell2

cell 0 cellcell

2

“hit”

The StartupBust object loops through the list of Startups, and asks each one to check the user guess for a match. Each Startup checks its locations ArrayList and returns a result (“hit,” “miss,” etc.).

And so the game continues...getting user input, asking each Startup to check for a match, and continuing

GameHelper helper ArrayList startups int numOfGuesses setUpGame() startPlaying() checkUserGuess() finishGame()

prep code test code real code prep code

DECLARE and instantiate the GameHelper instance variable, named helper.

DECLARE and instantiate an ArrayList to hold the list of Startups (initially three) Call it startups.

DECLARE an int variable to hold the number of user guesses (so that we can give the user a score at the end of the game). Name it numOfGuesses and set it to 0.

DECLARE a setUpGame() method to create and initialize the Startup objects with names and locations. Display brief instructions to the user.

DECLARE a startPlaying() method that asks the player for guesses and calls the checkUserGuess() method until all the Startup objects are removed from play.

DECLARE a checkUserGuess() method that loops through all remaining Startup objects and calls each Startup object’s checkYourself() method.

DECLARE a finishGame() method that prints a message about the user’s performance, based on how many guesses it took to sink all of the Startup objects.

METHOD: void setUpGame()

// make three Startup objects and name them CREATE three Startup objects.

SET a name for each Startup.

ADD the Startups to startups (the ArrayList).

REPEAT with each of the Startup objects in the startups List:

CALL the placeStartup() method on the helper object, to get a randomly-selected location for this Startup (three cells, vertically or horizontally aligned, on a 7 X 7 grid).

SET the location for each Startup based on the result of the placeStartup() call.

END REPEAT END METHOD StartupBust

The StartupBust class has three main jobs: set up the game, play the game until the Startups are dead, and end the game. Although we could map those three jobs directly into three methods, we split the middle job (play the game) into two methods to keep the granularity smaller. Smaller methods (meaning smaller chunks of func- tionality) help us test, debug, and modify the code more easily.

Prep code for the real StartupBust class

Variable