0

I wrote a simple code for a craps game. I'm still figuring out how to work out the looping glitches, but it is giving me a java.lang.StackOverflowError at line 33:
//display the value of dice System.out.println("You rolled " + die + "+ " + die2 + ". Totalling " + dice);.

Here is the code:

 /**
 * A simulation of the craps betting game.
 * 
 * @
 * @
 */
import java.util.Random;



public class Craps
{
    //random number generator
    private Random randomNumber = new Random();

    //roll the dice
    public int rollDice()
    {
        //first die
        int die = (randomNumber.nextInt(6) + 1);

        //second die
        int die2 = (randomNumber.nextInt(6) + 1);

        //add die + die2
        int dice = die + die2;

        //point value
        int myPoint = 0;

        //display the value of dice
        System.out.println("You rolled " + die + "+ " + die2 + ". Totalling " + dice);

            if (dice == 7) {
                System.out.println ("Congratulations, YOU WIN!");
        }
        if (dice == 11) { 
            System.out.println ("Congratulations, YOU WIN!");
        }

            if (dice == 2) {
                System.out.println ("I'm sorry, you lose.");
    }
    if (dice == 3) {
        System.out.println ("I'm sorry, you lose.");
    }
    if (dice == 12) {
        System.out.println ("I'm sorry, you lose.");
    }

        else {
            System.out.println ("And the game continues...");
            myPoint = dice;
            if (dice == 7) {
                System.out.println ("I'm sorry, you lose.");
            }

        while (dice != 7)
        {
         rollDice();

            if (dice == myPoint) {
                System.out.println ("Congratulations, YOU WIN!");
            }
            else {
                myPoint = dice;
            }
        }
    }
    return dice;
}

}

3 Answers3

1

Because you're growing the stack frame when you call rollDice() recursively. You eventually run out of space to continue. You should return an int from your method, and use a loop to call it (perhaps infinitely).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You are not properly returning from rollDice function, You keep calling it recursively. You need a proper exit condition for the method.

smk
  • 5,340
  • 5
  • 27
  • 41
0

Try this...

 while (dice != 7)
    {
      dice  = rollDice();
      // ...
    }

The problem in your code is, because dice is a local variable of rollDice(), recursive call to rollDice() does not change the value of dice in the current function call. Therefore, the while loop never exits. So, recursive calls to the function pile up and fills your stack.

This error message simply means your stack memory is full. Java limits the size of the stack memory to a certain maximum number of bytes. When recursive function calls pile up and fills it, at some point, there will not be enough memory available on the stack to the next function call. At this point java throws this error.

I suggest you to read more about stack and heap memories.
Check What and where are the stack and heap?

Community
  • 1
  • 1
Buddhima Gamlath
  • 2,318
  • 1
  • 16
  • 26