Program 1: Slot Machine
Due Date and Submission Requirements
- Due Date: Monday, September 18th at 11:59 p.m.
- Partner Information: You are allowed to work with one partner. Be sure to indicate in your submission who your partner is. Each Java class should have the name of each student in a comment at the top of the program
- Submission Instructions: Upload your solutions SlotMachine.java and SlotMachineDemo.java to the BrightSpace(D2L) Program 1 Dropbox.
The goal of this lab is:
- Write a complex Java class (Instance Fields, Methods, Constructor)
- Gain practice using OOP and multiple classes
- Gain practice writing different kinds of methods
- Use if statements to check conditions
- Use loops to iterate through a block of code or array
Background and Directions
A slot machine is a coin-operated gaming machine that generates a random combination of symbols. Players insert a coin into the machine, and pull the lever. The player wins varying amount of amount depending certain combinations of symbols. Here is example slot machine where there are 3 rows of symbols, and 3 columns of symbols
In this assignment, you will be developing a Java program that will emulate a slot machine game. A user starts off with a certain number of credits (you will define this value in your program), and each time they want to play the slot machine, they must use 1 credit. Your program will randomly generate symbols (which will be just be a random number 1, 2, or 3) and display the results to the screen
Your slot machine will be 3x3 grid, with 3 rows, and 3 columns. Here is an example roll of the slot machine. These numbers will be randomly generated each time the slot machine is played.
Once your program prints out the symbols/numbers of the machine. You program needs to determine the winnings of that play. There are five ways that a player can win credits by playing the slot machine.
- 1. If the sum of all the numbers on the slot machine is greater than 20, then they will win 2 credits. Example payout
- 2. If one of the diagonals in the slot machine has matching symbols, then they will win 2 credits. Both diagonals need to be checked. It is possible they could win 4 credits in total from both diagonals (if they are lucky). Example payout
- 3. If one of the rows all have matching symbols, then they will win 2 credits. They can only receive a maximum of 2 credits here, so if two different rows have matching symbols, they still will only win 2 credits. Example payout
- 4. If one of the columns all have matching symbols, then they will win 2 credits. They can only receive a maximum of 2 credits here, so if two different columns have matching symbols, they still will only win 2 credits. Example payout
- 5. If all 4 corners of the slot machine have matching symbols, then they will win 3 credits. Example payout
It's very possible that a player could win multiple credits from a combination of the winnings above. For example, here is a very lucky roll where the player satisfied all five of the possible winning conditions
Your program also needs to keep track of how many credits the player has, and the total winnings/credits that the machine has paid out.
There are many possible solutions to this assignment, so I am giving you the freedom to develop your solution in a way that makes sense to you. Your solution should have at least two classes, SlotMachineDemo.java and SlotMachine.java.
SlotMachineDemo.java
Your first Java class should be named SlotMachineDemo.java. This program should provide a menu-like functionality where the user has 4 choices:
1. Play the slot machine once (this costs one credit)
2. Play the slot machine N times (this costs N credits)
3. Print out the slot machine's total winnings
4. Print out the player's current number of credits
5. Exit program
It will keep printing out this menu until they gave a valid option, or until they exit the program. If the user does not have enough credits to play, then they should not be allowed to play the slots.
SlotMachine.java
This class should hold the instance fields and methods for playing one round of the slots. You will need to find a way to represent the symbols on the slot machine. I would recommend using three seperate integer arrays.
- private int[] top_row;
- private int[] middle_row;
- private int[] bottom_row;
(Alternatively, you could use a 2D array, but that makes things a little bit more complicated)
Additionally, you will need an instance field that will keep track of that machine's total winnings that have been paid out.
- private int total_winnings;
Assumptions
Your slot machine will only have 3 rows, 3 columns, and can only use 1, 2, or 3 for the symbols.
Required Output
Your program does not need to look exactly like this, but it should look very similar.
Here is a example output of the user playing one round at a time
Here is a example output of the user playing 3 rounds back-to-back
Optional Hints
I would recommend adding a method in the SlotMachine class called playOneRound() that will charge the user 1 credit, randomly generate the symbols, print out the symbols to the screen, and then determine the total number of winning and return the total winnings. Once you have this, you can invoke this method inside of a for loop for menu option #2
For each of the five winnings conditions, I would recommend having a method that checks each condition, and increases the player's total winnings if the method returns true. To check all the winning conditions, the easiest way would be to use the values of this.top_row, this.middle_row, and this.bottom_row. For example, to check for a matching row, this.top_row[0] == this.top_row[1] == this.top_row[2] would be one case you need to check for.
Here is some pseudocode that might help how to structure all the condition you need to check (you should not copy/paste this code... because it won't compile).
// I intentionally excluded some code that is needed for this to work. This is just pseudocode...
if(sumOfRows()) {
System.out.println("--> Sum of all numbers is greater than 20 (+2 credit)");
total_winnings +=2;
}
if(diagonals_left_to_right()) {
System.out.println("--> Matching Diagonals (+2 credit)");
total_winnings +=2;
}
if(diagonals_right_to_left()) {
System.out.println("--> Matching Diagonals (+2 credit)");
total_winnings +=2;
}
if(check_matching_rows()) {
System.out.println("--> Matching Row (+2 credit)");
total_winnings +=2;
}
if(check_matching_columns()) {
System.out.println("--> Matching Column (+2 credit)");
total_winnings +=2;
}
if(check_corners()) {
System.out.println("--> Matching Corners (+2 credit)");
total_winnings +=3;
}
Grading (100 points)
- 10 points- Your program allows the user to play one round of the slot machine (menu option #1)
- 10 points- Your program allows the user to play N roundS of the slot machine (menu option #2
- 5 points- Your program keeps track of the user's current credits (menu option #4)
- 5 points- Your program keeps track of the slot machine's total winnings that have been paid out(menu option #3)
- 10 points- Your program provides a menu, and keeps asking the user for a choice until they exit the program
- 10 points - Your program detects when the sum of all numbers is greater than 20, and awards the player 2 credits
- 10 points - Your program detects when there is matching diagonal(s), and awards the player 2 credits
- 10 points - Your program detects when there is a matching row, and awards the player 2 credits
- 10 points - Your program detects when there is a matching column, and awards the player 2 credits
- 10 points - Your program detects when all four corners match, and awards the player 3 credits
- 10 points- You have at least two Java classes. SlotMachineDemo.java and SlotMachine.Java