mardi 26 février 2019

Random in Java, repeating same number

My code:

public class SkillsDemoTwoCrapsGameClass {

public static void main(String[] args) {
    RandomNumber diceRoll = new RandomNumber(); //Create instance diceRoll of class RandomNumber
    play playGame = new play(); //Create instance playGame of class play


    //Initialise variables from play class
    playGame.diceRoll = diceRoll.randNum;
    playGame.point = playGame.diceRoll;
    playGame.newPoint = diceRoll.randNum;

        System.out.println("WELCOME TO A GAME OF CRAPS!");

        if(playGame.diceRoll == 7 || playGame.diceRoll == 11){
            //Show the users point
            System.out.println("Point: " + playGame.point);
            System.out.println("------------------------------");

            //Tell user they won
            System.out.println("Congratulations you won, with a " + playGame.diceRoll);
        }
        else if(playGame.diceRoll == 2 || playGame.diceRoll == 3|| playGame.diceRoll == 12){
            //Show the users point
            System.out.println("Point: " + playGame.point);
            System.out.println("------------------------------");

            //Tell the user they lost
            System.out.println("Sorry you lost, with a " + playGame.diceRoll);
        }
        else{
            System.out.println("Point: " + playGame.point);
            System.out.println("------------------------------");

            while(playGame.point != playGame.newPoint || playGame.point == playGame.newPoint){
                /*
                BUG: (2/2/19)
                    User will receive their original roll again, causing them to always win
                */
                //Roll dice again for the new point
                playGame.newPoint = diceRoll.randNum;

                //Checks if the user can win
                if(playGame.point == playGame.newPoint){
                    System.out.println("Your new roll: " + playGame.newPoint + "\t\t Win");
                    break;
                }
                //Checks if the user has lost
                else if(playGame.newPoint == 7){
                    System.out.println("Your new roll: " + playGame.newPoint + "\t\t Lose");
                    break;
                }
                //Check if user needs to roll again
                else{
                    System.out.println("Your new roll: " + playGame.newPoint + "\t\t No help");    
                    }
            }
        }
    }
    }

    class RandomNumber{
     Random rand = new Random();
     int randNum = rand.nextInt(12) + 1;    
    }

    class play{
    int diceRoll, point, newPoint;
    }

The above code is for a game of Craps, my problem is when the user needs to get a new point. Instead of a new random number being assigned, they receive the same number as before. Is there a way to call the RandomNumber class, and get a new random number to assign to the newPoint variable?

Thanks




Aucun commentaire:

Enregistrer un commentaire