+1 678 648 4277 

Dice Rolling Game using Java Assignment Solution


Dice Game: 30 or Bust!

The goal of the game is to reach a score of exactly 30.

Each player has two dice. The players take turns rolling their dice. When a player rolls both dice, the player may keep the total of both dice or the face value of either die to add to that player's total. A player MUST select either the face value of one of the dice or the total value of both dice. If a player's score goes over 30, then that player's score is reset to zero, and play continues until one player obtains a score of exactly 30. The first player to score exactly 30 is the Winner.

Your code should allow for two players.

Your code should obtain the players' names and print a personalized welcome to the game.

The first player's name and starting score of zero should be displayed. The game should roll two dice for the player and display the face value of each die and the total value of both dice. The first player is then required to pick the face value of one of the dice or the total value of both dice. This value is added to that player's score. The new player score is displayed.

If the player's score is less than 30, the turn is passed to the other player.

If the player's score is greater than 30, then the player score is reset to zero and the turn is passed to the other player.

If the player's score is exactly 30, then that player is declared the winner.

An example run:

Hello, what is the name of the first player? The One

What is the name of the second player? The Two

Welcome players The One and The Two!

The goal of this game is to accumulate a player score of exactly 30. The first player to score exactly 30 is the Winner!

You will roll a pair of dice, then you must choose the score of one of the dice or the total of the two dice. This value is added to your player score. If your score goes over 30, your score is reset to zero. Player turn changes after each roll of the dice. You win when you accumulate a score of exactly 30.

Here we go!

Player The One, it is your turn!

Your score: 0

Your roll:

Die 1: 5

Die 2: 6

Total: 11

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 11

Player The Two, it is your turn!

Your score: 0

Your roll:

Die 1: 2

Die 2: 1

Total: 3

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 3

Player The One, it is your turn!

Your score: 11

Your roll:

Die 1: 6

Die 2: 4

Total: 10

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 21

Player The Two, it is your turn!

Your score: 3

Your roll:

Die 1: 1

Die 2: 1

Total: 2

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 5

Player The One, it is your turn!

Your score: 21

Your roll:

Die 1: 5

Die 2: 2

Total: 7

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 28

Player The Two, it is your turn!

Your score: 5

Your roll:

Die 1: 3

Die 2: 3

Total: 6

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 11

Player The One, it is your turn!

Your score: 28

Your roll:

Die 1: 4

Die 2: 3

Total: 7

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 2

Your total: 31, your score is now reset to 0!

Player The Two, it is your turn!

Your score: 11

Your roll:

Die 1: 1

Die 2: 2

Total: 3

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 14

Player The One, it is your turn!

Your score: 0

Your roll:

Die 1: 5

Die 2: 5

Total: 10

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 10

Player The Two, it is your turn!

Your score: 14

Your roll:

Die 1: 4

Die 2: 3

Total: 7

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 21

Player The One, it is your turn!

Your score: 10

Your roll:

Die 1: 4

Die 2: 2

Total: 6

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 16

Player The Two, it is your turn!

Your score: 21

Your roll:

Die 1: 2

Die 2: 2

Total: 4

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 25

Player The One, it is your turn!

Your score: 16

Your roll:

Die 1: 3

Die 2: 5

Total: 8

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 3

Your total: 24

Player The Two, it is your turn!

Your score: 25

Your roll:

Die 1: 6

Die 2: 6

Total: 12

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 1

Your total: 31, your score is now reset to 0!

Player The One, it is your turn!

Your score: 24

Your roll:

Die 1: 6

Die 2: 1

Total: 7

Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): 1

Your total: 30! Congratulations, you WIN!!

Solution:

import java.util.Scanner; import java.util.Random; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Random rn = new Random(); System.out.println("Hello, what is the name of the first player?"); String name1 = sc.nextLine(); System.out.println("What is the name of the second player?"); String name2 = sc.nextLine(); System.out.printf("Welcome players %s and %s!", name1, name2); System.out.println("Here we go!"); int score1= 0,score2 = 0; while (true){ int rand1 = 0,rand2 = 0,decision = 0; // Player 1 System.out.printf( "Player %s, it is your turn!\n", name1); System.out.printf("Your score: %d\n", score1); System.out.println("Your roll:"); rand1 = rn.nextInt(6) + 1; System.out.println("Die 1: "+ rand1); rand2 = rn.nextInt(6) + 1; System.out.println("Die 2: "+ rand2 ); System.out.printf("Total: %d\n", rand1+rand2); System.out.println("Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): "); decision = sc.nextInt(); if (decision == 1){ score1 += rand1; } else if(decision == 2){ score1 += rand2; } else{ score1 += rand1 + rand2; } System.out.printf("Your Total: %d\n", score1); if( score1 == 30){ System.out.println("Your total: 30! Congratulations, you WIN!!"); break; } else if(score1>30){ System.out.printf("Your total: %d, your score is now reset to 0!\n", score1); score1 = 0; } //player2 System.out.printf( "Player %s, it is your turn!\n", name2); System.out.printf("Your score: %d\n", score2); System.out.println("Your roll:"); rand1 = rn.nextInt(6) + 1; System.out.println("Die 1: "+ rand1); rand2 = rn.nextInt(6) + 1; System.out.println("Die 2: "+ rand2 ); System.out.printf("Total: %d\n", rand1+rand2); System.out.println("Do you wish to (1) Keep die 1, (2) Keep die 2, (3) Keep the total? (Respond with 1 or 2 or 3): "); decision = sc.nextInt(); if (decision == 1){ score2 += rand1; } else if(decision == 2){ score2 += rand2; } else{ score2 += rand1 + rand2; } System.out.printf("Your Total: %d\n", score2); if( score2 == 30){ System.out.println("Your total: 30! Congratulations, you WIN!!\n"); break; } else if(score2>30){ System.out.printf("Your total: %d, your score is now reset to 0!\n", score2); score2 = 0; } } } }