3

I need help with this question:

A dice rolling game is played with two six-sided dice. A user playing the game, will roll the two dice and two random numbers between one and six will be generated. The sum of the two numbers will be used to decide the next step.

If the sum is 2,3 or 12 then the player wins. If the sum is 7 or 11 then he/she loses. If the sum is 4, 5, 6, 8, 9 or 10 then the program automatically rolls the dice again until the player wins or loses.

After every dice roll, the player will be prompted for an input. The player should decide if the game should continue or not.

The amount of games won and lost should also be displayed after every dice roll.

I have managed to get the first part to work but unable to work out how to prompt the user if they wish to continue or how many games they have won/lost

public class DiceGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      
        while (true) {
            int dice1=(int)(Math.random()*6+1);
            int dice2=(int)(Math.random()*6+1);
            int sum = dice1 + dice2;
            
            System.out.println("Roll: total = " + sum);
            
            if (sum==2 || sum==3 || sum==12) {
                System.out.println("Sorry with a " +  sum  + " you loose :(");
                break;
            }
            else if(sum==7 || sum==11) {
                System.out.println("With a " + sum + " you win :)");
                break;
            }
        }
    }
}
peterh
  • 11,875
  • 18
  • 85
  • 108
Bec
  • 59
  • 1
  • 8
  • Make an if statement where the program will ask you that, and if you choose that you don't want to continue just add a `break;`. I suppose you already know how to get data from standard input and all that stuff. – Shinra tensei Jul 24 '17 at 07:40
  • @Bec - my solution has both the flavors you need (with user input thru scanner and no user input with auto roll when 4 5 6 8 9 10). Please accept my answer and vote up if it works for you! – JRG Jul 26 '17 at 21:49
  • @Bec - please accept my answer and vote up, if it has solved your problem! – JRG Jul 30 '17 at 07:34

4 Answers4

3

Scanner can be used to prompt user to ask whether he/she wants to continue or not.

You can even track no. of times dices were rolled.

import java.util.Scanner;

public class DiceGame {

    public static int attempt = 1;
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int dice1 = (int) (Math.random() * 6 + 1);
        int dice2 = (int) (Math.random() * 6 + 1);
        int sum = dice1 + dice2;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
            }
            System.out.println();
            System.out.println("Do you wish to continue? Press 'y' for YES or ANY key for EXIT");
            if (!scanner.next().equalsIgnoreCase("y")) {
                break;
            }
            attempt++;
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

EDIT: If you want to roll the dices automatically when the sum is 4 5 6 8 9 10 then you would no more need Scanner i.e. user input whether to continue or not.

Here is the solution for the same.

public class DiceGame {

    public static int attempt = 1;

    public static void main(String[] args) {

        int dice1 = 0;
        int dice2 = 0;
        int sum = 0;

        while (true) {
            System.out.println();
            System.out.println("Rolling dice for " + attempt + " time!");

            dice1 = (int) (Math.random() * 6 + 1);
            dice2 = (int) (Math.random() * 6 + 1);
            sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum == 2 || sum == 3 || sum == 12) {
                System.out.println("Sorry with a " + sum + " you loose :(!");
                System.out.println();
                break;
            } else if (sum == 7 || sum == 11) {
                System.out.println("With a " + sum + " you win :)!");
                System.out.println();
                break;
                // this will roll the dices automatically
                // when sum is 4, 5, 6, 8, 9 or 10
            } else {
                System.out.println();
                System.out.println("With " + sum + " dices are rolled again automatically!!");
                attempt++;
            }
        }
        System.out.println("Thanks for playing dice game, you rolled the dice " + attempt + " times!");
    }
}

Sample Run

Rolling dice for 1 time!
Roll: total = 4

With a 4, dices are rolled again automatically!!

Rolling dice for 2 time!
Roll: total = 6

With a 6, dices are rolled again automatically!!

Rolling dice for 3 time!
Roll: total = 2
Sorry with a 2 you loose :(!

Thanks for playing dice game, you rolled the dice 3 times!
JRG
  • 4,037
  • 3
  • 23
  • 34
  • hi this semi worked it states attempt dont exist and im not sure what to place here int attempt = ; for it to show how many times dice were rolled – Bec Jul 25 '17 at 11:15
  • You have to declare and initialize attempts as 1 like I did, public static int attempt = 1; – JRG Jul 25 '17 at 14:45
  • If the sum is 4, 5, 6, 8, 9 or 10 then the program automatically rolls the dice again until the player wins or loses. do you know how to do this? – Bec Jul 26 '17 at 10:22
  • Yes, I have updated my post with an alternative solution where you want to roll the dices again until player wins or loses i.e. automatic rolling dices when sum is 4 5 6 8 9 10. If it is as you expected then please accept the answer and vote up! – JRG Jul 26 '17 at 17:29
2

You can use java Scanner class to prompt user for inputs.

Inside your while loop, prompt user for the inputs. You may need to modify your while loop condition.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

Based on the user's input, use 'brake' to exit from the loop. That should do it.

Scanner Reference

Supun Amarasinghe
  • 1,443
  • 3
  • 12
  • 24
1

I created a playAgainMessage method which returns an int. This decides whether or not the user will play again. Check it out:

import java.util.Scanner;

public class DiceGame {
    private static final int PLAY = 1;
    private static final int DO_NOT_PLAY = 2;
    private static int choice = 1;

    public static void main(String[] args) {
        while (choice == PLAY) {
            int dice1=(int)(Math.random()*6+1);
            int dice2=(int)(Math.random()*6+1);
            int sum = dice1 + dice2;

            System.out.println("Roll: total = " + sum);

            if (sum==2 || sum==3 || sum==12) {
                System.out.println("Sorry with a " +  sum  + " you loose :(");
                choice = playAgainMessage();
            }
            else if(sum==7 || sum==11) {
                System.out.println("With a " + sum + " you win :)");
                choice = playAgainMessage();
            }
        }
        if (choice == DO_NOT_PLAY) {
            System.out.println("Good bye...");
        }
    }

    private static int playAgainMessage() {
        Scanner input = new Scanner(System.in);
        System.out.println("Would you like to play again? '1' for yes, '2' for no");
        int choice = input.nextInt();
        input.close();
        if(choice == PLAY){
            System.out.println("Rolling...");
            return PLAY;
        } else {
            return DO_NOT_PLAY;
        }
    }
}

You can call the playAgainMessage method whenever you'd like, which makes it nice and easy to read.

This will keep playing until the user inputs something other than '1'. Try running it to see exact output.

1

You can use a do{}while(); loop, it will play once, and then ask the user to it again :

Scanner sc = new Scanner(System.in);
String input = "";
do {
    int // ...

    System.out.println("Roll: total = " + sum);

    if (sum == 2 || sum == 3 || sum == 12) {
        // ...
    } else if (sum == 7 || sum == 11) {    
        // ...
    }

    System.out.println("If you want to continue, write 'y' : ");
    input = sc.nextLine();
} while (input.equalsIgnoreCase("y"));
azro
  • 53,056
  • 7
  • 34
  • 70