Need help with Java for Programmers problem: Craps is a dice-based game played i
ID: 3781178 • Letter: N
Question
Need help with Java for Programmers 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.
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 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;
}
}
Explanation / Answer
GameOfCraps.java
import java.util.Scanner;
public class GameOfCraps {
public static void main(String[] args) {
// Scanner class object is used to read the numbers entered by the user
Scanner sc = new Scanner(System.in);
//Declaring variables
int result, total_games = 0, win_games = 0;
//This loop continues to execute until the user enters input other than 'Y' or 'y'
while (true) {
//Counting the no of games played
total_games++;
//Calling the method craps()
result = craps();
//if the result value is zero it displays the message "You Lost"
if (result == 0) {
System.out.println("You lost.");
}
//if the result value is one it displays the message "You Won"
else if (result == 1) {
System.out.println("You won!");
//counting the no of games won
win_games++;
}
// Getting the character from the user 'Y' or 'y' or 'N' or 'n'
System.out.print("Do you want to continue(Y/N) ::");
char ch = sc.next(".").charAt(0);
if (ch == 'Y' || ch == 'y')
continue;
else {
System.out.println("The number of wins was " + win_games
+ " out of " + total_games);
System.out.println(":: Program Exit ::");
break;
}
}
}
private static int craps() {
// Declaring variables
int count = 0, point = 0, num, dice1, dice2;
// This while loop continue to execute either player wins or lose the
// game
while (true) {
//Generating the random number between 1 and 6
dice1 = (int) (6.0 * Math.random() + 1.0);
//Generating the random number between 1 and 6
dice2 = (int) (6.0 * Math.random() + 1.0);
//calculating the sum of two dice values
num = dice1 + dice2;
/*
* If the player got sum as 7 for the first time player wins
* else player lost
*/
if (num == 7 || num==11) {
if (count == 0) {
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
return 1;
}
else if(num==7)
{
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
return 0;
}
else if(num==11)
{
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player got sum as 2 or 3 or 12 for the first time player wins
* else player continue the game
*/
else if(num==2 ||num==3 || num==12)
{
if(count==0)
{
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
return 0;
}
else {
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
count++;
continue;
}
}
/*
* If the player rolled the dice and got the number which is
* same as point Player won the game
*/
else if (point == num) {
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
return 1;
}
/*
* If the player roll the dice and got sum other than 7 for the
* first time The point is set.
*/
else {
if(count==0)
{
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
point = num;
count++;
continue;
}
else
{
System.out.println("Roll :" + dice1 + " and " + dice2 + "="+ num);
count++;
continue;
}
}
}
}
}
____________________
Output:
Roll :1 and 2=3
You lost.
Do you want to continue(Y/N) ::y
Roll :6 and 1=7
You won!
Do you want to continue(Y/N) ::y
Roll :5 and 2=7
You won!
Do you want to continue(Y/N) ::y
Roll :1 and 2=3
You lost.
Do you want to continue(Y/N) ::y
Roll :6 and 1=7
You won!
Do you want to continue(Y/N) ::y
yRoll :6 and 5=11
You won!
Do you want to continue(Y/N) ::
Roll :5 and 2=7
You won!
Do you want to continue(Y/N) ::y
Roll :1 and 4=5
Roll :6 and 5=11
Roll :6 and 5=11
Roll :1 and 5=6
Roll :6 and 5=11
Roll :2 and 4=6
Roll :3 and 5=8
Roll :5 and 6=11
Roll :2 and 3=5
You won!
Do you want to continue(Y/N) ::y
Roll :5 and 2=7
You won!
Do you want to continue(Y/N) ::y
Roll :3 and 5=8
Roll :3 and 4=7
You lost.
Do you want to continue(Y/N) ::y
Roll :1 and 3=4
Roll :5 and 6=11
Roll :5 and 4=9
Roll :6 and 5=11
Roll :5 and 4=9
Roll :3 and 3=6
Roll :1 and 1=2
Roll :4 and 5=9
Roll :1 and 4=5
Roll :5 and 5=10
Roll :4 and 4=8
Roll :1 and 3=4
You won!
Do you want to continue(Y/N) ::n
The number of wins was 8 out of 11
:: Program Exit ::
_________Thank You