Question

In: Computer Science

In this assignment, you will be implementing a slot machine that you find in casinos. A...

In this assignment, you will be implementing a slot machine that you find in casinos. A slot machine has certain number of reels which spin to produce one of a fixed set of symbols (e.g. flowers, bells) randomly when the user pulls a lever. The user needs to insert certain number of currency units as wager into the slot machine before pulling the lever. We refer to these currency units as “wagerUnitValue”. For example in slot machines with quarter (25 cents) as wagerUnitValue , user needs to insert one or more quarters while in dollar slot machines where wagerUnitValue = 100, user needs to insert one or more dollars as wager before pulling the lever. The user will receive a payout based on the matching of symbols on the reels. In a real slot machine there is a payout table which specifies the payout for certain combinations of symbols on the reels. In this assignment, you will instead be implementing simple payout rules based on number of matched symbols.

I have provided a skeletal implementation of a slot machine to help you get started (Check Moodle). Do not change class name, enum type or function signatures. You can however add some variables as needed. Specifically implement the following functions: (a) SlotMachine class constructor which has the following parameters: (i) numReels, (ii) odds array with one entry per symbol indicating probability of getting the symbol in a reel and (iii) wagerUnitValue in cents. (b) getSymbolForAReel() – use Math.random() to generate a random number between 0 and 1 and then use the odds array to generate a symbol randomly. (c) calcPayout() – calculate and return payout value for the given symbols on the reels; use the rules provided in the comments section of the function (d) pullLever() – this function simulates user pulling lever in the slot machine after inserting a wager. First use the function in (b), to generate symbol for each reel and then use the function in (c) to calculate the payout. You can assume that the symbols on the reels appear as independent random events. You need to print the reel symbols in a line followed by the payout in dollar format in another line. (e) getPayoutPercent() – you need to keep track of total wager given by the user as well as total payout provided to the user; this function calculates the total payout as a percent of the total wager value. (f) reset() – clears the total wager and total payout value for fresh calculation of payout percent. Submit only the SlotMachine.java file with your name added in comments section. You can use the main() function to test your program but it is not graded. The test program used for grading will call the class functions (a)-(f) directly.

package edu.stevens.cs570.assignments;

public class SlotMachine {

public enum Symbol {

BELLS("Bells", 10), FLOWERS("Flowers", 5), FRUITS("Fruits", 3),

HEARTS("Hearts", 2), SPADES("Spades", 1);

// symbol name

private final String name;

// payout factor (i.e. multiple of wager) when matching symbols of this

type

private final int payoutFactor;

Symbol(String name, int payoutFactor) {

this.name = name;

this.payoutFactor = payoutFactor;

}

public String getName() {

return name;

}

public int getPayoutFactor() {

return payoutFactor;

}

}

/**

* Constructor

* @param numReels number of reels in slot machine

* @param odds odds for each symbol in a reel, indexed by its enum ordinal

value; odds value is non-zero and sums to 1

* @param wagerUnitValue unit value in cents of a wager

*/

public SlotMachine(int numReels, double [] odds, int wagerUnitValue) {

}

/**

* Get symbol for a reel when the user pulls slot machine lever

* @return symbol type based on odds (use Math.random())

*/

public Symbol getSymbolForAReel() {

return null;

}

/**

* Calculate the payout for reel symbols based on the following rules:

* 1. If more than half but not all of the reels have the same symbol then

payout factor is same as payout factor of the symbol

* 2. If all of the reels have the same symbol then payout factor is twice the

payout factor of the symbol

* 3. Otherwise payout factor is 0

* Payout is then calculated as wagerValue multiplied by payout factor

* @param reelSymbols array of symbols one for each reel

* @param wagerValue value of wager given by the user

* @return calculated payout

*/

public long calcPayout(Symbol[] reelSymbols, int wagerValue) {

return 0;

}

/**

* Called when the user pulls the lever after putting wager tokens

* 1. Get symbols for the reels using getSymbolForAReel()

* 2. Calculate payout using calcPayout()

* 3. Display the symbols, e.g. Bells Flowers Flowers..

* 4. Display the payout in dollars and cents e.g. $2.50

* 5. Keep track of total payout and total receipts from wagers

* @param numWagerUnits number of wager units given by the user

*/

public void pullLever(int numWagerUnits) {

}

/**

* Get total payout to the user as percent of total wager value

* @return e.g. 85.5

*/

public double getPayoutPercent() {

return 0;

}

/**

* Clear the total payout and wager value

*/

public void reset() {

}

public static void main(String [] args) {

double [] odds = new double[Symbol.values().length];

// sum of odds array values must equal 1.0

odds[Symbol.HEARTS.ordinal()] = 0.3;

odds[Symbol.SPADES.ordinal()] = 0.25;

odds[Symbol.BELLS.ordinal()] = 0.05;

odds[Symbol.FLOWERS.ordinal()] = 0.2;

odds[Symbol.FRUITS.ordinal()] = 0.2;

SlotMachine sm = new SlotMachine(3, odds, 25); // quarter slot machine

sm.pullLever(2);

sm.pullLever(1);

sm.pullLever(3);

System.out.println("Pay out percent to user = " + sm.getPayoutPercent());

sm.reset();

sm.pullLever(4);

sm.pullLever(1);

sm.pullLever(1);

sm.pullLever(2);

System.out.println("Pay out percent to user = " + sm.getPayoutPercent());

}

}

