CS1341 – Lab 5 Assignment
Overview For Lab 5, you will create a game called Peruna Pop using one Java class containing multiple methods. The lab will be split into three versions. Version 1 will contain basic game setup and functionality, and versions 2 and 3 will add additional functions and features.
Pre-lab due the week of 10/21. Lab due November 2 at 6:00am.
NOTES: Each program should include comments that explain what each block of code is doing. Additionally, the programs should compile without errors, and run with the results described in the exercise. The following deductions will be made from each exercise if any of the following is incorrect or missing:
Proper formatting [5 points] Proper names for classes and variables [5 points] Comments [5 points per class] Program doesn’t compile [ 10 points] Source code (java file) missing [ 10 points] Executable (class file) missing [10 points] Missing array where an array was required [5 points each] Missing loop where a loop was required [5 points each] Missing class from the design provided [10 points each] Missing method from the design provided [ 5 points each]
Pre-Lab (10 Points) Due at beginning of your lab class the week of Oct 21. Create the class named Pop1.java with the shells for the five methods listed in the Pop1 instructions. Read the instructions carefully and make sure you have the proper method headers for each of those methods (including parameters and return type) based on the information provided in the instructions.
Lab (90 Points) Due 11/2/19 6am Create the PerunaPop system version 1, version 2, and version 3 following the instructions provided. Note that each version will start with a working of the previous version. It’s best to build each solution incrementally, adding one feature at a time and testing it before continuing.
Submit the java and class files via Canvas (as a single zip-file). Include a comment block at the top of each Java file that includes your name, student id number, and “Lab 5-Fall 2019”.
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
$$$ Money
GAME SETUP:
GAME PLAY: Player selects an unpopped balloon to pop. If the balloon contains a money amount, the player adds the money to previously accumulated values.
If the balloon contains a horned frog, the player’s money is reset to 0, but play continues.
If the balloon contains Peruna, the player’s Peruna count is incremented.
Player continues to pop balloons until four Perunas have been popped. Player wins the accumulated amount at that time.
Twenty numbered balloons are randomly filled, 10 with a money amount and 10 with a Peruna. Then one of those balloons is replaced with a Horned Frog.
GAME INSTRUCTIONS
The first version of the game will contain basic game setup and play, but will be missing key game elements that will be added in version 2. Create Pop1.java containing five methods.
Version 1 – Pop1.java
Method parameters returns functionality
main String[] n/a Call the inflate method to setup the game, then create a loop that continuously does the following: – call the displayBalloons method – call the selectBalloon method with will return an int – call the pop method, using the balloon number returned by selectBalloon For version 1, loop continuously without a normal termination. (The user must force a break to end the program.)
inflate n/a n/a Using java.util.Random, loop through the static variable containing the array of balloon values and randomly assign the value 100 or 1 to each. 100 will represent money, and 1 will represent Peruna.
selectBalloon n/a int Prompt the user to enter a balloon number (1-20) and return that number to main
pop int void The balloon number is passed in from the main method. Subtract 1 from that number so it can be used as an index number of the balloons array. If the value at that array location is 100, add 100 to money and print a message. If the value is 1, increment the Peruna counter and display a message.
displayBalloons n/a n/a Display the current peruna count and total money accumulated so far. After that, create a loop that prints the contents of the array. On this printout, replace the 100 with a $, and replace the 1 with a P for Peruna. Format the output to print a CRLF after each five items, displaying four rows of five items. (Hint: use % function)
Static variables: • A static int variable containing money (initialized to 0). • A static int variable containing peruna count (initialized to 0). • An static int array of size 20 to contain the balloon values, which will initially contains all zeros.
See sample output on the next page…
Version 1 – Pop1.java Sample Output
Continued…
Hit ctrl-C to exit
> java Pop1
Perunas: 0 Money: $ 0
AVAILABLE BALLOONS: $ P $ $ $ $ P P P P $ P P P $ P P P P P
Select a balloon to pop (1-20): 6 You popped $100! Perunas: 0 Money: $100
AVAILABLE BALLOONS: $ P $ $ $ $ P P P P $ P P P $ P P P P P
Select a balloon to pop (1-20): 4 You popped $100! Perunas: 0 Money: $200
AVAILABLE BALLOONS: $ P $ $ $ $ P P P P $ P P P $ P P P P P
Select a balloon to pop (1-20): 20 You popped a Peruna!
Perunas: 1 Money: $200
AVAILABLE BALLOONS: $ P $ $ $ $ P P P P $ P P P $ P P P P P
Select a balloon to pop (1-20): 13 You popped a Peruna!
Perunas: 2 Money: $200
AVAILABLE BALLOONS: $ P $ $ $ $ P P P P $ P P P $ P P P P P
Select a balloon to pop (1-20): ^C
Make a copy of Pop1.java named Pop2.java. You will make changes to each of the items described below. You will now keep track of already selected balloons to prevent selection of a popped balloon. You will also prevent selection of a balloon outside the valid range of 1-20.
Version 2 – Pop2.java
Additional static variable: • An static boolean array of size 20 with all values set to false. ((false is the default value for all elements of a new boolean array)
See sample output on the next page…
main Method:
inflate Method:
selectBalloon Method:
pop Method:
displayBalloons Method:
<No changes>
Use the new boolean array to see if the selected balloon was already popped. If so, display an error message and force the user to make a different selection. Also display an error if the selected balloon number is outside the valid range of 1-20. Hint: Prompt using the Scanner inside a loop. Break out of the loop only when a valid selection is made.
Change the value of the balloon you are popping in the new boolean array to true. Note that the index number of the boolean array will correspond to the same index number of the balloons array.
We will now hide the balloon contents for unpopped balloons Using the new boolean array, if the balloon was popped (true) display $ for money or P for Peruna. For all those with the value false (unpopped), display the balloon number [ 1 ] through [ 20 ].
<No changes>
Hit ctrl-C to exit
Version 2 – Pop2.java Sample Output Re-prompt if
invalid entries $ java Pop2
Perunas: 0 Money: $ 0
AVAILABLE BALLOONS: [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 2 You popped a Peruna!
Perunas: 1 Money: $ 0
AVAILABLE BALLOONS: [ 1] P [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 4 You popped $100! Perunas: 1 Money: $100
AVAILABLE BALLOONS: [ 1] P [ 3] $ [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 6 You popped a Peruna!
Perunas: 2 Money: $100
AVAILABLE BALLOONS: [ 1] P [ 3] $ [ 5] P [ 7] [ 8] [ 9] [10]
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 2 Balloon was already popped! Select a balloon to pop (1-20): 25 Invalid selection! Select a balloon to pop (1-20): 8 You popped a Peruna!
Perunas: 3 Money: $100
AVAILABLE BALLOONS: [ 1] P [ 3] $ [ 5] P [ 7] P [ 9] [10]
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 10 You popped a Peruna!
Perunas: 4 Money: $100
AVAILABLE BALLOONS: [ 1] P [ 3] $ [ 5] P [ 7] P [ 9] P
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 12 You popped $100! Perunas: 4 Money: $200
AVAILABLE BALLOONS: [ 1] P [ 3] $ [ 5] P [ 7] P [ 9] P
[11] $ [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): ^C
Make a copy of Pop2.java named Pop3.java. You will make changes to each of the items described below. One balloon will now contain a Horned Frog, which erases money earned by a player. The balloon money value will now be a randomly generated value. Also, the game ends when a player collects 4 Perunas.
Version 3 – Pop3.java
static variables: <no changes>
See sample output on the next page…
main Method:
inflate Method:
selectBalloon Method:
pop Method:
displayBalloons Method: Instead of setting the money value to $100 for money balloons, call a new method named getBalloonValue which will return a number in the range of $50 – $1000 to be the money value of the balloon.
After populating the balloons array with either a money value or 1 (Peruna), generate a random number (0 – 19) to select one of those balloons and replace it with a -1. This will represent a horned frog.
Add an additional condition to test if the selected balloon contains -1. If true, print a Horned Frog message and reset the money total to zero.
Also change the if condition for balloons containing money. Money will no longer be $100, but will be a number greater than 1.
Also change the if condition here that checks for money values. It will be any number greater than 1 (not just 100.) Also check for a Horned Frog (-1) and print an H for that balloon instead of P or $.
When the peruna counter reaches 4, break out of the game loop and print the total winnings.
<No changes>
getBalloonValue Method:
This new method has no parameters and returns an int containing a random number in the range of 50 to 1000, that is a multiple of 50. (Hint: use % function) This method is called by the inflate method.
Horned Frog reset money to
$0
Version 3 – Pop3.java Sample Output $ java Pop3
Perunas: 0 Money: $ 0
AVAILABLE BALLOONS: [ 1] [ 2] [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 2 You popped $750! Perunas: 0 Money: $750
AVAILABLE BALLOONS: [ 1] $ [ 3] [ 4] [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 4 You popped a Peruna!
Perunas: 1 Money: $750
AVAILABLE BALLOONS: [ 1] $ [ 3] P [ 5] [ 6] [ 7] [ 8] [ 9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 4 Balloon was already popped! Select a balloon to pop (1-20): 6 You popped a Peruna!
Perunas: 2 Money: $750
AVAILABLE BALLOONS: [ 1] $ [ 3] P [ 5] P [ 7] [ 8] [ 9] [10]
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 8 Horned Frog! You lost your money!
Perunas: 2 Money: $ 0
AVAILABLE BALLOONS: [ 1] $ [ 3] P [ 5] P [ 7] H [ 9] [10]
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 10 You popped a Peruna!
Perunas: 3 Money: $ 0
AVAILABLE BALLOONS: [ 1] $ [ 3] P [ 5] P [ 7] H [ 9] P
[11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 100 Invalid selection! Select a balloon to pop (1-20): 13 You popped $700! Perunas: 3 Money: $700
AVAILABLE BALLOONS: [ 1] $ [ 3] P [ 5] P [ 7] H [ 9] P
[11] [12] $ [14] [15] [16] [17] [18] [19] [20]
Select a balloon to pop (1-20): 18 You popped a Peruna! You popped four Perunas! Game Over! You won $700!
Game ends when 4th Peruna is
popped
Entry must be 1-20
Selection was already
popped.