Its been a busy week so far. I went out with a few few friends for some fun this weekend. Found a slot machine and thought of doing it in Java.
So here I present to you a simple Slot Machine in Java 8.
package Casino; import java.lang.Override; import java.lang.String; import java.util.*; import java.lang.Math; public class SlotMachine { public static Scanner input; public static final String BAR = "BAR"; public static final String CHERRIES = "CHERRIES"; public static final String SPACE = "SPACE"; public static final String SEVEN = "7"; // main method public static void main(String[] args) { input = new Scanner(System.in); // getting user bet int bettingAmount; do { bettingAmount = getBet(); // validating input bet if (bettingAmount < 0 || bettingAmount > 100) continue; if (bettingAmount == 0) break; // player pulls the slot machine TripleString tripleString = pull(); // get multiplier from the slot pull int multiplier = getPayMultiplier(tripleString); // getting winnings (or nothing) int winnings = bettingAmount * multiplier; // showing winnings (or nothing) display(tripleString, winnings); } while (bettingAmount != 0); input.close(); } // get bet method public static int getBet() { int inputBet; String instructions = "How much would you like to bet (1 - 100) or 0 to quit?"; System.out.println(instructions); inputBet = input.nextInt(); return inputBet; } public static String randString() { double randomValue; randomValue = Math.random() * 100; int randomIntValue; randomIntValue = (int) randomValue; // all even numbers between 1 and 100 will return bar (50%) if (randomIntValue % 2 == 0) return Bar; // all odd numbers between 1 and 50 will return cherries (25%) if (randomIntValue >= 1 && randomIntValue < 50) return Cherries; // all odd numbers between 51 and 75 will return space (12.5%) if (randomIntValue >= 51 && randomIntValue < 74) return Space; // all odd numbers between 76 and 100 will return 7 (12.5%) if (randomIntValue >= 75 && randomIntValue < 100) return Seven; return ""; } // pull method public static TripleString pull() { // creating object from default constructor TripleString thePull = new TripleString(); // using mutators to change string values thePull.setString1(randString()); thePull.setString2(randString()); thePull.setString3(randString()); // returning triple string return thePull; } // returning amount won (0, 5, 15, 30, 50 or 100). public static int getPayMultiplier(TripleString thePull) { if (thePull.getString1() == Cherries) if (thePull.getString2() == Cherries) if (thePull.getString3() == Cherries) return 30; else return 15; else return 5; if (thePull.getString1() == Bar && thePull.getString2() == Bar && thePull.getString3() == Bar) return 50; if (thePull.getString1() == Seven && thePull.getString2() == Seven && thePull.getString3() == Seven) return 100; return 0; } // displaying if user won or lost public static void display(TripleString thePull, int winnings) { System.out.println("Whirrrr.......and your pull is"); System.out.println(thePull); if (winnings > 0) System.out.println("Congratulations, you win: " + winnings); else System.out.println("Sorry, you lose. Better luck next time."); } } class TripleString { public static final String BAR = "BAR"; public static final String CHERRIES = "CHERRIES"; public static final String SPACE = "SPACE"; public static final String SEVEN = "7"; // member data private String string1 = "", string2 = "", string3 = ""; // static constant public static final int MAX_LEN = 20; // private helper method private boolean validString(String str) { if (str != null && str.length() <= MAX_LEN) return true; else return false; } // accessor method for string 1 public String getString1() { return string1; } // accessor method for string 2 public String getString2() { return string2; } // accessor method for string 3 public String getString3() { return string3; } // mutator method for string 1 public boolean setString1(String string1) { if (validString(string1)) { this.string1 = string1; return true; } else return false; } // mutator method for string 2 public boolean setString2(String string2) { if (validString(string2)) { this.string2 = string2; return true; } else return false; } // mutator method for string 3 public boolean setString3(String string3) { if (validString(string3)) { this.string3 = string3; return true; } else return false; } @Override public String toString() { return " " + ((string1.contains(Space)) ? "(" + string1 + ")" : string1) + " " + ((string2.contains(Space)) ? "(" + string2 + ")" : string2) + " " + ((string3.contains(Space)) ? "(" + string3 + ")" : string3); } }