Question

In: Computer Science

In Java Language. Must Include Comments. We will simulate a dice game in which 2 dice...

In Java Language.

Must Include Comments.

We will simulate a dice game in which 2 dice are rolled.

If the roll is 7 or 11, you win.

If the roll is 2, 3 or 12, you lose

If the roll is any other value, it establishes a point.

If, with a point established, that point is rolled again before a 7, you win.

If, with a point established, a 7 is rolled before the point is rolled again you lose.

Build your algorithm incrementally. First write a program that simulates a roll of two dice, then

outputs if it is a simple win (7 or 11), or a simple loss (2 or 3 or 12), or something else.

1. I will help you with the algorithm:

//get a random number between 1 and 6, call it d1

//get a second random number between 1 and 6, call it d2.

//compute the total & print it so we know what it was

//if the total is 7 or 11 print “congratulations, you win”

// else if the total is 2 or 3 or 12 print “you lose”

//else print “something else”

2. Create the program and test it by running it a few times to verify that you are correctly

printing whether you won, lost, or “something else”.

3. Now fix the part where you wrote “something else”. In this case you rolled a number which

we called total. We need to keep rolling the dice and looking at the new total each time. If the

new total is the same as total, you win. If the new total is 7, you lose. And if something else

you roll again...... For example, an initial total of 6 was neither a win or a lose. So 6 is now

the important number called “point” above. Roll the dice again, if it is 6 you win, and if 7 you

lose. If anything else you roll again and keep doing so until you roll either a 6 and win, or 7

and lose.

Write this part of your algorithm here (or on another piece of paper). Once we verify your logic is

correct you may code it.

Solutions

Expert Solution

CODE IN JAVA:

DiceGame.java file:

import java.util.Random;

public class DiceGame {

public static void main(String[] args) {

//creating object to the Random class to generate random numbers

Random r = new Random();

//simulating the rolling of two dice

int die1 = r.nextInt(6) + 1;

int die2 = r.nextInt(6) + 1;

int roll = die1 + die2;

System.out.println("The roll is:" + roll);

//checking the roll

if (roll == 7 || roll == 11) {

System.out.println("Congratulations, You win the game");

} else if (roll == 2 || roll == 3 || roll == 12) {

System.out.println("You lose the game");

} else {

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

die1 = r.nextInt(6) + 1;

die2 = r.nextInt(6) + 1;

int total = die1 + die2;

System.out.println("The roll is:" + total);

while (total != roll && total != 7) {

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

die1 = r.nextInt(6) + 1;

die2 = r.nextInt(6) + 1;

total = die1 + die2;

System.out.println("The roll is:" + total);

}

if (total == roll) {

System.out.println("Congratulations, You win the game");

} else {

System.out.println("You lose the game");

}

}

}

}

OUTPUT:


Related Solutions

We will simulate a dice game in which 2 dice are thrown. If the roll is...
We will simulate a dice game in which 2 dice are thrown. If the roll is 7 or 11, you win. If the roll is 2, 3, or 12, you lose If the roll is any other value, it establishes a point. If with a point established, that point is rolled again before a 7, you win. If, with a point established, a 7 is rolled before the point is rolled again you lose. Build your algorithm incrementally. First write...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY...
Write a MATLAB program to simulate the FIFTY dice game that can automatically play the FIFTY dice game for any amount of players, keep scores for all the players, clearly show the game progress for every round, every player, and every roll in the command window, automatically ends the game when the first player accumulates 50 points or more game score, and automatically select the game winner and the winning game score. i have coded most of the game, i...
Write a Java program to simulate the rolling of two dice. The application should use an...
Write a Java program to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12. Your application should roll the dice 36,000,000 times. Store the results of each roll...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately....
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at...
Write a complete JAVA program, including comments (worth 2 pts -- include a good comment at the top and at least one more good comment later in the program), to process data for the registrar as follows: NOTE: Include prompts. Send all output to the screen. 1. Read in the id number of a student, the number of credits the student has, and the student’s grade point average (this is a number like 3.25). Print the original data right after...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two...
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two dice consisting of 6 sides are rolled. Write a program that simulates this. The user will be asked if he/she wishes to continue the roll of dice. If the answer is, “Y” or “y”, then your application will continue to roll the two dice. Each roll of the dice must occur 6 times, each time the user agrees to continue (see sample output). Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT