Created
January 3, 2014 10:08
-
-
Save desrtfx/8235711 to your computer and use it in GitHub Desktop.
Java Applet simple Slot Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class is used to create a lottery program using 3 symbols | |
* (as names CHERRY, LEMON, ORANGE, PLUM, BELL, and BAR) | |
* to represent a block. A block (BanditBlock) is an object, and any methods are | |
* to change the value of the block, or to get values from the block. | |
* | |
* Note: In hindsight, it would be much less confusing if symbols were called slots. | |
* | |
* -Winning block's with integer values, symbol names, and winning amounts- | |
* | |
* 0, #, # CHERRY, -----, ----- $2 | |
* 0, 0, # CHERRY, CHERRY, ----- $5 | |
* 0, 0, 0 CHERRY, CHERRY, CHERRY $7 | |
* | |
* 2, 2, 2/5 ORANGE, ORANGE, ORANGE/BAR $10 | |
* | |
* 3, 3, 3/5 PLUM, PLUM, PLUM/BAR $14 | |
* | |
* 4, 4, 4/5 BELL, BELL, BELL/BAR $20 | |
* | |
* 5, 5, 5 BAR, BAR, BAR $250 | |
* */ | |
import acm.util.RandomGenerator; | |
public class BanditBlock { | |
//Symbols can be any value from 0-5, blockName is probably not | |
// even necessary for it's use in the program. | |
public BanditBlock() { | |
blockName = ""; | |
symbolOne = 0; | |
symbolTwo = 0; | |
symbolThree = 0; | |
} | |
//Gives a block(BanditBlock) 3 new random values between 0 and 5 | |
// to simulate a spin, or the insert of a dollar. | |
public void nextBlock() { | |
symbolOne = rgen.nextInt(0, 5); | |
symbolTwo = rgen.nextInt(0, 5); | |
symbolThree = rgen.nextInt(0, 5); | |
} | |
//Returns the names that are assigned to the 3 values of | |
// of the block. | |
public String getBlockName() { | |
blockName = | |
switchSymbol(symbolOne) + " " + | |
switchSymbol(symbolTwo) + " " + | |
switchSymbol(symbolThree); | |
return blockName; | |
} | |
//Takes the integer value of a symbol, between 0 and 5, | |
// and returns a name corresponding to it's value. | |
private String switchSymbol(int symbol) { | |
String symbolName = ""; | |
switch (symbol) { | |
case 0: symbolName = "CHERRY"; | |
break; | |
case 1: symbolName = "LEMON"; | |
break; | |
case 2: symbolName = "ORANGE"; | |
break; | |
case 3: symbolName = "PLUM"; | |
break; | |
case 4: symbolName = "BELL"; | |
break; | |
case 5: symbolName = "BAR"; | |
break; | |
} | |
return symbolName; | |
} | |
//Returns the value, if any, that was won from the 3 integers, | |
// symbols, that is dependant on their symbol name, and order. | |
public int getWinnings() { | |
int money = 0; | |
//case 1: is skipped because you cannot win anything on a lemon. | |
switch(symbolOne) { | |
//case 0: First slot CHERRY | |
case 0: if(symbolTwo != 0) { | |
money = 2; | |
//goes to 'else' if second slot is CHERRY | |
} else { | |
if(symbolThree != 0) { | |
money = 5; | |
//goes to second 'else' if third is CHERRY | |
} else { | |
money = 7; | |
}} | |
break; | |
//case 2: First 2 slots ORANGEs, third slot ORANGE, or BAR. | |
case 2: if(symbolTwo == 2 && (symbolThree == 2 || symbolThree == 5)) { | |
money = 10; | |
} | |
break; | |
//case 3: First 2 slots PLUMs, third slot PLUM, or BAR. | |
case 3: if(symbolTwo == 3 && (symbolThree == 3 || symbolThree == 5)) { | |
money = 14; | |
} | |
break; | |
//case 4: First 2 slots BELLs, third slot BELL, or BAR. | |
case 4: if(symbolTwo == 4 && (symbolThree == 4 || symbolThree == 5)) { | |
money = 20; | |
} | |
//case 5: All slots BAR. | |
case 5: if(symbolTwo == 5 && symbolThree == 5) { | |
money = 250; | |
} | |
} | |
//Returns how much the block is worth. | |
return money; | |
} | |
//The first slot of a BanditBlock | |
private int symbolOne; | |
//The second slot of a BanditBlock | |
private int symbolTwo; | |
//The third slot of a BanditBlock | |
private int symbolThree; | |
//The 3 symbolNames of a block. | |
private String blockName; | |
//Creates an instance of a random number frorm the ACM package. | |
private RandomGenerator rgen = RandomGenerator.getInstance(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import acm.program.ConsoleProgram; | |
/** | |
* This class extends BanditBlock, and is used to simulate a lottery from a 'One | |
* Armed Bandit' hence the name 'BanditBlock' | |
*/ | |
public class RunLotto extends ConsoleProgram { | |
// Creates BanditBlock() | |
BanditBlock block = new BanditBlock(); | |
private int money = 50; | |
public void run() { | |
// This is a code to be later added upon, for now it's | |
// just here. | |
println("Would you like to know how to play?"); | |
// For now it is just an infinite loop. | |
while (money > 0) { | |
// The value returned does not matter, it is just | |
// to pause the program. | |
readLine("Presee enter to insert your dollar!"); | |
// A dollar is removed to simulate the insert of one. | |
money--; | |
// Calls nextBlock() which gives 3 new integer | |
// values to the symbols of block | |
block.nextBlock(); | |
// Prints out the 3 symbol names corresponding to | |
// the 3 random integers, using getBlockName(), | |
// which is where our annoying bug is, and hence | |
// the 1. | |
println(block.getBlockName()); | |
// Returns the winnings of the three symbols(or slots) | |
// that is dependent on there identity. | |
money += block.getWinnings(); | |
// Informs you that you won, and how much. | |
if (block.getWinnings() != 0) { | |
println("You win " + block.getWinnings() + "!"); | |
} | |
// Informs you your broke, because you may | |
// never stop using the machine until | |
// it has all your money! | |
println("You have $" + money); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment