Friday, June 27, 2014

Code Slot machine in Java


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);
    }
}


21 comments:

  1. A good blog always comes-up with new and exciting information and while reading I have feel that this blog is really have all those quality that qualify a blog to be a one.  파워볼엔트리

    ReplyDelete
  2. I want you to thank for your time of this wonderful read!!! I definitely enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog 서포터주소

    ReplyDelete
  3. A debt of gratitude is in order for the significant data and experiences you have so given here... 영앤리치 먹튀

    ReplyDelete
  4. I am undeniably benefiting as much as possible from your site. You undeniably have some unprecedented information and unimaginable stories. 윈윈 먹튀

    ReplyDelete
  5. The primary stage the arrangement should, hypothetically, be uninfluenced by the planned force and term of the sound which is hence created. Truth be told, be that as it may, so rapidly are the three stages achieved that the piano player once in a while has ability to think, in execution, of each stage independently. Situs Agen Taruhan Judi Bola

    ReplyDelete
  6. Hi, I enjoyed your wonderful site.
    slot

    ReplyDelete
  7. I must say, I thought this was a pretty interesting read when it comes to thisC topic. Liked the material. . .
    แทงบอลสเต็ป

    ReplyDelete
  8. Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!
    รีวิวเว็บพนัน

    ReplyDelete
  9. I am often to blogging and that i actually appreciate your posts. This article has truly peaks my interest. I am going to bookmark your internet site and keep checking for first time data
    รีวิวเว็บพนัน

    ReplyDelete
  10. I must say, I thought this was a pretty interesting read when it comes to thisC topic. Liked the material. . .
    ufa365

    ReplyDelete
  11. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    เว็บแทงบอลฝากถอนauto

    ReplyDelete
  12. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job! เว็บแทงบอลสเต็ปไม่มีขั้นต่ำ

    ReplyDelete
  13. Consequently, starting in the amount of electronic marketing strategy, is whenever a company's existing website, which will be to review the present site and its purpose is to improve the potency of the future. There is no evidence that the development and implementation of a method to be significantly different way of electronic marketing. Strategic planning for enterprise development or strategic marketing to comply with the established framework should still be
    เว็บแทงบอลออโต้

    ReplyDelete
  14. I am always searching online for articles that can help me. There is obviously a lot to know about this. I think you made some good points in Features also. Keep working, great job!
    UFA365

    ReplyDelete
  15. You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us to read this.
    ทำไมต้องเล่นบาคาร่า

    ReplyDelete
  16. I must say, I thought this was a pretty interesting read when it comes to thisC topic. Liked the material. . .
    แทงบาสเว็บไหนดี

    ReplyDelete
  17. Your post was very nicely written. I’ll be back in the future for sure!
    บาคาร่าsa-gaming

    ReplyDelete
  18. his is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.
    แทงบาสออนไลน์

    ReplyDelete
  19. his is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.
    lucky-god

    ReplyDelete
  20. It turns out that even the hottest port has a few places where you can get off the beaten path. Here are some recommendations that will make you feel like you're in the know
    สล็อตออนไลน์ufabet

    ReplyDelete