lundi 7 mai 2018

Java: if statement not executed

I am trying to get a better understanding of Java loops by implementing the following Solitaire Dice game:

  • The user will start with a pot of money of $50.

  • In each play of the game, the player will:

1- Enter a valid bet amount (less than or equal to the current pot, not negative, a bet of 0 means quit the game).

2- The game will then remove the bet amount from the pot

3- User then roll three dices and display the values and add these values to a total.

4- Then, the game should adjust the pot in the following way:

  • If the total of the three dice is greater than 12, then the player wins their bet back.

  • If two of the three die have the same value, then the player wins double their bet back.

  • If all three dice have the same value, then the player wins triple their bet back.

  • Otherwise, the player has lost the bet money.

I have written the following code:

public class Game {

Scanner input = new Scanner(System.in);

int potAmount;
int betAmount;

public Game(int potAmount, int betAmount) {
    this.potAmount = potAmount;
    this.betAmount = betAmount;
}

public Game() {
    potAmount = 50;
}

public void getBetAmountFromUser() {
    //Get Amount of Bet
    int x = 0;
    System.out.println("Your current pot is: " + potAmount);
    System.out.println("Enter your bet amount: ");
    x = input.nextInt();
    //Check of Bet larger than 0 and less that pot amount 
    //        while (!(x > 0) && !(x <= potAmount)){
    //            System.out.println("Error - bet amount must be larger than 0 and less than pot amount: ");
    //            x = input.nextInt();
    //        }

    if (x > 0 && x <= potAmount) {
        betAmount = x;
        potAmount = potAmount - betAmount;
        System.out.println("Pot Amount: " + potAmount);
    } else if (x == 0) {
        System.out.println("You end the game with pot " + potAmount);
    } 

}

public void playGame() {

    Die die1 = new Die();
    Die die2 = new Die();
    Die die3 = new Die();

    do{
        getBetAmountFromUser();
        System.out.println("Your current pot is: " + potAmount);
        //Get Bet

        //Roll die
        die1.rollDie();
        die2.rollDie();
        die3.rollDie();
        die1.displayDie();
        die2.displayDie();
        die3.displayDie();
        int dieSum = die1.getDieValue() + die2.getDieValue() + die3.getDieValue();

        //Check die >= 12
        if(dieSum >= 12){
            //Double
            if(die1.getDieValue() == die2.getDieValue()
                || die1.getDieValue() == die3.getDieValue()
                || die2.getDieValue() == die3.getDieValue()){
                potAmount = (betAmount*2)+potAmount;
                System.out.println("You won double the bet");
            }
            //Triple
            else if(die1.getDieValue() == die2.getDieValue()
                && die1.getDieValue() == die3.getDieValue()
                && die2.getDieValue() == die3.getDieValue()){
                potAmount = (betAmount*3)+potAmount;
                System.out.println("You won triple the bet");
            }
            //Win same amount back
            else{
                potAmount = potAmount+betAmount;
                System.out.println("You won your bet back");
            }
        }
        //Check die < 12
        else if(dieSum < 12){
                potAmount = potAmount-betAmount;    
        }
    } while(potAmount !=0);

    //when pot is 0 user lose the game
    if(potAmount<=0){
        System.out.println("You lost the game");
    }

}



}

When I run the playGame() function from the Main class. I get asked to enter the bet, get the dice values then get asked again to enter the bet without going throw the following part:

//Check die >= 12
        if(dieSum >= 12){
            //Double
            if(die1.getDieValue() == die2.getDieValue()
                || die1.getDieValue() == die3.getDieValue()
                || die2.getDieValue() == die3.getDieValue()){
                potAmount = (betAmount*2)+potAmount;
                System.out.println("You won double the bet");
            }
            //Triple
            else if(die1.getDieValue() == die2.getDieValue()
                && die1.getDieValue() == die3.getDieValue()
                && die2.getDieValue() == die3.getDieValue()){
                potAmount = (betAmount*3)+potAmount;
                System.out.println("You won triple the bet");
            }
            //Win same amount back
            else{
                potAmount = potAmount+betAmount;
                System.out.println("You won your bet back");
            }
        }
        //Check die < 12
        else if(dieSum < 12){
                potAmount = potAmount-betAmount;    
        }

using while instead of if would result in an infinite loop and when using Break; the exucution stops. How can I solve the problem?

Aucun commentaire:

Enregistrer un commentaire