Question

JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...

JAVA: MUST BE DONE IN JAVA

Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out roll results in a 2 (“snake eyes”), 3 (“ace deuce”) or 12 (“box cars”), then the shooter immediately loses the game (“craps out”). Otherwise, the game continues and the total for the coming out roll becomes the “point”. If the shooters rolls the point before rolling a 7, the shooter wins. If the shooter rolls a 7 before re-rolling the point, the shooter loses. The shooter will continue to roll until they win or lose. Refer to https://en.wikipedia.org/wiki/Craps for the basics and typical names for the sum of the die rolls. Typical play-game pseudocode:

reset game;

roll two dice and sum;

increment number of rolls;

point = sum;

if (point was a 7 or 11)

game is won;

else if (point is not 2, not 3 and not 12) {

do {

roll two dice and sum;

increment number of rolls;

value = sum;

} while (value is not point or is 7);

if (value is point)

   game is won;

else

      game is lost;

}

Output: Output will provide the analysis of running a certain number of games of Craps – presented in clear, readable and attractive manner. The results of each game (win/lose) will be recorded by the Analyzer class in order to facilitate an analysis of the game of Craps. Analysis will include the:

~ total number of games played,

~ total number of rolls for all games played,

~ summary of the number of rolls for each game to finish

(1 to 21+),

~ average length (in rolls) of the games played,

~ probability of winning (total wins/total games),

~ total number of wins that occurred on the coming out roll,

~ probability of winning on the coming out roll,

~ total number of losses that occurred on the coming out roll,

~ probability of losing on the coming out roll,

~ longest game played.

Probabilities and average game length will be computed and displayed to 5 decimal places. Expected values (sources should be documented) should be displayed with the probability results.

Input: Input will involve prompting the user for the number of games to be analyzed, an integer between 1 and 1,000,000, inclusive. Input validation is expected.

Requirements: Use only material covered in the first eight chapters. Style requirements as discussed in class expected. Efficiency should always be considered. Round only for output. Choose the most appropriate loop/decision structures and variable types. No switch or breaks statements allowed. No Magic numbers!

You must write at least three programs: one for the Die class, one for the Craps class (that uses the Die class) and one for the Analyzer (Driver/Main) class (that uses the Craps class).

The Die class will provide instance variables and methods to support the rolling of a “fair” die of any number of sides, an integer between [3-100]. The method rollDie() will access the sides variable and will return a random number between 1 and sides, inclusive. Constructor(s) and other methods, as needed. Refer to the text - section 6.9, pages 283-284 - for a similar example.

The Craps class will provide constants (i.e. for sums of die rolls), instance variables and methods to play the game. A roll/throw in the game will consist of two rollDie() method calls. Since many games will be played, in addition to Constructor(s), the Craps class should also have a resetGame() method, a playGame() method, and “getter” methods for the game status and the number of rolls needed to decide the game. Other methods, as needed. Review enumeration types – pages 206-207 in text – as these may prove useful.

The Analyzer class will prompt the user for the number games to be played and will conduct those games, gathering the required statistics from each game for eventual analysis. See above (Output) for details. The main() should represent your high-level view of the required tasks - other methods, as needed.

Homework Answers

Answer #1

The program inputs number of games to be analysed and creates a file output.txt giving details on all games played and number of games lost and win.

Driver.java

import java.io.PrintWriter;
import java.io.File;
import java.util.Scanner;

public class Driver
{
public static void main(String[] args) throws Exception
{
PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
PairOfDice d = new PairOfDice();
int n;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of games to be analysed: ");
n = scanner.nextInt();
int i = 0;
int wins = 0;
int loses = 0;
while(i<n)
{
writer.println(i+1);
d.Roll();
int mysum = d.summ();
if(mysum == 2 || mysum == 3 || mysum == 12)
{
writer.println("You Rolled " + d.toString());
writer.println("You lose!");
loses = loses + 1;
}
else if(mysum == 7 || mysum == 11)
{
writer.println("You Rolled " + d.toString());
writer.println("You win!");
wins = wins + 1;
}
else
{
writer.println("You Rolled " + d.toString());
writer.println("Point is " + mysum);
int point = mysum;
while(true)
{
d.Roll();
writer.println("You Rolled " + d.toString());
mysum = d.summ();
if(mysum == point)
{
writer.println("You win!");
wins = wins + 1;
break;
}
else if(mysum == 7)
{
writer.println("You lose!");
loses = loses + 1;
break;
}
}
}
i =i + 1;
}
writer.println("Tatal wins = " + wins);
writer.println("Tatal loses = " + loses);
writer.close();
}

}

