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
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
Use CPP This question is about providing game logic for the game of craps we developed...
Use CPP This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game, you should ask user if wants to play another game, if so, make it happen. Otherwise quit. craps.cpp /*** This is an implementation of the famous 'Game of Chance' called 'craps'. It is a dice game. A player rolls 2 dice. Each die has...
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...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields...
Classes/ Objects(programming language java ) in software (ATOM) Write a Dice class with three data fields and appropriate types and permissions diceType numSides sideUp The class should have A 0 argument (default) constructor default values are diceType: d6, numSides: 6, sideUp: randomValue 1 argument constructor for the number of sides default values are diceType: d{numSides}, sideUp: randomValue 2 argument constructor for the number of sides and the diceType appropriate accessors and mutators *theoretical question: can you change the number of...
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...
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....
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...
After Turner finishes at the Blackjack table, you (the casino’s statistician) follow him to a craps...
After Turner finishes at the Blackjack table, you (the casino’s statistician) follow him to a craps table. In two hours of playing, he's racked up $30,000 in winnings and is showing no sign of stopping. Crowds are gathering around him to watch his streak, and he is telling everyone within earshot that his good luck is due to the fact that he's using the casino's lucky pair of "bruiser dice," so named because one is black and the other blue....
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...
   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py that has the following functions in the following order: in python Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts) Write a function scoreRound that...