In: Computer Science
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
//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());
}
}