Java programming 10th Edition Exercise 6.32 *6.30 Game: craps) Craps is a popula
ID: 3602213 • Letter: J
Question
Java programming 10th Edition
Exercise 6.32
*6.30 Game: craps) Craps is a popular dice game played in casinos. Write a program to play a variation of the game, as follows: Roll two dice. Each die has six faces representing values 1, 2, ., and 6, respec tively. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win. Your program acts as a single player. Here are some sample runs. You rolled 5 + 6 = 11 You wirn You rolled 1 + 2 = 3 You lose You rolled 4+4=8 point is 8 You rolled 6 + 2 = 8 You wirn You rolled 3 + 2 = 5 point is 5 You rolled 2 + 5 = 7 You loseExplanation / Answer
1)
GameOfCraps.java
import java.util.Scanner;
public class GameOfCraps {
public static void main(String[] args) {
// Scanner class object is used to read the numbers entered by the user
Scanner sc = new Scanner(System.in);
// Declaring variables
int result;
// Calling the method craps()
result = craps();
// if the result value is zero it displays the message "You Lost"
if (result == 0) {
System.out.println("You lost.");
}
// if the result value is one it displays the message "You Won"
else if (result == 1) {
System.out.println("You won!");
}
}
private static int craps() {
// Declaring variables
int count = 0, point = 0, num, dice1, dice2;
// This while loop continue to execute either player wins or lose the
// game
while (true) {
dice1 = rollDie();
dice2 = rollDie();
// calculating the sum of two dice values
num = dice1 + dice2;
/*
* If the player got sum as 7 for the first time player wins else
* player lost
*/
if (num == 7 || num == 11) {
if (count == 0) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
return 1;
} else if (num == 7) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
return 0;
} else if (num == 11) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player got sum as 2 or 3 or 12 for the first time player
* wins else player continue the game
*/
else if (num == 2 || num == 3 || num == 12) {
if (count == 0) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
return 0;
} else {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player rolled the dice and got the number which is same as
* point Player won the game
*/
else if (point == num) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "=" + num);
return 1;
}
/*
* If the player roll the dice and got sum other than 7 for the
* first time The point is set.
*/
else {
if (count == 0) {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
point = num;
System.out.println("Point is " + point);
count++;
continue;
} else {
System.out.println("You rollled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
}
}
private static int rollDie() {
// Generating the random number between 1 and 6
return (int) (6.0 * Math.random() + 1.0);
}
}
________________
Output:
You rollled 5 + 3=8
Point is 8
You rollled 2 + 3=5
You rollled 5 + 1=6
You rollled 2 + 6=8
You won!
_______________
Output#2:
You rollled 6 + 3=9
Point is 9
You rollled 1 + 2=3
You rollled 2 + 3=5
You rollled 4 + 5=9
You won!
________________
2)
GameOfCraps.java
import java.util.Scanner;
public class GameOfCraps {
public static void main(String[] args) {
// Scanner class object is used to read the numbers entered by the user
Scanner sc = new Scanner(System.in);
//Declaring variables
int result, total_games = 0, win_games = 0;
//This loop continues to execute until the user enters input other than 'Y' or 'y'
for(int i=0;i<10000;i++)
{
//Counting the no of games played
total_games++;
//Calling the method craps()
result = craps();
//if the result value is zero it displays the message "You Lost"
if (result == 0) {
System.out.println("You lost.");
}
//if the result value is one it displays the message "You Won"
else if (result == 1) {
System.out.println("You won!");
//counting the no of games won
win_games++;
}
}
System.out.println("Total No of games won :"+win_games);
}
private static int craps() {
// Declaring variables
int count = 0, point = 0, num, dice1, dice2;
// This while loop continue to execute either player wins or lose the
// game
while (true) {
//Generating the random number between 1 and 6
dice1 = (int) (6.0 * Math.random() + 1.0);
//Generating the random number between 1 and 6
dice2 = (int) (6.0 * Math.random() + 1.0);
//calculating the sum of two dice values
num = dice1 + dice2;
/*
* If the player got sum as 7 for the first time player wins
* else player lost
*/
if (num == 7 || num==11) {
if (count == 0) {
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
return 1;
}
else if(num==7)
{
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
return 0;
}
else if(num==11)
{
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player got sum as 2 or 3 or 12 for the first time player wins
* else player continue the game
*/
else if(num==2 ||num==3 || num==12)
{
if(count==0)
{
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
return 0;
}
else {
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player rolled the dice and got the number which is
* same as point Player won the game
*/
else if (point == num) {
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
return 1;
}
/*
* If the player roll the dice and got sum other than 7 for the
* first time The point is set.
*/
else {
if(count==0)
{
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
point = num;
System.out.println("Point is "+point);
count++;
continue;
}
else
{
System.out.println("You rolled " + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
}
}
}
___________________
Output:
You rolled 1 + 5=6
Point is 6
You rolled 4 + 5=9
You rolled 2 + 2=4
You rolled 1 + 1=2
You rolled 6 + 4=10
You rolled 3 + 3=6
You won!
You rolled 3 + 4=7
You won!
You rolled 3 + 4=7
You won!
You rolled 1 + 3=4
Point is 4
You rolled 6 + 4=10
You rolled 6 + 2=8
You rolled 1 + 3=4
You won!
You rolled 4 + 4=8
Point is 8
You rolled 6 + 6=12
You rolled 3 + 5=8
You won!
You rolled 4 + 3=7
You won!
You rolled 5 + 5=10
Point is 10
You rolled 6 + 3=9
You rolled 6 + 4=10
You won!
You rolled 2 + 5=7
You won!
You rolled 4 + 5=9
Point is 9
You rolled 5 + 3=8
You rolled 6 + 4=10
You rolled 2 + 5=7
You lost.
You rolled 6 + 1=7
You won!
Total No of games won :4917
______________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.