lundi 22 avril 2019

Advice on multi-conditional if statement

This is what im trying to accomplish with my code for a slot machine :

Each reel can display an ORANGE, or a CHERRY, or a WATERMELON or a QUESTION MARK. The machine pays off 500 dollars if ALL the reels display the same image: 3 oranges, 3 cherries, 3 watermelons, or 3 question marks. The machine pays off 10 tokens if one and only one real displays a Question Mark. The machine pays off 100 tokens if the reels display 1 Orange, 1 Cherry and 1 Watermelon.

I've made an absurd if/else if chain to start to solve it, but its getting out of hand and I feel like there is a better way of solving this. Here is what I have thus far, keeping in mind I stopped before the 100 token payout part of the code. My question is: Is there a better way to solve this problem, aside from the route i've taken in my code?

public class SMB extends SlotMachine {
    private static final int ORANGE = 1;
    private static final int CHERRY = 2;
    private static final int WATERMELON = 3;
    private static final int QUESTIONMARK = 4;


    public static void run() {
        num1 = getRandomIntRange(1, 4);
        num2 = getRandomIntRange(1, 4);
        num3 = getRandomIntRange(1, 4);


        if ((num1 == ORANGE) && (num2 == ORANGE) && (num3 == ORANGE)) {
            TOKENS = TOKENS + 500;
            System.out.println("SMB paid 500 - Output:(ORANGE,ORANGE,ORANGE) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 == CHERRY) && (num2 == CHERRY) && (num3 == CHERRY)) {
            TOKENS = TOKENS + 500;
            System.out.println(count + " SMB paid 500 - Output:(CHERRY,CHERRY,CHERRY) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 == WATERMELON) && (num2 == WATERMELON) && (num3 == WATERMELON)) {
            TOKENS = TOKENS + 500;
            System.out.println(count + " SMB paid 500 - Output:(WATERMELON,WATERMELON,WATERMELON) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 == QUESTIONMARK) && (num2 == QUESTIONMARK) && (num3 == QUESTIONMARK)) {
            TOKENS = TOKENS + 500;
            System.out.println(count + " SMB paid 500 - Output:(QUESTION MARK,QUESTION MARK, QUESTION MARK) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 == QUESTIONMARK) && (num2 != QUESTIONMARK) && (num3 != QUESTIONMARK)) {
            TOKENS = TOKENS + 10;
            System.out.println(count + " SMB paid 10 - Output:(QUESTION MARK,x,x) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 != QUESTIONMARK) && (num2 == QUESTIONMARK) && (num3 != QUESTIONMARK)) {
            TOKENS = TOKENS + 10;
            System.out.println(count + " SMB paid 10 - Output:(x,QUESTION MARK,x) - Current Token count: " + TOKENS);
            count++;
        } else if ((num1 != QUESTIONMARK) && (num2 != QUESTIONMARK) && (num3 == QUESTIONMARK)) {
            TOKENS = TOKENS + 10;
            System.out.println(count + " SMB paid 10 - Output:(QUESTION MARK,x,x) - Current Token count: " + TOKENS);
            count++;


        }
    }//end SMB

Aucun commentaire:

Enregistrer un commentaire