patternjavaMinor
Slot Machine Simulator
Viewed 0 times
machinesimulatorslot
Problem
I'm studying alone, so I don't know if I'm writing efficiently. Could this slot machine simulator be improved? How?
Here are the prize rules:
import acm.program.*;
import acm.util.*;
/*
* THIS PROGRAM SIMULATES THE SLOT MACHINE
*/
public class cap6ex5 extends ConsoleProgram{
private RandomGenerator rgen = RandomGenerator.getInstance();
private int money = 50; //starting stake
private String value = "";
public void run(){
String instAnswer = readLine("Would you like instructions?");
while (!instAnswer.equals("yes") && !instAnswer.equals("no")) {
instAnswer = readLine("Answer yes or no.");
}
if (instAnswer.equals("yes")) println("YOU SHOUD KNOW!");
startGame(money);
}
private void startGame(int x){
String keepPlaying = "";
int finalMoney = x;
int prize = 0;
while (finalMoney > 0){
keepPlaying = readLine("You have $" + finalMoney +". Would you like to play?");
//avoid different answers
while(true){
if (keepPlaying.equals("yes") || keepPlaying.equals("no")) break;
keepPlaying = readLine("Asnwer yes or no.");
}
if (keepPlaying.equals("no")) break;
finalMoney--;
prize = gamePrize();
finalMoney += prize;
println(value + " -- You win $" + prize);
}
println("Okay, bye. You ended with $" + finalMoney);
}
private int gamePrize(){
int countBAR = 0; //BAR == 1
int countBELL = 0; //BELL == 2
int countPLUM = 0; //PLUM == 3
int countORANGE = 0;//ORANGE == 4
int countCHERRY = 0;//CHERRY == 5
value = "";
for (int i = 1; i 1 && bar ==1) || bell == 3){
prize = 20;
} else if ((plum > 1 && bar == 1) || plum == 3){
prize = 14;
} else if ((orange >1 && bar == 1) || orange == 3){
prize = 10;
} else switch(cherry){
case 1: prize = 7; break;
case 2: prize = 5; break;
case 3: prize = 2; break;
}
return prize;
}
}Here are the prize rules:
Solution
- When comparing against
"yes"or"no", you should consider usingequalsIgnoreCaseso that answers likeYEScan be accepted. (Perhaps the player is excited to play?)
startGame: I cannot tell what thexargument is. It should be renamed.
gamePrize: Rather than hard-coding the 1-6 figures in yourswitchstatement, you should make each one a constant (ex.VAL_ORANGE).
result: I would not know that you arguments are the number of occurrences of each type. These should probably be renamed.
result: You currently have 1 cherry as winning $7 and 3 as winning $2. This appears to be a bug.
Context
StackExchange Code Review Q#147475, answer score: 3
Revisions (0)
No revisions yet.