Home > Software design >  Why is my craps game (java) not running the code for the second rolls?
Why is my craps game (java) not running the code for the second rolls?

Time:10-15

I am very new to java and I am trying to create a craps game for class. I need to get the code to roll for the initial score, if it does not land on the rules to win or lose, then to reroll again until the player wins or loses. I can get it to perform the first loop and roll the initial score of die, but it won't reroll if it doesn't Win/Lose on the first roll. My code is:

public static void main(String[] args) {
    Random rnd = new Random ();
    int dice1 = rnd.nextInt (6)   1;
    int dice2 = rnd.nextInt (6)   1;
    int roll = dice1 dice2;
    int score = 0;
    System.out.println ("\n dice 1 = "   dice1   " dice 2 = "   dice2);
    

    System.out.println("Rolling dice....");
    int crapsRoll = roll;
    if(crapsRoll == 2 || crapsRoll ==3 || crapsRoll == 12) 
        System.out.println("Your score is " crapsRoll);
        System.out.println("You lose!");
    }
    if (crapsRoll ==7 || crapsRoll == 11) {
        System.out.println("Your score is "   crapsRoll);
        System.out.println("You win!");
    } else {
        score = crapsRoll;
        System.out.println("Your score = "   score);
        
        while (true) {
            
            int nRoll = dice1   dice2;
            score=nRoll;
            System.out.println("\n You rolled = "   score);
            if (score == crapsRoll) {
                System.out.println("you win!");
                break;
            }
            if (score > crapsRoll || score < crapsRoll) {
                System.out.println("You rolled = " score);
                System.out.println("Rolling again...");
                
            }
            if (score == 7) {
                System.out.println("You lose!");
                break;
            }
            
        }
        
    }

}

Running as is, I get
dice 1 = 3 dice 2 = 1 Rolling dice.... Your score = 4

You rolled = 4 you win!

It is supposed to say "rolling again", and give me new values. Sorry for poor formatting or if my issue is hard to understand. Any help would be appreciated!

CodePudding user response:

This is the statement that you break the flow, because: score == crapsRoll You need different logic for first and second if statements inside while loop

   if (score == crapsRoll) {
                    System.out.println("you win!");
                    break;
                }

CodePudding user response:

I believe I found four potential bugs:

1: I believe you are missing an opening bracket on your first conditional statement:

if(crapsRoll == 2 || crapsRoll ==3 || crapsRoll == 12)

2: crapsRoll and score variables are assigned the same value when you attempt to set the score for re-rolls, which is meeting a condition in your while loop and breaking the loop in the first iteration:

if (score == crapsRoll) {
            System.out.println("you win!");
            break;
 }

The score is set to the value of the first roll, which is correct. The problem is that the while loop then checks the first roll for win/lose conditions instead of re-rolling. The else statement will always break because the score and first roll will always be equal.

3: The program outputs to the terminal that it is re-rolling, but it never re-rolls the dice/ gets new random values. Your loop should keep re-rolling the dice each iteration.

4: I think there was a mix up or two in the while loop conditions where score was evaluated in placed where crapsRoll should be evaluated.

I believe replacing your else statement with this do-while loop should do the trick. The closing bracket at the bottom just closes the else statement, so make sure you have a method closing bracket and a class closing bracket below this:

else {
        
        // If first roll was not a win or lose, set the score:
        score = crapsRoll;
        System.out.println("Your score = "   score);
        

        // Do a re-roll, and continue playing/re-rolling until a win or lose condition occurs:
        do{
           

            System.out.println("Rolling again...");

            // Get new random values for each dice:
            dice1 = rnd.nextInt (6)   1;
            dice2 = rnd.nextInt (6)   1;

            crapsRoll = dice1   dice2;

            System.out.println("You rolled = "  crapsRoll);

            
        // If crapsRoll is not equal to 7 (lose) or score (win), continue loop:
        }while ((crapsRoll != 7) && (crapsRoll != score));

        // Output whether you win or lose:

        if(crapsRoll == score)
            System.out.println("You win!");
        else
            System.out.println("You lose!");
        
    }

CodePudding user response:

You're not rolling the dices inside the loop, so the previous values are reused.

To resolve the issue, add the statements generating the random values into the loop before calculating nRoll:

while (true) {
    int dice1 = rnd.nextInt (6)   1;
    int dice2 = rnd.nextInt (6)   1;
    int nRoll = dice1   dice2;
    ...

Also, you can probably handle the logic with a bit fewer variables. For instance, at least one of these three is redundant: score, crapsRoll, nRoll.

  •  Tags:  
  • java
  • Related