Docsity

Slot Machine Assignment: Writing a Java Program for a Simple Slot Machine Game - Prof. Gre, Study notes of Computer Science

Toggle sidebar
bg1
Computer Programming I Instructor: Greg Shaw
COP 2210
Programming Assignment #6
(Decision-Making)
I. The Assignment
A typical slot machine consists of 3 rotating disks. If
certain combinations appear when the disks stop spinning, the
player wins varying amounts of money.
Suppose that each disk contains the integers from 0 through 9.
Write a program where the user enters the values for the 3
disks and the amount wagered, and an appropriate message is
printed as shown in the following table.
Result Payoff
All 3 Numbers
The Same 100 to 1
Any 2 Numbers
The Same 4 to 1
3 Consecutive Numbers 60 to 1
Anything Else 0
(Player Loses)
Note that “3 Consecutive Numbers” means that combinations
such as these would win:
1 2 3 3 2 1 4 5 6 6 5 4 etc.
...but not these:
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Slot Machine Assignment: Writing a Java Program for a Simple Slot Machine Game - Prof. Gre and more Study notes Computer Science in PDF only on Docsity!

Computer Programming I Instructor: Greg Shaw COP 2210

Programming Assignment

( Decision-Making )

I. The Assignment

A typical slot machine consists of 3 rotating disks. If certain combinations appear when the disks stop spinning, the player wins varying amounts of money. Suppose that each disk contains the integers from 0 through 9. Write a program where the user enters the values for the 3 disks and the amount wagered, and an appropriate message is printed as shown in the following table.

Result Payoff

All 3 Numbers The Same 100 to 1 Any 2 Numbers The Same 4 to 1 3 Consecutive Numbers 60 to 1 Anything Else

(Player Loses)

 Note that “3 Consecutive Numbers” means that combinations

such as these would win: 1 2 3 3 2 1 4 5 6 6 5 4 etc. ...but not these:

1 3 2 (consecutive but not in order) 9 0 1 (no “wrap around”)

A zip file containing both Java classes and the 5 sets of output

VI. Due Date: Thursday, October 27th

VII. Extra Credit Opportunities (2)

  1. Have your method that evaluates the spin call three separate boolean methods that indicate whether 1.) three numbers are the same, or 2.) two numbers are the same, or 3.) the three numbers are consecutive
  2. Instead of defining “3 Consecutive Numbers” as shown above, allow consecutive numbers in any order. I.e., winners would include combinations such as: 1 2 3 1 3 2 2 1 3 2 3 1 3 2 1 3 1 2 (but still no “wrap arounds” such as 8 9 0)

 Note that the payoff for three consecutives in any

