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.
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
Get Answers For Free
Most questions answered within 1 hours.