Question

In: Computer Science

Java programming One of the most popular games of chance is a dice game known as...

Java programming

One of the most popular games of chance is a dice game known as “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 that sum becomes the player’s “points.” To win, you must continue rolling the dice until you “make your points.” The player loses by rolling a 7 before making the points.

Solutions

Expert Solution

Here is the Java Code which simulates "craps" game.Each program run simulates one game.

Sample outputs have been added in the end.

Code:

public class Craps {

    public static int rollDie(){                /*This function simulates throwing one die*/
        return 1+(int)(Math.random()*6);   /*returns a random number between 1 and 6*/
    }

    public static int sumOnDie(){         /*This function throws two die, finds the sum of the rolls and returns it*/
        return rollDie()+rollDie(); /*returns sum of the values on two die*/
    }

    public static void main(String[] args){

        int points,currentRoll;   /*points is used to store first roll and currentRoll is used to store any subsequent rolls*/
        System.out.println("Starting game :");

        points=sumOnDie();               /*finds first roll and assigns it to 'points'*/
        System.out.println("You rolled "+points);

        if(points==7 || points==11){        /*checks if points is equal to 7 or 11*/
            System.out.println("You win!");
        }
        else if(points==2 || points==3 || points==12){    /*checks if points is equal to 2,3 or 12*/
            System.out.println("You lose!");
        }
        else{                                      /*if any other number is rolled(4,5,6,8,9,10)*/
            do{
                currentRoll=sumOnDie();   /*finds the current roll*/
                System.out.println("You rolled "+currentRoll);
                if(currentRoll==points){           /*checks if current roll is equal to the player's points*/
                    System.out.println("You win!");
                    break;              /*exits while loop*/
                }
                else if(currentRoll==7){        /*checks if current roll is equal to 7*/
                    System.out.println("You lose!");
                    break;   /*exits the loop*/
                }
            }while(true);   /*runs the loop until player wins or loses*/
        }
    }
}

Sample output-1:

Sample output-2:


Related Solutions

1. One of the most popular games ever created is called the Ultimatum Game. In this...
1. One of the most popular games ever created is called the Ultimatum Game. In this game, there are two players: proposer and responder. The proposer moves first and is given $10 to split. Suppose they can offer any integer amount from 0,1,2,...,10. The responder observes this offer and has two choices: accept or reject. If the responder accepts, the proposed offer goes through. If the responder rejects, both players receive $0. What is the Nash Equilibrium in this game?...
1. One of the most popular games ever created is called the Ultimatum Game. In this...
1. One of the most popular games ever created is called the Ultimatum Game. In this game, there are two players: proposer and responder. The proposer moves first and is given $10 to split. Suppose they can offer any integer amount from 0,1,2,...,10. The responder observes this offer and has two choices: accept or reject. If the responder accepts, the proposed offer goes through. If the responder rejects, both players receive $0. What is the Nash Equilibrium in this game?...
Stuck in the mud is a popular dice game in UK. The game uses five (5)...
Stuck in the mud is a popular dice game in UK. The game uses five (5) 6-sided dice to play. The players play in turns. Choose one player to start the game. The player will roll all five (5) dice. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2...
Stuck in the mud is a popular dice game in UK. The game uses five (5)...
Stuck in the mud is a popular dice game in UK. The game uses five (5) 6-sided dice to play. The players play in turns. Choose one player to start the game. The player will roll all five (5) dice. If the player rolled any 2s or 5s, the player does not score any points for this throw. The player can only score on a roll which does not include the number 2 and 5. Any dice with a 2...
Suppose that a game of chance is played with a pair of fair 10-sided dice (with...
Suppose that a game of chance is played with a pair of fair 10-sided dice (with the sides numbered 1 to 10). In the game, you can pick any number from 1 to 10 and the two dice are then “rolled” in a cage. If $1 is bet and exactly one of the number that you picked is rolled you win $1, and if both of the dice are the number that you picked you win $20 (in each of...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die...
PROGRAMMING IN C-DICE GAME Q1. A player rolls two dice at the same time. Each die has six faces, which 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. (i) If the sum is 2 or 10 on the first throw, the player wins. (ii) If the sum is 3, 7 or 12 on the first throw, the player loses. (iii)...
3.50 Passedix is a game of chance played with three fair dice. Players bet whether the...
3.50 Passedix is a game of chance played with three fair dice. Players bet whether the sum of the faces shown on the dice will be above or below ten. During the late sixteenth century, the astronomer and mathematician Galileo Galilei was asked by the Grand Duke of Tuscany to explain why “the chance of throwing a 9 with three fair dice was less than that of throwing a 10.” (Interstat, Jan. 2004) The grand duke believed that the chance...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
A gambler plays a dice game where a pair of fair dice are rolled one time...
A gambler plays a dice game where a pair of fair dice are rolled one time and the sum is recorded. The gambler will continue to place $2 bets that the sum is 6, 7, 8, or 9 until she has won 7 of these bets. That is, each time the dice are rolled, she wins $2 if the sum is 6, 7, 8, or 9 and she loses $2 each time the sum is not 6, 7, 8, or...
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice....
In the game of craps, a player (known as the shooter) rolls two fair six-sided dice. The shooter immediately loses if the sum of the dice is 2, 3, or 12 and immediately wins if the sum of the dice is 7 or 11 on the first roll. If the sum is anything else (4, 5, 6, 8, 9, or 10), that number becomes the point and the shooter rolls again. The shooter now wins by rolling that same point...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT