Java Programming (Please add comments in your source code) 6.32 (Game: chance of
ID: 3782127 • Letter: J
Question
Java Programming (Please add comments in your source code)
6.32 (Game: chance of winning at craps) Revise Exercise 6.30 to run it 10,000 times and display the number of winning games.
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, respectively. 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 win
You rolled 1 + 2 = 3
You lose
You rolled 4 + 4 = 8
point is 8
You rolled 6 + 2 = 8
You win
You rolled 3 + 2 = 5
point is 5
You rolled 2 + 5 = 7
You lose
Explanation / Answer
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("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 1;
}
else if(num==7)
{
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 0;
}
else if(num==11)
{
System.out.println("Roll :" + 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("Roll :" + dice1 + " + " + dice2 + "="+ num);
return 0;
}
else {
System.out.println("Roll :" + 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("Roll :" + 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("Roll :" + dice1 + " + " + dice2 + "="+ num);
point = num;
System.out.println("Point is "+point);
count++;
continue;
}
else
{
System.out.println("Roll :" + dice1 + " + " + dice2 + "="+ num);
count++;
continue;
}
}
}
}
}
_______________________________
Output:
Roll :3 + 2=5
Point is 5
Roll :6 + 1=7
You lost.
Roll :5 + 3=8
Point is 8
Roll :3 + 3=6
Roll :3 + 5=8
You won!
Roll :4 + 4=8
Point is 8
Roll :1 + 3=4
Roll :3 + 5=8
You won!
Roll :6 + 6=12
You lost.
Roll :2 + 1=3
You lost.
Roll :3 + 2=5
Point is 5
Roll :2 + 2=4
Roll :1 + 6=7
You lost.
Roll :6 + 2=8
Point is 8
Roll :4 + 2=6
Roll :3 + 4=7
You lost.
Roll :2 + 5=7
You won!
Roll :3 + 3=6
Point is 6
Roll :3 + 4=7
You lost.
Roll :4 + 1=5
Point is 5
Roll :2 + 6=8
Roll :6 + 1=7
You lost.
Roll :3 + 6=9
Point is 9
Roll :2 + 3=5
Roll :1 + 6=7
You lost.
Total No of games won :4779
____________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.