Craps is a popular dice game played in casinos Write a progr

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.

Solution

/* CraapsGame: dice game played in casinos */

import java.util.Scanner;

public class CraapsGame
{
public static void main(String[] args)
{

Scanner sc=new Scanner(System.in);
int dice1;
int dice2;
  
dice1 = (int)(6.0*Math.random() + 1.0);
dice2 = (int)(6.0*Math.random() + 1.0);

int sum = dice1 + dice2;

System.out.println(\"You rolled \" + dice1 + \" + \" + dice2 + \" = \" + sum);
if (sum == 2 || sum == 3 || sum == 12)
{
System.out.println(\"You lose\ \");
System.exit(1);
}

else if (sum == 7 || sum == 11)
{
System.out.println(\"You win\ \");
System.exit(1);
}

else
{
// point: 4, 5, 6, 8, 9, or 10
int point = sum;
System.out.println(\"Point is \" + point);

while (true)
{
// keep rolling
dice1 = (int)(6.0*Math.random() + 1.0);
dice2 = (int)(6.0*Math.random() + 1.0);
sum = dice1 + dice2;
System.out.println(\"You rolled \" + dice1 + \" + \" + dice2 + \" = \" + sum);

if (sum == point)
{
System.out.println(\"You won\ \");
break; // break out of loop, a win
}
else if (sum == 7)
{
System.out.println(\"You lose\ \");
break; // break out of loop, a loss
}

}
}
}
}

/*
Output:

You rolled 5 + 1 = 6
Point is 6
You rolled 5 + 4 = 9
You rolled 2 + 3 = 5
You rolled 3 + 1 = 4
You rolled 3 + 6 = 9
You rolled 5 + 3 = 8
You rolled 6 + 1 = 7
You lose


You rolled 1 + 4 = 5
Point is 5
You rolled 1 + 3 = 4
You rolled 3 + 6 = 9
You rolled 5 + 1 = 6
You rolled 1 + 2 = 3
You rolled 5 + 3 = 8
You rolled 4 + 2 = 6
You rolled 4 + 4 = 8
You rolled 4 + 5 = 9
You rolled 1 + 5 = 6
You rolled 5 + 4 = 9
You rolled 3 + 2 = 5
You won

*/

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site