In Java language Objectives: Random Class and Flow of control. Description: Crap
ID: 3789206 • Letter: I
Question
In Java language
Objectives: Random Class and Flow of control.
Description: Craps is a popular gambling game. The rules of the game are as follows: The player rolls 2 dice the first time (the “come out roll”). If the 2 dice sum to 7 or 11 it is a “natural” and the player wins. If the sum of the 2 dice is 2, 3 or 12 then it is “craps” and the player loses.
Any other sum on the first roll 4, 5, 6, 8, 9, or 10 becomes the player’s “point”. The player then continues to roll the dice until either the player rolls the point again or until 7 is rolled. If the player rolls the point again then the player wins. If the player rolls 7 before rolling the point again then the player loses.
Here are some example outcomes of games.
Game 1: 7 (win)
Game 2: 11 (win)
Game 3: 2 (lose)
Game 4: 3 (lose)
Game 5: 12 (lose)
Game 6: 8, 2, 6, 11, 8 (win)
Game 7: 9, 12, 6, 7 (lose)
The player starts off with an initial account of ($1000). The player can bet some amount of money (less than or equal to his current account) in a game. If the player wins he gets double the amount of what he bet. If he loses, the money he bet will be reduced from its current account. For example, the current account of a player is $500. He bets $100 on a game. If he wins, his account will be $ 700 (500 + 300). If he loses, his account will become $ 400 (500 -100).
Your Program allows the user to play more than one games. It should give the player four options to choose:
1. Play another game
2. Print the summary of all the games played
3. Help
4. Quit.
Input: All input are done interactively.
Output: A sample execution of the program is given below. Only the bolded letters are user’s input.
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P
Current account: $1000
How much you want to bet: $100
Game 1:7 Current account: $ 1200
------------------------------------------
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P
Current account: $1200
How much you want to bet: $500
Game 2: 9, 12, 6, 7 <lose>
Current amount: $700
------------------------------------------
Game of Craps
P–Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
P
Current account: $700
How much you want to bet: $50
Game 3: 4, 12, 6, 4 <win>
Current amount: $800
------------------------------------------
Game of Craps
P –Play one Game of Craps
R-print a summary of all the games played
H-help
Q-Quit
R
------------------------------------------
Total number of Games wins 3
lost 2
average 1
rolls 3
Explanation / Answer
Hi buddy, please find the below java program. Comments have been added for your better understanding. I tested the program and It's working fine :-)
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
static int roll2Dice(Random r){
int dice1=0, dice2=0, total=0;
dice1 = r.nextInt(6)+1;
dice2 = r.nextInt(6)+1;
total = dice1 + dice2;
threws ++;
return total;
}
static int threws = 0;
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner obj = new Scanner(System.in);
char choice;
int wins = 0;
int looses = 0;
int amount = 1000;
int games = 0;
int bet = 0;
int sum = 0;
char ch;
Random r = new Random();
int dice1 , dice2, total;
while(true){
//Printing the options and exiting if the input is Q
System.out.println("P - Play another game R - Print the summary of all the games played H - Help Q - Quit.");
ch = obj.next().charAt(0);
if(ch=='Q'){
break;
}
System.out.println("Current Account: $"+amount);
boolean point = false;
int pointResult = 0;
//If the user enters 'P' then the game is played
if(ch=='P'){
System.out.print("How much you want to bet: ");
bet = obj.nextInt();
System.out.println("PLAYING : "+games);
//Increment game count by one
games++;
//Game is played in a loop
System.out.print("Game : ");
while(true){
//This method simulates the dice and returns sum of 2 throws randomly
total = roll2Dice(r);
System.out.print(total+" ,");
//Checks whether a point was achieved in the previous game
if(point){
//If previous throw was point, then roll the dice till 7 or the previous point is thrown
while(true){
//If 7 is thrown player looses
if(total==7){
looses++;
amount = amount-bet;
System.out.println(" <loose>");
break;
}
//If previous point is thrown, player wins
else if(total==pointResult){
wins++;
amount = amount + 2*bet;
System.out.println(" <win>");
break;
}
//Else keep rolling the dice
total = roll2Dice(r);
System.out.print(total+" ,");
}
break;
}
//If previous throw was not a point
else{
//If player throws 7 or 11, he wins
if(total==7||total==11){
wins++;
amount = amount + 2*bet;
System.out.println(" <win>");
break;
}
//Player looses incase it is 2, 3 or 12
else if(total==2||total==3||total==12){
looses++;
amount = amount - bet;
System.out.println(" <loose>");
break;
}
//Else it is a point and store it in these two variables
else{
point = true;
pointResult = total;
}
}
}
System.out.println("Current Amount :$"+amount);
}
if(ch=='R'){
System.out.println("Total number of games won "+wins);
System.out.println("Lost "+looses);
System.out.println("Average "+(wins*1d)/(games*1d));
System.out.println("rolls "+threws);
}
}
}
}
OUTPUT:
D:>java Main
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
P 100
Current Account: $1000
How much you want to bet: PLAYING : 0
Game : 11 <win>
Current Amount :$1200
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
P 200
Current Account: $1200
How much you want to bet: PLAYING : 1
Game : 4 ,6 ,7 <loose>
Current Amount :$1000
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
P 300
Current Account: $1000
How much you want to bet: PLAYING : 2
Game : 9 ,6 ,8 ,2 ,11 ,6 ,6 ,6 ,6 ,9 <win>
Current Amount :$1600
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
P 400
Current Account: $1600
How much you want to bet: PLAYING : 3
Game : 6 ,8 ,9 ,3 ,6 <win>
Current Amount :$2400
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
R
Current Account: $2400
Total number of games won 3
Lost 1
Average 0.75
rolls 19
P - Play another game
R - Print the summary of all the games played
H - Help
Q - Quit.
Q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.