order would be only 20 to 1 (not 60 to 1) Slot Machine Assignment (6) (Copy and paste when necessary) SlotMachine.java /* File: SlotMachine.java

  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / /*
  • A class that models a typical slot machine in a casino. / public class SlotMachine { // Instance Variables: private int slot1 ; // The value of the first slot private int slot2 ; // The value of the second slot private int slot3 ; // The value of the third slot private int wager ; // The amount wagered /*
  • The constructor that creates a slot machine object
  • to input the slot values.
  • @param inSlot1 The value of the first slot

return same3Slots ; } /**

  • A method to check if two of the three slot values are identical
  • @return True iff. two slots are the same / public boolean same2Slots() { boolean same2Slots = false ; // Default assumption // Under any of the following conditions, the boolean value will be true if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) { same2Slots = true ; } return same2Slots ; } /*
  • A method to verify any of the slot values as consecutive,
  • (hence,CTS in the method name);
  • this method includes these numbers in any order like "1 2 3", "1 3 2",
  • "3 1 2", et cetera but no "wrap arounds" like "9 0 1" and "8 9 0".
  • @return True iff. the slot values are CST as mentioned above */ public boolean slotsAreCST() { boolean slotsAreCST = false ; // Default assumption // When viewing the three slot values that are consecutive in any order, // adding their absolute values of their differences (the three // differences to calculate in total as shown below) will always be 4 if (Math.abs(slot1 - slot2) + Math.abs(slot1 - slot3)
  • Math.abs(slot2 - slot3) == 4) { slotsAreCST = true ; } return slotsAreCST ; } /**
  • Method to limit the numbers on the slot from 0 to 9

inclusive

  • @return True iff. each slot value is between 0 & 9 inclusive / public boolean wrongSlotNumber() { boolean wrongSlotNumber = false ;// Default assumption if ((slot1 < 0 || slot1 > 9) || (slot2 < 0 || slot2 > 9) || (slot3 < 0 || slot3 > 9)) { wrongSlotNumber = true ; } return wrongSlotNumber ; } /*
  • A method to evaluate the play and return the proper reward for playing.
  • @return The payment for playing a game on the slot machine */ public String playSlots() { String receipt ; // The bill that states the prize and wager // based on the following conditions: if (same3Slots()) // If I have the same 3 slot numbers, { int prize = (100 * wager) ; // Then this is the amount that I won! receipt = "Congratulations! You have won the jackpot! You won: $"
  • prize + "! Also, reclaim your wager of: $" + wager ; } else if (same2Slots()) // If I have two slot numbers that are equal, { int prize = (4 * wager) ; // Then this is the amount that I won! receipt = "Congratulations! You have won a prize! You won: $"
  • prize + "! Also, reclaim your wager of: $"
  • wager ; } else if (slotsAreCST()) // If I have consecutive slot
  • none of it is the work of any other person.
  • The class that simulates a real slot machine; it just needs random numbers */ import javax.swing.JOptionPane ; // user input public class SlotMachineTest { public static void main(String args[]) { System.out.println("@ Name (\u00A7U0#)") ; String text ; // Main variable that accepts user input text = "Let's Play with the Slot Machine Today!" ; // Sounds legit! // Inputting the proper introduction to the Slot Machine "window" JOptionPane.showMessageDialog(null, text) ; text = JOptionPane.showInputDialog("Enter The 1st Number") ; int slot1 = Integer.parseInt(text) ; // Value for slot one text = JOptionPane.showInputDialog("Enter The 2nd Number") ; int slot2 = Integer.parseInt(text) ; // Value for slot two text = JOptionPane.showInputDialog("Enter The 3rd Number") ; int slot3 = Integer.parseInt(text) ; // Value for slot three text = JOptionPane.showInputDialog("Enter the Wager & Spin!") ; int wager = Integer.parseInt(text) ; // Amount wagered to play // The object that utilizes the four initialized parameters below SlotMachine Game = new SlotMachine(slot1, slot2, slot3, wager) ; // The accessor method that collects all the data before

playing System.out.println(Game.getSlotsAndWager()) ; // This method enforces rules of the game and prints the receipt System.out.println(Game.playSlots()) ; } } // Input Data: // // A. 7 7 7, $ // B. 7 3 7, $ // C. 0 1 2, $ // D. 9 8 7, $ // E. 5 3 4, $ // F. 8 9 10, $ // G. 5 6 0, $ /* Output for A. @ Name (§U0#) Numbers Entered: 7, 7, & 7 Amount wagered: $ Congratulations! You have won the jackpot! You won: $100! Also, reclaim your wager of: $ BUILD SUCCESSFUL (total time: 0 seconds) / / Output for B. @ Name (§U0#) Numbers Entered: 7, 3, & 7 Amount wagered: $ Congratulations! You have won a prize! You won: $4! Also, reclaim your wager of: $ BUILD SUCCESSFUL (total time: 0 seconds) / / Output for C. @ Name (§U0#) Numbers Entered: 0, 1, & 2 Amount wagered: $ Congratulations! You have won a prize! You won: $20! Also, reclaim your wager of: $ Remember that consecutive numbers can be in any order, this is why I changed the ratio from 60 : 1 to 20 : 1 as instructed. BUILD SUCCESSFUL (total time: 0 seconds) / / Output for D.