In Java Please Objectives Random Class and Flow of control D

In Java Please.

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

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

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   lost   average rolls

          3              2       1        3

Solution

Code

import java.util.Random;

import java.util.Scanner;

public class Craps {

   // declaring global variables to track games and num of rolls

   static int total;

   static int win;

   static int lost;

   static int rolls;

   static int amount = 1000;

   static Scanner in = new Scanner(System.in);

   public static void main(String[] args) {

       char option = \'Q\';

       do {

           System.out.println(\"Menu\");

           System.out.println(\"P. Play another game\");

           System.out.println(\"R. Print the summary of all the games played\");

           System.out.println(\"H. Help\");

           System.out.println(\"Q. Quit\");

           System.out.println(\"Enter your option\");

           option = in.next().charAt(0);

           // to handle case upper and lower case

           switch (option) {

           case \'P\':

           case \'p\':

               play();

               break;

           case \'R\':

           case \'r\':

               System.out.println(\"Total number of Games:\" + total + \" wins:\" + win + \" lost:\" + lost

                       + \" average rolls:\" + (rolls / total));

               break;

           case \'H\':

           case \'h\':

               System.out.println(

                       \"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.\ \");

               break;

           case \'Q\':

           case \'q\':

               System.out.println(\"Exiting the game!\");

               System.exit(0);

               break;

           }

       } while (true);

   }

   public static void play() {

       System.out.println(\"Current account: \" + amount + \"$\");

       System.out.print(\"How much you want to bet: $\");

       int bet = in.nextInt();

       // System.out.println();

       Random rn = new Random();

       int f = rn.nextInt(6) + 1;

       int s = rn.nextInt(6) + 1;

       int sum = f + s;

       total++;

       rolls++;

       // rolling the dice for the first time

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

           win++;

           System.out.println(\"Game \" + total + \": \" + sum);

           amount += (2 * bet);

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

           lost++;

           System.out.println(\"Game \" + total + \": \" + sum);

           amount -= bet;

       } else {

           System.out.print(\"Game \" + total + \": \" + sum);

           // roll the dice if he gains to point

           do {

               rolls++;

               f = rn.nextInt(6) + 1;

               s = rn.nextInt(6) + 1;

               sum = f + s;

               System.out.print(\", \" + sum);

               // to roll the dice until player gains the point or throws 7

           } while (!(sum == 7 || sum == 4 || sum == 5 || sum == 6 || sum == 8 || sum == 9 || sum == 10));

           System.out.println();

           if (sum == 7) {

               amount -= bet;

               lost++;

           } else {

               amount += (2 * bet);

               win++;

           }

       }

       System.out.println(\"Current account: $ \" + amount);

   }

}

Sample output

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

p

Current account: 1000$

How much you want to bet: $200

Game 1: 5, 7

Current account: $ 800

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

p

Current account: 800$

How much you want to bet: $100

Game 2: 6, 7

Current account: $ 700

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

p

Current account: 700$

How much you want to bet: $30

Game 3: 6, 9

Current account: $ 760

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

r

Total number of Games:3 wins:1 lost:2 average rolls:2

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

h

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.

Menu

P. Play another game

R. Print the summary of all the games played

H. Help

Q. Quit

Enter your option

q

Exiting the game!

In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro
In Java Please. Objectives: Random Class and Flow of control. Description: Craps is a popular gambling game. The rules of the game are as follows: The player ro

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site