Need .java programme
Write a simulation of the Craps dice game. Craps is a dice game that revolves around rolling two six-sided dice in an attempt to roll a particular number. Wins and losses are determined by rolling the dice. This assignment gives practice for: printing, loops, variables, if-statements or switch statements, generating random numbers, methods, and classes.
Craps game rules:First roll:
The first roll (“come-out roll”) wins if the total is a 7 or 11. The first roll loses if the total is a 2, 3, or 12 (“craps”). If any of these five numbers is rolled on the first roll, tally the win or loss, and the round is over.
If none of the above is rolled on a first roll, then the value rolled is saved, i.e., a “point” is established.
Subsequent rolls:
Continue rolling the dice until either a 7 is rolled or the point is rolled.
If the point is rolled before a 7, then tally a win. If a 7 is rolled before the point, tally a loss. Then the round is over.
Program specifications:
1) Run 100,000 rounds.
2) Print out information about each round.
3) Keep track of the wins and losses for all rounds played.
4) Print out the total number of round wins and losses over 100,000 rounds.
You should print rounds exactly as shown on the next page. Only print the round information for the first 10 rounds.
The key task, which I suggest you do first, is to get your logic for the game working. After you’ve gotten this working, put this code in a loop and run it for ten or twenty rounds, keeping track of the number of wins and losses, and printing out the total wins and losses at the end. After that, the final touch will be to make the detailed round printing, and then only printing the first ten rounds. Then you can increase the total rounds played to 100,000, and you’re done!
It may be helpful to create a class to store the game information. For instance, you could create a class called CrapsRound that holds the first roll, point, result of the round, and a toString method that prints the round information in the correct format. Then you can create a class that holds an array of CrapsRound objects to simulate running many rounds
If you have any problem with the code feel free to comment.
Program
public class Test {
public static void main(String[] args) {
int total;
boolean winLoss;
int win = 0, loss = 0, point =
0;
System.out.println("*********First
10 Results*********");
for (int i = 0; i < 100000; i++)
{// running loop
total =
getScore();// getting roll score
if (total == 7
|| total == 11) {// first case of win
winLoss = true;
win++;
} else if (total
== 2 || total == 3 || total == 13) {// first case of loss
winLoss = false;
loss++;
} else {
point = total;// initializing point
while (true) {
total = getScore();//getting
roll score
if (total == point) {//second
win case
winLoss =
true;
win++;
break;
} else if (total == 7)
{//second loss case
winLoss =
false;
loss++;
break;
}
}
}
if (i <
10) {//showing first 10 result
if (winLoss)
System.out.println("Dice
Total: " + total + ", Point: " + point + " You Win");
else
System.out.println("Dice
Total: " + total + ", Point: " + point + " You Loss");
}
}
//showing total wins and loss
System.out.println();
System.out.println("Total wins: " +
win);
System.out.println("Total loss: " +
loss);
}
private static int getScore() {
//generating random dice
value
int dice1 = (int) (Math.random() *
(6 - 1) + 1);
int dice2 = (int) (Math.random() *
(6 - 1) + 1);
int total = dice1 + dice2;//adding
both dice value
return total;
}
}
Output
Get Answers For Free
Most questions answered within 1 hours.