The following description has been adopted from Deitel Deit
The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called \"craps,\" which is played in casinos and back alleys throughout the world. The rules of the game are straightforward:
A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3, or 12 on the first throw (called \"craps\"), the player loses (i.e. the \"house\" wins). If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player\'s \"point.\" To win, you must continue rolling the dice until you \"make your point.\" The player loses by rolling a 7 before making the point.
Write a program that implements a craps game according to the above rules. The game should allow for wagering. This means that you need to prompt that user for an initial bank balance from which wagers will be added or subtracted. Before each roll prompt the user for a wager. Once a game is lost or won, the bank balance should be adjusted. As the game progresses, print various messages to create some \"chatter\" such as, \"Sorry, you busted!\", or \"Oh, you\'re going for broke, huh?\", or \"Aw cmon, take a chance!\", or \"You\'re up big, now\'s the time to cash in your chips!\"
Use the below functions to help you get started! You may define more than the ones suggested if you wish!
(5 pts) void print_game_rules (void) — Prints out the rules of the game of \"craps\".
(5 pts) double get_bank_balance (void) - Prompts the player for an initial bank balance from which wagering will be added or subtracted. The player entered bank balance (in dollars, i.e. $100.00) is returned.
(5 pts) double get_wager_amount (void) - Prompts the player for a wager on a particular roll. The wager is returned.
(5 pts) int check_wager_amount (double wager, double balance) - Checks to see if the wager is within the limits of the player\'s available balance. If the wager exceeds the player\'s allowable balance, then 0 is returned; otherwise 1 is returned.
(5 pts) int roll_die (void) - Rolls one die. This function should randomly generate a value between 1 and 6, inclusively. Returns the value of the die.
(5 pts) int calculate_sum_dice (int die1_value, int die2_value) - Sums together the values of the two dice and returns the result. Note: this result may become the player\'s point in future rolls.
(10 pts) int is_win_loss_or_point (int sum_dice) - Determines the result of thefirstdice roll. If the sum is 7 or 11 on the roll, the player wins and 1 is returned. If the sum is 2, 3, or 12 on the first throw (called \"craps\"), the player loses (i.e. the \"house\" wins) and 0 is returned. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player\'s \"point\" and -1 is returned.
(10 pts) int is_point_loss_or_neither (int sum_dice, int point_value) - Determines the result of any successive roll after the first roll. If the sum of the roll is the point_value, then 1 is returned. If the sum of the roll is a 7, then 0 is returned. Otherwise, -1 is returned.
(5 pts) double adjust_bank_balance (double bank_balance, double wager_amount, int add_or_subtract) - If add_or_subtract is 1, then the wager amount is added to the bank_balance. If add_or_subtract is 0, then the wager amount is subtracted from the bank_balance. Otherwise, the bank_balance remains the same. The bank_balance result is returned.
(5 pts) void chatter_messages (int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance) - Prints an appropriate message dependent on the number of rolls taken so far by the player, the current balance, and whether or not the player just won his roll. The parameter win_loss_neither indicates the result of the previous roll.
(10 pts) Others?
(20 pts) A main ( ) function that makes use of the above functions in order to play the game of craps as explained above. Note that you will most likely have a loop in your main ( ) function (or you could have another function that loops through the game play).
| (5 pts) void print_game_rules (void) — Prints out the rules of the game of \"craps\". | |
| (5 pts) double get_bank_balance (void) - Prompts the player for an initial bank balance from which wagering will be added or subtracted. The player entered bank balance (in dollars, i.e. $100.00) is returned. | |
| (5 pts) double get_wager_amount (void) - Prompts the player for a wager on a particular roll. The wager is returned. | |
| (5 pts) int check_wager_amount (double wager, double balance) - Checks to see if the wager is within the limits of the player\'s available balance. If the wager exceeds the player\'s allowable balance, then 0 is returned; otherwise 1 is returned. | |
| (5 pts) int roll_die (void) - Rolls one die. This function should randomly generate a value between 1 and 6, inclusively. Returns the value of the die. | |
| (5 pts) int calculate_sum_dice (int die1_value, int die2_value) - Sums together the values of the two dice and returns the result. Note: this result may become the player\'s point in future rolls. | |
| (10 pts) int is_win_loss_or_point (int sum_dice) - Determines the result of thefirstdice roll. If the sum is 7 or 11 on the roll, the player wins and 1 is returned. If the sum is 2, 3, or 12 on the first throw (called \"craps\"), the player loses (i.e. the \"house\" wins) and 0 is returned. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player\'s \"point\" and -1 is returned. | |
| (10 pts) int is_point_loss_or_neither (int sum_dice, int point_value) - Determines the result of any successive roll after the first roll. If the sum of the roll is the point_value, then 1 is returned. If the sum of the roll is a 7, then 0 is returned. Otherwise, -1 is returned. | |
| (5 pts) double adjust_bank_balance (double bank_balance, double wager_amount, int add_or_subtract) - If add_or_subtract is 1, then the wager amount is added to the bank_balance. If add_or_subtract is 0, then the wager amount is subtracted from the bank_balance. Otherwise, the bank_balance remains the same. The bank_balance result is returned. | |
| (5 pts) void chatter_messages (int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance) - Prints an appropriate message dependent on the number of rolls taken so far by the player, the current balance, and whether or not the player just won his roll. The parameter win_loss_neither indicates the result of the previous roll. | |
| (10 pts) Others? | |
| (20 pts) A main ( ) function that makes use of the above functions in order to play the game of craps as explained above. Note that you will most likely have a loop in your main ( ) function (or you could have another function that loops through the game play). |
Solution
import java.util.Scanner;
public class craps {
public double balance;
Scanner in=new Scanner(System.in);
void print_game_rules()
{
System.out.println(\"************Instructions ********************\");
System.out.println(\"1.A player rolls two dice\ 2.If the sum is 7 or 11 on the first throw ,the player wins.\ 3.If the sum is 2, 3, or 12 on the first throw (called craps), the player loses (i.e. the house wins).\ 4. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player\'s \\\"point.\\\" \ 5.To win, you must continue rolling the dice until you \\\"make your point\\\".\ 5. The player loses by rolling a 7 before making the point.\ ---------------------------------------------\ \");
}
double get_bank_balance ()
{
System.out.println(\"Please enter your bank balance\");
balance=in.nextDouble();
return balance;
}
double get_wager_amount ()
{
double wager;
System.out.println(\"enter the wager amount for the round\");
wager=in.nextDouble();
return wager;
}
int check_wager_amount (double wager, double balance)
{
if(wager>balance)
return 0;
else return 1;
}
int roll_die ()
{
int ran = (int )(Math.random() * 6 + 1);
return ran;
}
int calculate_sum_dice (int die1_value, int die2_value)
{
int sum=die1_value+die2_value;
System.out.println(\"your round sum is:\"+sum);
return sum;
}
int is_win_loss_or_point (int sum_dice)
{
if(sum_dice==7 || sum_dice==11 )
return 1;
else if(sum_dice==2 ||sum_dice==3||sum_dice==12)
return 0;
else
return -1;
}
int is_point_loss_or_neither (int sum_dice, int point_value)
{
if(sum_dice==7)
return 0;
else if(sum_dice==point_value)
return 1;
else return -1;
}
double adjust_bank_balance (double bank_balance, double wager_amount, int add_or_subtract)
{
if(add_or_subtract==1)
bank_balance+=wager_amount;
else if (add_or_subtract==0)
bank_balance-=wager_amount;
return bank_balance;
}
void chatter_messages (int number_rolls, int win_loss_neither, double initial_bank_balance, double current_bank_balance)
{
if(initial_bank_balance<current_bank_balance)
System.out.println(\"You\'re up big, now\'s the time to cash in your chips!\");
if(win_loss_neither==-1)
System.out.println(\"Oh !common Take a chance\");
}
public static void main(String args[]){
Scanner on=new Scanner(System.in);
craps c= new craps();
c.print_game_rules ();
int point=0;
double initial_balance=c.get_bank_balance();
double balance=initial_balance;
int i=1;
int x=0;
int val=0;
while(true)
{
double wager=c.get_wager_amount();
x=c.check_wager_amount(wager,balance);
if(x==0)
{System.out.println(\"wager is greater than the current balance in your account\ do you want to choose anothe wagon amount:If yes press 1 else press any key to exit\");
int ch=on.nextInt();
if(ch==1)
continue;
else
break;
}
int r1=c.roll_die();
int r2=c.roll_die();
int sum=c.calculate_sum_dice(r1,r2);
if(i==1)
{
val=c.is_win_loss_or_point(sum);
if (val==1)
{
System.out.println(\"Hey!!! you won the game\");
balance=c.adjust_bank_balance (balance, wager, 1);
break;
}
else if( val==0)
{
System.out.println(\"oops!!! you loss the the game\");
balance= c.adjust_bank_balance (balance, wager, 0);
break;
}
else
{
point=sum;
System.out.println(\"you neither win nor loss.got your point continue with next round\");
}
}
else
{
val=c.is_point_loss_or_neither (sum,point);
if(val==1)
{
System.out.println(\"Hey!!! you won the game\");
balance=c.adjust_bank_balance (balance, wager, 1);
break;
}
else if( val==0)
{
System.out.println(\"oops!!! you loss the the game\");
balance= c.adjust_bank_balance (balance, wager, 0);
break;
}
else if (val==-1)
System.out.println(\"continue wiyh next throw\");
}
c.chatter_messages (i,1, initial_balance,balance);
i++;
}
}
}
Sample output:
************Instructions ********************
1.A player rolls two dice
2.If the sum is 7 or 11 on the first throw ,the player wins.
3.If the sum is 2, 3, or 12 on the first throw (called craps), the player loses (i.e. the house wins).
4. If the sum is 4, 5, 6, 8, 9, or 10 on the first throw, then the sum becomes the player\'s \"point.\"
5.To win, you must continue rolling the dice until you \"make your point\".
5. The player loses by rolling a 7 before making the point.
---------------------------------------------
Please enter your bank balance
10000
enter the wager amount for the round
3000
your round sum is:9
you neither win nor loss.got your point continue with next round
enter the wager amount for the round
4000
your round sum is:5
continue wiyh next throw
enter the wager amount for the round
3000
your round sum is:7
oops!!! you loss the the game