PairOfDice.java

public class PairOfDice
{
public int d1;
public int d2;

public PairOfDice() {
}

   public void Roll()
   {
   this.d1 = (int )(Math.random() * 6 + 1);
   this.d2 = (int )(Math.random() * 6 + 1);      
   }

   public String toString()
   {
String r;
   r = this.d1 + " + " + this.d2 + " = " + (this.d1 + this.d2) ;
   return r;
   }

   public int summ()
   {
   return (this.d1 + this.d2);      
   }

}

Output.txt

1
You Rolled 5 + 1 = 6
Point is 6
You Rolled 2 + 5 = 7
You lose!
2
You Rolled 1 + 5 = 6
Point is 6
You Rolled 2 + 6 = 8
You Rolled 1 + 5 = 6
You win!
3
You Rolled 5 + 2 = 7
You win!
4
You Rolled 5 + 3 = 8
Point is 8
You Rolled 5 + 1 = 6
You Rolled 6 + 3 = 9
You Rolled 5 + 4 = 9
You Rolled 4 + 5 = 9
You Rolled 5 + 5 = 10
You Rolled 2 + 5 = 7
You lose!
5
You Rolled 2 + 5 = 7
You win!
6
You Rolled 5 + 6 = 11
You win!
7
You Rolled 6 + 3 = 9
Point is 9
You Rolled 5 + 6 = 11
You Rolled 4 + 4 = 8
You Rolled 3 + 2 = 5
You Rolled 3 + 3 = 6
You Rolled 3 + 2 = 5
You Rolled 3 + 2 = 5
You Rolled 6 + 4 = 10
You Rolled 2 + 2 = 4
You Rolled 2 + 2 = 4
You Rolled 6 + 6 = 12
You Rolled 6 + 6 = 12
You Rolled 4 + 5 = 9
You win!
8
You Rolled 2 + 4 = 6
Point is 6
You Rolled 1 + 6 = 7
You lose!
9
You Rolled 6 + 3 = 9
Point is 9
You Rolled 6 + 3 = 9
You win!
10
You Rolled 4 + 1 = 5
Point is 5
You Rolled 6 + 6 = 12
You Rolled 5 + 2 = 7
You lose!
Tatal wins = 6
Tatal loses = 4

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
What is the total probability of rolling a natural on the come-out roll in craps? 2/36...
What is the total probability of rolling a natural on the come-out roll in craps? 2/36 4/36 6/36 8/36 If a player’s point in craps is 8, what is the probability that they will win by rolling another 8 before a 7? 5/36 6/36 5/11 6/11 Assume that rolling an 8 before a 7 will pay at 6-to-5 odds net. Find the net payoff on a $5 wager. $1.20 $5 $6 $11 Find the expected number of 5’s from 100...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
Bunco is a group dice game that requires no skill. The objective of the game is...
Bunco is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations. The game is played with three dice, but we will consider a simpler version involving only two dice. How do you play two dice Bunco? There are six rounds, one for each of the possible outcomes in a die, namely the numbers one through six. Going clockwise, players take turns rolling two dice trying to score points....
The game requires $5 to play, once the player is admitted, he or she has the...
The game requires $5 to play, once the player is admitted, he or she has the opportunity to take a chance with luck and pick from the bag. If the player receives a M&M, the player loses. If the player wins a Reese’s Pieces candy, the player wins. If the player wins they may roll a dice for a second turn, if the die rolls on a even number, they may pick from the bag once again with no extra...
To play the 7-11 game at a gambling casino one must pay $1. if one rolls...
To play the 7-11 game at a gambling casino one must pay $1. if one rolls a sum of 7 or a sum of 11 one wins $5, since one paid $1 to play the net gain is $4. for all other sums rolled the net gain is -$1. a. Play the game for 36 rolls of two dice. Record the sums you rolled in the chart provided. Sum: 2 3 4 5 6 7 8 9 10 11 12...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s wants and needs for goods and services seem to be unlimited. (2 points) 2.) (1 point) Adam Smith’s “invisible hand” refers to a.) the subtle and often hidden methods that businesses use to profit at consumers’ expense. b.) the ability of free markets to reach desirable outcomes, despite the self-interest of market participants. c.) the ability of government regulations to benefit consumers, even if...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT