ObjectiveArrays are one of the ways that Java allows for storing items in a list. In this lab, we will gain experience creating and manipulating arrays. FilesThere are three files that you need to download for this lab.
Be sure to create a separate directory for this lab -- you don't want to overwrite your previous Game.java with the one for this lab. Customize Your GameIn this lab, you will be able to customize your game. First, think about what theme you want your game to be. It can be the original idea of traveling across the US frontier to west coast via the Oregon trail. Or you could travel across a space-based map, along the lines of Star Wars / Star Trek / Serenity. Or it can be traveling across UVa's grounds. Or Harry Potter. Or anything else that you can imagine -- you can choose any theme that you want. As you do think about it, there are a few categories of things that you will need to pick for your customization:
More detail about what exactly is required for each of these categories is described as this lab progresses. Although there are 10 methods to implement, many of the methods operate the same way, so once you have written one, others should be easy. During the course of this lab, we will primarily use two different themes: one from the original Oregon Trail game, and one modeled after the Firefly / Serenity universe. The sample outputs at the end of this lab reflect these two themes. Task 1: Methods that just return a StringFour of the methods just return a single String value. This value can be kept in a String reference, or you can just have a return "foo"; as the sole line in the method body. The four methods are:
Once these methods are written, you can run the DescriptionGenerator class to make sure they are returning the correct values. Task 2: Methods that return a random StringThese four methods return a random String, selected from a set of Strings in an array. For each of the methods, you will have to create a String array that is explicitly initialized to contain the various values. Thus, you will be creating three arrays, one for each method listed below. These arrays should go outside the methods (i.e. be part of the class). Note that since you will be using the array from a static method, the arrays themselves must also be static. And they should be private. If you forget about explicit initialization, see the slide set for arrays, slides 16-17. Each of these methods will choose a random integer (from 0 to the size of the array), and return that particular element from the associated array. You will need to use the Game.rand object (it's of class Random -- so you use the nexInt() method). And recall that an array's length can be found by the .length property. All of these methods must have a corresponding array that contains the value, and must select a random value from that array -- you cannot have a big if-else-if clause or a switch statement for these methods. Note that it is possible to have successive calls to this method return the same value -- such is the case when randomly choosing items from an array in this manner. However, you will have to pick 10-20 values for each method, so the occurrence of duplicate choices will be reduced.
Task 3: getPartyMemberName()This method will return the names of the party members that are traveling with the leader. Like the previous methods, you will need an explicitly initialized array (also defined as static and defined outside the method) that contains the name of the party members. However, whereas the previous methods returned a random value from an array, this method will return successive values -- thus, you will not be able to get two party members of the same name. The easiest way to do this is to keep track (as a static int variable) of the last value returned, so that you can return the next value when the method is called again. You will need to choose at least 5 party member names, as the DescriptionGenerator class will try to obtain 5 such names. Task 4: getCorrespodingHealthChange()Once you know what health affliction one of your party members has, we will need to determine how much of a health penalty that affliction has caused. For example, it's probably more serious to be struck by lightning than to break an arm. Keep in mind that a person in the game will a maximum health of 10 -- so if you choose 10 (or more!) as the penalty for a given event, you will cause that person to die. You can also have events that happen that will raise the person's health -- for example, "find wild fruit" in the original game was beneficial. The method is defined as public static int getCorrespondingHealthChange (String event). The parameter that it takes in will be one of the events returned by the getRandomHealthEvent() method, described above. When you defined the getRandomHealthEvent() method, you defined an array that contained the Strings of the various health events. For the getCorrespondingHealthChange() method, you must define a second array OF THE SAME LENGTH. This second array will be of int values. Each value in the first array will correspond to the value in the same position in the second array. For example, if the 3rd value in the first array is "has dysentery", and the 3rd value in the second array is -5, then this means that when somebody has dysentery, it will cost 5 points of their health. A negative value in the second array will be a penalty to health; a positive value in the second array will be a bonus to health. The getCorrespodingHealthChange() method will thus need to search the first array to find that health event (remember that you cannot compare strings with ==; you must use the .equals() method). Remember it's position. Once found, you should return the int value in the same position in the second array. You MUST implement this method as a searching of an array; you cannot implement it as a big if-else-if clause or a switch statement. Task 5: getTerrainTypeName()This method will return the types of terrain. In the default game, they are "grassland", "forest", and "mountain". Whatever terrain types you choose, the one that corresponds to grassland should be easy to travel through ("street", "empty space", etc.). The one that corresponds to forest should be moderately difficult to travel through ("dorm", "nebulae", etc.). And the one that corresponds to mountain should be very difficult to travel through ("star cluster", "crowd of people", etc.). As we will see later in the game, it takes more time (and thus will consume more food) to travel through mountains than grassland. Alternatively, you could make the terrain corresponding to a mountain something that should be avoided (such as in the second output run, below). The method is defined as public static String getTerrainType (int terrainType). The value it takes in will be one of the integer constants in the Game class: TERRAIN_UNSET, TERRAIN_GRASSLAND, TERRAIN_FOREST, and TERRAIN_MOUNTAIN. If TERRAIN_UNSET is passed in as the parameter, then the method should just return "unset". Otherwise, it should return the String of your choice for that terrain type. DescriptionGeneratorThis class will test all the methods in your Descriptions class. A sample execution output appears below. Don't worry if duplicate entries occur (unless they occur with the getPartyMemberName() method, which would be bad). The first execution run uses the original Oregon Trail theme. This class will test the Descriptions class Task 1: methods that just return a String The party leader is: 'Aaron Bloomfield' The vehicle name is: 'Conestoga wagon' The map title is: 'Western United States Frontier' The propulsion name is: 'Oxen' Task 2: methods that return a random String 10 random terrain adjectives: sloped, wet, wet, rocky, flat, rocky, overcast, cloudy, sunny, bland 10 random depot names: Watermelon, Apple, Gourd, Potato, Watermelon, Date, Gourd, Apple, Gourd, Fig 10 random health events (and associated health penalty): has typhoid (-6) has dysentery (-5) has a broken leg (-4) has a broken arm (-2) has dysentery (-5) was bitten by a snake (-3) has typhoid (-6) has the mumps (-4) has dysentery (-5) has dysentery (-5) Task 3: getPartyMemberName() 5 party member names: Peeka, Mila, Gin, Tonic, Arlo Task 4: getCorrespondingHealthChange() This method was tested above (with getRandomHealthEvent()) Task 5: getTerrainTypeName() Unset terrain: unset Grassland equivalent: grassland Forest equivalent: forest Mountain equivalent: mountain
The second execution run uses the Firefly / Serenity theme. This class will test the Descriptions class Task 1: methods that just return a String The party leader is: 'Malcolm Reynolds' The vehicle name is: 'Serenity' The map title is: 'The 'verse' The propulsion name is: 'Fuel cells' Task 2: methods that return a random String 10 random terrain adjectives: dry, overcast, rocky, overcast, deserted, overcast, dry, dry, dry, dry 10 random depot names: Lilac, St. Albans, Verbena, Miranda, Dyton, Londinium, Santo, Bellerophon, Sihnon, Londinium 10 random health events (and associated health penalty): was knocked out (-3) was tortured by the bad guys (-5) was tortured by the bad guys (-5) was slashed by River (-2) was attacked by Reavers (-8) was tortured by the bad guys (-5) was shot (-5) had their amygdala stripped (-4) was attacked by Reavers (-8) was stabbed (-2) Task 3: getPartyMemberName() 5 party member names: Zoe, Wash, Kaylee, Jayne, Inara Task 4: getCorrespondingHealthChange() This method was tested above (with getRandomHealthEvent()) Task 5: getTerrainTypeName() Unset terrain: unset Grassland equivalent: empty space Forest equivalent: alliance patrol Mountain equivalent: Reavers! SubmissionWhen you are finished, you just need to submit the Descriptions.java file. As the DescriptionGenerator.java file was not modified, it does not need to be submitted. If you are done early....If you finish this lab early, use any remaining time to work on HW 8. |