This is Sample Output:

Spades Hearts Flowers

payout=$0.00

Flowers Hearts Hearts

payout=$0.50

Flowers Spades Spades

payout=$0.75

Pay out percent to user = 83.33333333333333

Spades Flowers Hearts

payout=$0.00

Spades Hearts Flowers

payout=$0.00

Fruits Hearts Hearts

payout=$0.50

Hearts Spades Fruits

payout=$0.00

Pay out percent to user = 25.0

Solutions

Expert Solution

//complete code and have executed sucessfully

import java.util.*;
import java.util.Random;

public class SlotMachine {

public enum Symbol {

BELLS("Bells", 10), FLOWERS("Flowers", 5), FRUITS("Fruits", 3),

HEARTS("Hearts", 2), SPADES("Spades", 1);

// symbol name

private final String name;

// payout factor (i.e. multiple of wager) when matching symbols of thistype

private final int payoutFactor;

Symbol(String name, int payoutFactor) {

this.name = name;

this.payoutFactor = payoutFactor;

}

public String getName() {

return name;

}

public int getPayoutFactor() {

return payoutFactor;

}

}

/**

* Constructor

* @param numReels number of reels in slot machine

* @param odds odds for each symbol in a reel, indexed by its enum ordinal

value; odds value is non-zero and sums to 1

* @param wagerUnitValue unit value in cents of a wager

*/
public int k;
public int twager;
public double tpay;
public double[] odds = new double[Symbol.values().length];
public SlotMachine(int numReels, double [] odds, int wagerUnitValue) {
this.k = wagerUnitValue;
this.odds = odds;
}

/**

* Get symbol for a reel when the user pulls slot machine lever

* @return symbol type based on odds (use Math.random())

*/

/*here random number is calculated directly based on the probability values and actually it can be calculated by taking odds and multiplying and adding the integer and i have directly return the values directly due some constraints*/

public Symbol getSymbolForAReel() {
Random r = new Random();
int number = r.nextInt(100)+1;
//System.out.println(number);
if(number >=0)&&(number<=5)
return Symbol.BELLS;
else if((number >=5 )&&(number<=25))
return Symbol.FLOWERS;
else if((number >=25 )&&(number<=45))
return Symbol.FRUITS;
else if((number >=45 )&&(number<=75))
return Symbol.HEARTS;
else
return Symbol.SPADES;
}

/**

* Calculate the payout for reel symbols based on the following rules:

* 1. If more than half but not all of the reels have the same symbol then

payout factor is same as payout factor of the symbol

* 2. If all of the reels have the same symbol then payout factor is twice the

payout factor of the symbol

* 3. Otherwise payout factor is 0

* Payout is then calculated as wagerValue multiplied by payout factor

* @param reelSymbols array of symbols one for each reel

* @param wagerValue value of wager given by the user

* @return calculated payout

*/

public long calcPayout(Symbol[] reelSymbols, int wagerValue) {
int p = this.k;
if((reelSymbols[0]==reelSymbols[1])&&(reelSymbols[1]==reelSymbols[2]))
{
long cal = reelSymbols[1].payoutFactor*2*wagerValue*p;
return cal;   
}
else if(reelSymbols[0]==reelSymbols[1])
{
long cal = reelSymbols[1].payoutFactor*wagerValue*p;
return cal;
}
else if(reelSymbols[1]==reelSymbols[2])
{
long cal = reelSymbols[1].payoutFactor*wagerValue*p;
return cal;
}
else if(reelSymbols[0]==reelSymbols[2])
{
long cal = reelSymbols[2].payoutFactor*wagerValue*p;
return cal;
}
else
return 0;

}

/**

* Called when the user pulls the lever after putting wager tokens

* 1. Get symbols for the reels using getSymbolForAReel()

* 2. Calculate payout using calcPayout()

* 3. Display the symbols, e.g. Bells Flowers Flowers..

* 4. Display the payout in dollars and cents e.g. $2.50

* 5. Keep track of total payout and total receipts from wagers

* @param numWagerUnits number of wager units given by the user

*/

public void pullLever(int numWagerUnits) {
Symbol s1 =getSymbolForAReel();
System.out.print(s1 + " ");
Symbol s2 =getSymbolForAReel();
System.out.print(s2 + " ");
Symbol s3 =getSymbolForAReel();
System.out.print(s3 + " ");
System.out.println();
Symbol[] sys = new Symbol[3];
sys[0] = s1;
sys[1] = s2;
sys[2] = s3;
long t = calcPayout(sys,numWagerUnits);
double tt = (double)t/100;
System.out.println("payout=$" + tt);
this.tpay = this.tpay + t;
this.twager = this.twager + numWagerUnits;
}

/**

* Get total payout to the user as percent of total wager value

* @return e.g. 85.5

*/

public double getPayoutPercent() {
int p = this.k;
double payper = this.tpay*100 /(double)(this.twager*p);
return payper;

}

/**

* Clear the total payout and wager value

*/

public void reset() {
this.twager = 0;
this.tpay = 0;
}

public static void main(String [] args) {

double [] odds = new double[Symbol.values().length];

// sum of odds array values must equal 1.0

odds[Symbol.HEARTS.ordinal()] = 0.3;

odds[Symbol.SPADES.ordinal()] = 0.25;

odds[Symbol.BELLS.ordinal()] = 0.05;

odds[Symbol.FLOWERS.ordinal()] = 0.2;

odds[Symbol.FRUITS.ordinal()] = 0.2;

SlotMachine sm = new SlotMachine(3, odds, 25); // quarter slot machine

sm.pullLever(2);

sm.pullLever(1);

sm.pullLever(3);

System.out.println("Pay out percent to user = " + sm.getPayoutPercent());

sm.reset();

sm.pullLever(4);

sm.pullLever(1);

sm.pullLever(1);

sm.pullLever(2);

System.out.println("Pay out percent to user = " + sm.getPayoutPercent());

}

}


