Question: Need help with Intro to Java problem: Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The

Need help with Intro to Java problem:

Craps is a dice-based game played in many casinos. Like blackjack, a player plays against the house. The game starts with the player throwing a pair of (that is, two distinct) standard, six-sided dice. (Note that throwing a single 12-sided die will not result in the same probabilities of winning/losing. Don't do this!) If the player rolls a total of 7 or 11 in the first round, the player wins. If the player rolls a total of 2, 3, or 12 in the first round, the player loses. For all other roll values, the player must roll again to determine whether he/she has won or lost. In the second and subsequent rounds the player rolls the pair of dice again. If the player matches the roll value from the first round again, she/he wins. If the player rolls a 7, he/she loses. Play continues into another round until the initial roll is matched (for a win) or a 7 is rolled (for a loss).

Write a craps() function that plays a game of craps as described above. The function should continue rolling as described in the rules above until it has determined that the player has won or lost. If the function determines that the player has lost, it should return 0. If the function determines that the player has won, it should return 1. The main method I have provided calls the craps() function and reports the outcome based on what the function returns. It also tracks how many rounds the player has won and how many the player has lost. Do not change the main() method. Your only task is to correcly implement the craps() function.

Note: The craps function needs to always return a value, which means that you cant have return statements only in branches. If you return in branches, you also need to have a return of some bogus value at the very end of the function. Java enforces that functions that return integers (or anything other than void) actually return something and branching statements can mess things up.

The following is some sample output from my solution. Please remember that this game involves randomness so that your precise test cases will likely differ from mine. But you should follow the rules described above in all cases:

Roll: 2 and 5 = 7 You won! Roll: 2 and 3 = 5 Roll: 2 and 5 = 7 You lost. Roll: 6 and 6 = 12 You lost. Roll: 4 and 6 = 10 Roll: 5 and 6 = 11 Roll: 3 and 4 = 7 You lost. Roll: 1 and 6 = 7 You won! Roll: 4 and 6 = 10 Roll: 5 and 2 = 7 You lost. Roll: 4 and 6 = 10 Roll: 1 and 5 = 6 Roll: 6 and 4 = 10 You won! Roll: 3 and 5 = 8 Roll: 4 and 2 = 6 Roll: 3 and 2 = 5 Roll: 5 and 5 = 10 Roll: 4 and 5 = 9 Roll: 2 and 2 = 4 Roll: 6 and 3 = 9 Roll: 2 and 3 = 5 Roll: 3 and 5 = 8 You won! Roll: 6 and 5 = 11 You won! Roll: 5 and 5 = 10 Roll: 6 and 5 = 11 Roll: 1 and 1 = 2 Roll: 3 and 1 = 4 Roll: 3 and 6 = 9 Roll: 1 and 1 = 2 Roll: 1 and 4 = 5 Roll: 5 and 5 = 10 You won! The number of wins was 6 out of 10.

______________________________________________________________________________________________________

This is what I have so far:

public class Craps {

public static void main(String[] args) {

int num = 10; int n = num; int total = 0; int ans; while (n > 0) { ans = craps(); if (ans == 1) System.out.println("You won!"); else System.out.println("You lost."); total += ans; System.out.println(); n--; } System.out.println("The number of wins was " + total + " out of " + num + ".");

} // Write this method public static int craps() { // A stub -- remove when you solve the assignment return 0; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!