CS 142 Project 1: Leroy and Leora’s Treasure Hunt
If you have not completed the One Is Zero program, you might want to use that as a warm-up to this project, although that is optional, and does not need to be turned in.
For this project, you will write a game similar to the Leroy and the Cookies lab.
Game description
Leroy and Leora, the Rhodes College lynx mascots, are going on a treasure hunt. They have found an row of treasure chests, each with an integer amount of treasure inside. They decide to play a game to divide the treasure between them. Here’s how the game works. Each round begins by Leroy begins by picking a treasure. Then Leora automatically gets either the treasure to the left or right of the one Leroy chose (determined by a coin flip). Then Leroy picks again, and then Leora automatically gets treasure again. (So it’s really a one-player game where the second player doesn’t get to make any choices.) Leroy cannot pick a treasure that doesn’t have at least one treasure on the left or the right (Leora must get one treasure per turn). Leroy can end the game at any time, or it will end automatically when there are no treasures left. When the game ends, Leora automatically gets any treasure leftover. The winner of the treasure hunt is whoever has more treasure at the end!
What you need to do
- Create a new IntelliJ project. Inside, create a new Java class called
LeroyAndLeora. Write amainfunction inside this class that allows the user to play the game above, taking on the role of Leroy. (The computer will automatically assign Leora’s treasure and points.) - While your program is running, it should clearly show which treasure chests are remaining, the current scores, and prompt the user for which treasure Leroy should take.
- Your program should display the treasure chosen by each lynx as the choices are made.
- At the end of the game, announce the winner (or if it’s a tie).
Details
- The game always starts with ten treasures, indexed 0 through 9, which should be stored in an array of integers. Each treasure if initialized randomly to be an integer 10-99.
- Leroy should be prompted to enter the index of a treasure he wants. Then, Leora will be automatically given one of the treasures to the left or the right of Leora’s, chosen randomly. If Leroy chooses a treasure that only has one treasure to the left or the right (for example, by choosing either the left-most or right-most treasure, or a treasure that has a zero to the left or the right), Leora will automatically get the one available treasure. In other words, Leora will never get zero treasure on her turn.
- When Leroy or Leora gets a treasure, the treasure value in the array is simply replaced with a zero.
- Leroy should not be allowed to choose a treasure that has no treasure on either side (this can happen in a few different ways).
- Leroy may enter a treasure index of -1 to end the game. But whenever the game ends, Leora will get all remaining treasure.
- It is possible to end up in a situation where there are no valid moves for Leroy remaining. In this case, Leroy will be forced to enter -1 to end the game.
Sample interactions
Run 1:
=== Leroy and Leora's Treasure Hunt ===
Pick a treasure index for Leroy. Leora gets a randomly chosen treasure
to the immediate left or right of Leroy's treasure.
You cannot pick an index for Leroy where Leora would get zero treasure.
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 40 60 21 56 19 90 72 79 69 89
Scores: Leroy: 0 Leora: 0
Enter treasure index for Leroy (or -1 to quit): 9
Leroy collects treasure at index 9 worth 89 points!
Leora collects treasure at index 8 worth 69 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 40 60 21 56 19 90 72 79 0 0
Scores: Leroy: 89 Leora: 69
Enter treasure index for Leroy (or -1 to quit): 5
Leroy collects treasure at index 5 worth 90 points!
Leora collects treasure at index 6 worth 72 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 40 60 21 56 19 0 0 79 0 0
Scores: Leroy: 179 Leora: 141
Enter treasure index for Leroy (or -1 to quit): 3
Leroy collects treasure at index 3 worth 56 points!
Leora collects treasure at index 2 worth 21 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 40 60 0 0 19 0 0 79 0 0
Scores: Leroy: 235 Leora: 162
Enter treasure index for Leroy (or -1 to quit): 1
Leroy collects treasure at index 1 worth 60 points!
Leora collects treasure at index 0 worth 40 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 0 0 0 0 19 0 0 79 0 0
Scores: Leroy: 295 Leora: 202
Enter treasure index for Leroy (or -1 to quit): -1
Leora collects treasure at index 4 worth 19 points!
Leora collects treasure at index 7 worth 79 points!
Final scores: Leroy: 295 Leora: 300
Leora wins!
Run 2:
=== Leroy and Leora's Treasure Hunt ===
Pick a treasure index for Leroy. Leora gets a randomly chosen treasure
to the immediate left or right of Leroy's treasure.
You cannot pick an index for Leroy where Leora would get zero treasure.
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 21 71 76 43 23 27 81 26 30 48
Scores: Leroy: 0 Leora: 0
Enter treasure index for Leroy (or -1 to quit): 6
Leroy collects treasure at index 6 worth 81 points!
Leora collects treasure at index 5 worth 27 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 21 71 76 43 23 0 0 26 30 48
Scores: Leroy: 81 Leora: 27
Enter treasure index for Leroy (or -1 to quit): 2
Leroy collects treasure at index 2 worth 76 points!
Leora collects treasure at index 3 worth 43 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 21 71 0 0 23 0 0 26 30 48
Scores: Leroy: 157 Leora: 70
Enter treasure index for Leroy (or -1 to quit): 4
Invalid move! Please choose a valid index.
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 21 71 0 0 23 0 0 26 30 48
Scores: Leroy: 157 Leora: 70
Enter treasure index for Leroy (or -1 to quit): 1
Leroy collects treasure at index 1 worth 71 points!
Leora collects treasure at index 0 worth 21 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 0 0 0 0 23 0 0 26 30 48
Scores: Leroy: 228 Leora: 91
Enter treasure index for Leroy (or -1 to quit): 9
Leroy collects treasure at index 9 worth 48 points!
Leora collects treasure at index 8 worth 30 points!
Treasures:
Index: 0 1 2 3 4 5 6 7 8 9
Value: 0 0 0 0 23 0 0 26 0 0
Scores: Leroy: 276 Leora: 121
Enter treasure index for Leroy (or -1 to quit): -1
Leora collects treasure at index 4 worth 23 points!
Leora collects treasure at index 7 worth 26 points!
Final scores: Leroy: 276 Leora: 170
Leroy wins!
Getting started
Here are the steps I suggest you follow to write the program. In particular, we will break the program down into small parts, testing each part before we write the next part.
- Write code that creates an array of 10 integers and assigns random numbers from 10-99 to each position in the array.
- Write code that will print out the above array with indices and the array contents (this will become your treasure array).
- Write code that will prompt the user for an index in the array, let them type in the index, and then it will give the points for the treasure to Leroy, and re-assign that index in the array to zero. At this point, do not worry about checking if the index is valid.
- Write code that will perform the previous step in a loop, until the user types -1.
- Write code that will assign Leora’s treasures after Leroy’s choice. This is probably the hardest step, as it requires a good amount of logic to check how many treasures are available to the left and right of Leroy’s treasure, taking into account treasures that might already be zero or treasures on the ends of the array.
- Some things to think about:
- How do you simulate the “coin flip” to pick randomly between the left and right treasure to give to Leora? Hint: Use
Math.random()to generate a number. Since you know this number is between 0 and 1, what does it mean if the number is less than 0.5? - How do you check if a treasure to the left or right is 0? (already been chosen)
- How do you check if a treasure to the left or right doesn’t exist? (off the end of the array)
- Write code to check if the treasure Leroy asks for is valid (allows for Leora to get treasure as well).
- Write code to give Leora all the remaining treasures after Leroy elects to stop the game.
- Write code to figure out who wins.
Guidelines
- Your program does not have to have output that 100% matches mine, but all the functionality seen above should be there, in particular:
- You should display the treasures clearly indicating the indices and the treasure amounts.
- Current scores should be displayed before Leroy gets to pick a treasure.
- Every time a treasure is given, print both the index and the treasure amount being awarded.
Use of AI or other outside code
I encourage you not to use AI to write this program. Generative AI is a powerful tool, but learning to write code without it is necessary to later use AI to its full advantage. So for the moment, using AI to write code is not allowed.
All your code must be developed and written independently by yourself. Any ideas you receive from outside sources (people, websites, or AI), must be acknowledged. This can be done with a comment in the code, explaining where the idea came from, including the person’s name, website URL, or name of the AI.
If you would like to use AI to help debug code that you’ve already written, that is fine. In particular, you can ask for help from AI as you would a human. You wouldn’t ask a human to write the code for you, so don’t ask an AI either. Here’s a good prompt that will get an AI to act like a tutor for you.
Hints and tips
- I suggest using the Leroy and the Cookies lab as a guide for this program. In particular, you should use an array of integers (
int[]) to represent the treasures. You should make an array of 10 ints (int[] treasure = new int[10]). You shouldn’t need to do much (if any) string handling in this program, but if you need to, be aware that comparing
Strings in Java is a bit tricky. If you have two Strings, you shouldn’t compare them with==or!=:String str = scanner.next(); if (str == "A") { // don't do this! statements... }The code above is legal, however the comparison will sometimes work correctly and sometimes not (it will be clear later).
The correct way to compare
Strings in Java for equality or inequality is:// compare for equality if (str.equals("A")) { statements... } // compare for inequality: if (!str.equals("A")) { // the exclamation point means "not" statements... }
Style, comments, and pledge
You should use good programming style when writing your program, except that you don’t need to use functions in Java because we haven’t fully learned them yet. You may use them if you so desire. All other style guidelines, including proper indentation and comments, should be followed.
What to turn in
Through Canvas, turn in your LeroyAndLeora.java file. Additionally, upload a text file answering the following questions:
- What bugs and conceptual difficulties did you encounter? How did you overcome them? What did you learn?
- Describe any serious problems you encountered while writing the program (larger things than the previous question).
- Describe whatever help (if any) that you received. Don’t include readings, lectures, and exercises, but do include any help from other sources, such as websites or people (including classmates and friends) and attribute them by name.
- Did you do any of the challenges (see below)? If so, explain what you did.
- List any other feedback you have. Feel free to provide any feedback on how much you learned from doing the assignment, and whether you enjoyed doing it.
Challenge Problems
From time to time, I will offer “challenge problems” on assignments. These problems are designed to have little (but some) impact on your grade whether you do them or not. You should think of these problems as opportunities to work on something interesting and optional, rather than a way to raise your grade through “extra credit.”
Policy on challenge problems:
- Challenge problems will typically allow you to get 2-5% of additional credit on an assignment, yet they will typically be much more difficult than this credit amount suggests.
- You should not attempt a challenge problem until you have finished the rest of an assignment.
- The tutors will not provide help on challenge problems. The instructor will provide minimal assistance only, as these problems are optional and are designed to encourage independent thought.
- Challenge problems may be less carefully specified and less carefully calibrated for how difficult or time-consuming they are.
- If you solve a challenge problem, include a comment at the top of your program detailing what you did.
Challenge problems for this assignment:
- Add in logic to detect when there are no more valid moves in the game (this can happen if there are no treasures left, but also if the only treasures left would result in Leora getting no treasure).
- Develop a strategy for the game and print the suggested move for your strategy at the beginning of each turn. (Explain what your strategy is in a comment.)