Related Solutions

Please read the case study entitled “Casinos and Crime” that you find in the reading assignment....
Please read the case study entitled “Casinos and Crime” that you find in the reading assignment. Based on what you have learned in this unit, answer the following questions: 1. In most casino states and counties, laws protect owners from liability claims arising from problems caused by gambling. In ethical terms, however, if you’re the sole proprietor of the casino, do you feel any responsibility for this episode? Why or why not? If you feel any responsibility, to whom would...
1. Slot machines are the favorite game at casinos throughout the U.S. The following sample data...
1. Slot machines are the favorite game at casinos throughout the U.S. The following sample data show the number of women and number of men who selected slot machines as their favorite game. Women Men Sample size 320 250 Favorite game-slots 256 165 Test at a 5% level of significance whether the difference between the proportion of women and proportion of men who say slots is their favorite game is statistically significant. (Please use both confidence interval approach and standard...
In this assignment you will be implementing a function that will find all single-word anagrams given...
In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter....
Slot machines are the favorite game at casinos throughout the United States (Harrah’s Survey 2002: Profile...
Slot machines are the favorite game at casinos throughout the United States (Harrah’s Survey 2002: Profile of the American Gambler). A local casino wants to estimate the difference in the percent of women and me who prefer the slots with a 95% level of confidence. Random samples of 320 women and 250 men found that 256 women prefer slots and 165 men prefer slots. 1- -Hypothesis test for one population mean (unknown population standard deviation) 2-Confidence interval estimate for one...
If the probability of winning a slot machine is 5% and you are going to play 500 pulls.
If the probability of winning a slot machine is 5% and you are going to play 500 pulls. Using a normal approximation. What’s the probability that you win less than 40? What’s the probability that you win 30?  
Suppose you go to a local casino to gamble using a slot machine. The following probability...
Suppose you go to a local casino to gamble using a slot machine. The following probability distribution characterizes your net earnings on a single play. Assume each play is independent of the other and every play has this same distribution for earnings. Net earnings ($) Probability -1.00 0.90 -0.50 0.07 5.00 0.0285 500.00 0.0015 You play this slot machine one hundred times. Assuming the central limit theorem, what is the chance that you will come out of the casino with...
A gambler begins playing a slot machine with $10 in quarters in her coin bucket.
A gambler begins playing a slot machine with $10 in quarters in her coin bucket. She plays 15 quarters before winning a jackpot of 50 quarters. She then plays 20 more quarters in the same machine before walking away. How many quarters does she now have in her coin bucket?
Java - Create a program that simulates a slot machine. When the program runs, it should...
Java - Create a program that simulates a slot machine. When the program runs, it should do the following: - Ask the user to enter the amount of money he or she wants to enter into the slot machine. - Instead of displaying images, have the program randomly select a word from the following list: Cherries, Oranges, Plums, Bells, Melons, Bars (To select a word, the program can generate a random number in the range of 0 through 5. If...
A slot machine have an enterprise of 5$, it has four slots that generates numbers between...
A slot machine have an enterprise of 5$, it has four slots that generates numbers between number 1 and 5. If all the slots generates 5, you win 100$. If three of the slots generates 4, you win 20$. If two of the slots generates 3, or 2, you win 2$. Otherwise, you win x$. What is the smallest number that boss can set in order to profit on average?
A slot machine has 5 reels each with 9 symbols one of which is a Jackpot....
A slot machine has 5 reels each with 9 symbols one of which is a Jackpot. How many different outcomes will occur after one spin? What is the possibility of atleast one jackpot? What is the possibility of no jackpots? What is the possibility of 3 jackpots?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT