Program 1: Slot Machine

Due Date and Submission Requirements


The goal of this lab is:


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.


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: 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. (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.

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)