Select Page

I need help debugging a Java program. There are 3 classes. Please only make changes to the Player class, and explain the changes you are making. The other 2 classes are provided for testing purposes. Please use “IntelliJ IDEA 15 CE” to work on this program. Also be sure to read through the entire instructions file provided as an attachment. The 3 classes are provided as a word document attachment; please copy and paste them into IntelliJ. Thank you.instructions.docxclass_game.docxclass_project01studenttest.docxclass_player.docxLearning Objectives
1 Simple arithmetic operations
2 Using conditionals and primitive types
3 Using loops
4 Using Scanner
Introduction
Three friends Sophia, Riya and Nicole are traveling for a break. Waiting at
the airport, Riya says: “I wish we had 5 dice! I know a very nice game that
could make the time fly faster and allow us to have some fun.” Sophia and
Nicole do not have the dice; but Sophia has her laptop and she realizes
that she could use her fresh Java skills to implement the game. So Riya
explains the game to Sophia and Nicole.
Goal
▪ Be the first to score at least 5000 points
Game Rules
▪ The game is played in rounds. In each round one player plays, then the
next player plays her round and so forth until a player wins. The
number of players and the order in which they play are fixed at the
start of the game. Since we are three of us let’s follow the order:
Sophia, Riya and Nicole.
▪ The game ends when a player wins. A player wins when her total score is
at least 5000 points.
▪ One round is executed as follows:
1
The player launches 5 dice. Each die has 6 sides with numbers 1–
6.
2
If there are no 1s or 5s, the player stops the round and scores a 0
for that round. Otherwise, for each 1 the player adds to her
round score 100 points, and for each 5 she adds 50 points.
3
If the current round score is a multiple of 100, the player has two
choices: stop the round and increase her cumulative score by
the value of the current round score OR relaunch the dice that
didn’t previously score.
4
If the current round score is not a multiple of 100, then the player
MUST relaunch the dice which didn’t score.
5
Once every die is scored the player MUST relaunch all of them
again, and the game continues.
6
If at any point the player relaunches the dice that were not scored
and does not roll any 1s or 5s, then the player loses all of the
points accumulated for that round, i.e., the player gets a 0 for
that round.
Examples:
1 Example 1:

Sophia launches and gets 1, 1, 5, 2, 6. Her round score is now 250
points. Since 250 is not a multiple of 100 she relaunches the
dice that scored 2 and 6.

Now she gets 5, 6. Her updated round score is 250 + 50 = 300. She
can stop and score 300 points OR relaunch the die that scored
6.
2 Example 2:

Sophia launches and gets 1, 1, 5, 2, 6. Her round score is now 250
points. She relaunches the dice that scored 2 and 6.

Now she get 3, 6. Since she didn’t roll any 1s or 5s during this
launch her round score is 0.
Project Details
This project consists of two classes:
1 Player: it keeps all the information about the player and executes one
round of the game. You have to implement the following methods
within the Player class. Description for each of these methods is
given in the subsequent section.
a
public Player(String name)
b
public String throwDice(int nbDice)
c
public int nbDiceScored(String diceValues)
d
public int countPoints(String diceValues)
e
public boolean continueRound(int nbDice)
f
public boolean isWinner()
g
public void playRound()
2 Game: it creates the Players and makes them play. This class is
Player
The class Player stores the name and score of a Player. Each player also
has a set of dice simulated with a Random object.
▪ Instance Variables: Let us give names to our instance variables. DO
NOT declare these instance variables to be private for this project. If
you don’t know what that means, don’t worry about it for now.
Description
Type
variableName
Name
String
name
Cumulative Score
int
score
Dice
Random
dice
▪ String constants: In the various methods of this class, you are required
to print messages to the standard output. In order to follow a common
convention throughout this project and to facilitate auto-grading, you
MUST use the following string constants in the Player class.
Download them and then copy and paste them in the Player class.
constants.txt
private static final String LOSE_MESSAGE = “tDice values: %s, Sorry, you
scored 0 for the roundnn”;
private static final String THROW_DICE_MESSAGE = “tDice values: %s, %d +
%d = %d points!n”;
private static final String ALL_DICE_SCORED_MESSAGE = “tYou have to
relaunch the %d dicenn”;
private static final String NOT_MULTIPLE_OF_100_MESSAGE = “tYou have
to continue, ”
+ “the score is not a multiple of 100nn”;
private static final String CONTINUE_MESSAGE = “tContinue with %d dice?
Current score: %d points (Y/N)n”;
private static final String ROUND_SCORE_MESSAGE = “tSuper you scored
%d for the round!! Your new score is %dnn”;
While printing the above String messages, use System.out.printf(). DO
NOT use System.out.println() even if no formatting is needed. It will add an
extra newline.
▪ Player( ): This method has the same name as that of the class and is
called a constructor. It creates an object instance of the class and
initializes the instance variables of the newly created object. The
constructor, being a method, can have parameters. In our case it only
takes the name of the player.
public Player(String name) {
this.dice = new Random();
// initialize other instance variables
}
Playing one round is quite complex, so let’s create four auxiliary methods:
▪ throwDice(): this method simulates the throw of the dices and returns a
String representing the result. To simulate throwing the dice, use the
dice instance variable. This will allow you to generate random
numbers.
public String throwDice(int nbDice) {
String diceVal = “”;
// Simulate dice throw
return diceVal;
}
The format of the return string is the dice values separated by a space. If
nbDice is 4 then randomly generate four integers in [1, 6] and return them
as a String separated by space. For example, if the numbers generated are
2, 4, 2 and 5 then return the string “2 4 2 5 ”. Note the space after the last
5.
▪ nbDiceScored(): this method returns the number of dice that scored a 1
or a 5. The input value is the output of the method throwDice().
public int nbDiceScored(String diceValues) {
int val = 0;
return val;
}
A Scanner can be used on a string to read the integers in it.
Examples:
1 If the string diceValues is “2 4 2 5 ” return a 1
2 If the string diceValues is “6 6 ” return a 0
3 If the string diceValues is “1 ” return a 1
▪ countPoints(): This method takes the diceValues as input and returns the
score. Remember, a die with a value of 1 scores 100 points, and a
die with the value of 5 scores 50 points.
public int countPoints(String diceValues) {
int val = 0;
// count points
return val;
}
Examples:
1 If the string diceValues is “2 4 2 5 ” return 50
2 If the string diceValues is “6 6 ” return 0
3 If the string diceValues is “1 ” return 100
▪ continueRound(): This method prompts the player to know if the Player
wants to continue. This is done using the string constant
CONTINUE_MESSAGE.
public boolean continueRound(int nbDice) {
// Code here
}
For example, if the method is called with nbDice = 2 then it displays the
following message:
Continue with 2 dice? Current score 3400 points. (Y/N)
This method returns true if the player enters Y, or false if the player enters
N. If the player enters something else then the method prompts again.
▪ isWinner(): This method returns true when the player score is greater or
equal to 5000 points, false otherwise.
public boolean isWinner() {
}
Now let’s write the method for playing a complete round:
▪ playRound(): This method implements the logic of one round of the
game. It uses the auxiliary methods (i.e., the other methods that you
have written) and prints information on the screen for the user.
Remember to use the strings that are provided for printing
messages.
public void playRound() {
//Code here
}
In the playRound() method you have to do the following:
1 Launch the dice.
2 Calculate the launch score.
3 If the launch score is 0 then stop and print a message using
LOSE_MESSAGE. The score for this round is 0.
4 Update round score using the launch score and print the scores using
THROW_DICE_MESSAGE.
5 Update the number of dice to be used for the next launch in the round.
6 If all 5 dice have scored then print a message using
ALL_DICE_SCORED_MESSAGE and go back to Step 1 (with all 5 dice)
7 If the round score is a multiple of 100 then prompt the player and ask if
the player wants to continue.
a
If the player wants to continue then go back to Step 1 (with the
proper number of dice).
b
Else update the player’s total score by adding the player’s round
score and stop the round after printing a message using
ROUND_SCORE_MESSAGE.
8 Else go back to Step 1 (with the proper number of dice) after printing a
message using NOT_MULTIPLE_OF_100_MESSAGE
Example 1:
Dice values: 3 3 5 1 3 , 0 + 150 = 150 points!
You have to continue, the score is not a multiple of 100
Dice values: 1 2 1 , 150 + 200 = 350 points!
You have to continue, the score is not a multiple of 100
Dice values: 1 , 350 + 100 = 450 points!
You have to relaunch the 5 dice
Dice values: 3 1 4 5 2 , 450 + 150 = 600 points!
Continue with 3 dice? Current score: 0 points (Y/N)
u
Continue with 3 dice? Current score: 0 points (Y/N)
N
Super you scored 600 for the round!! Your new score is 600
Example 2:
Dice values: 5 5 4 3 3 , 0 + 100 = 100 points!
Continue with 3 dice? Current score: 0 points (Y/N)
Y
Dice values: 3 2 3 , Sorry, you score 0 for the round
Game
The Game class implements the main method. There are 3 players with the
names Sophia, Riya and Nicole. Run this class to play the game. You may
modify this class if you feel it will help you to better test the program.
public class Game {
public static void main(String[] args) {
Player sophia = new Player(“Sophia”);
Player riya = new Player(“Riya”);
Player nicole = new Player(“Nicole”);
while (true) {
System.out.printf(“Sophia plays:n”);
sophia.playRound();
if (sophia.isWinner()) {
System.out.printf(“nSophia wins!!n”);
return;
}
System.out.print(“Riya plays:n”);
riya.playRound();
if (riya.isWinner()) {
System.out.printf(“nRiya wins!!n”);
return;
}
System.out.printf(“Nicole plays:n”);
nicole.playRound();
if (nicole.isWinner()) {
System.out.printf(“nNicole wins!!n”);
return;
}
}
}
}
Setup
1 Create a new project in IntelliJ.
project.
4 Create a new class called Player and start coding.
import java.util.Scanner;
public class Player {
public Player(String name) {
Player sophia = new Player(“Sophia”);
Player riya = new Player(“Riya”);
Player nicole = new Player(“Nicole”);
}
public String throwDice(int nbDice) {
String diceVal = “”;
int die1;
int die2;
int die3;
int die4;
int die5;
die1 = (int) (Math.random() * 6) + 1;
die2 = (int) (Math.random() * 6) + 1;
die3 = (int) (Math.random() * 6) + 1;
die4 = (int) (Math.random() * 6) + 1;
die5 = (int) (Math.random() * 6) + 1;
diceVal = (die1 + ” ” + die2 + ” ” + die3 + ” ” + die4 + ” ” + die5 + ” “);
return diceVal;
}
public int nbDiceScored(String diceValues) {
int val = 0;
Scanner scan = new Scanner(diceValues);
if (diceValues.equals(“1”))
val += 1;
if (diceValues.equals(“5”))
val += 1;
return val;
}
public int countPoints(String diceValues) {
int val = 0;
if (diceValues.equals(“1”))
val += 100;
if (diceValues.equals(“5”))
val += 50;
return val;
}
public boolean continueRound(int nbDice, int countPoints) {
if (nbDice >= 1 && nbDice = 5000)
return true;
else {
return false;
}
}
public void playRound(int score, int diceVal, int countPoints, int nbDice) {
int die1;
int die2;
int die3;
int die4;
int die5;
die1 = (int) (Math.random() * 6) + 1;
die2 = (int) (Math.random() * 6) + 1;
die3 = (int) (Math.random() * 6) + 1;
die4 = (int) (Math.random() * 6) + 1;
die5 = (int) (Math.random() * 6) + 1;
if (die1 == 1)
score += 100;
if (die2 == 1)
score += 100;
if (die3 == 1)
score += 100;
if (die4 == 1)
score += 100;
if (die5 == 1)
score += 100;
if (die1 == 5)
score += 50;
if (die2 == 5)
score += 50;
if (die3 == 5)
score += 50;
if (die4 == 5)
score += 50;
if (die5 == 5)
score += 50;
if (score == 0)
System.out.printf(“Dice values: ” + (diceVal) + ” Sorry, you scored 0 for the round”);
if ((die1 == 1 | die1 == 5) && (die2 == 1 | die2 == 5) && (die3 == 1 | die3 == 5) && (die4 == 1 | die4 == 5) &&
(die5 == 1 | die5 == 5))
System.out.printf(“You have to relaunch the dice”);
if (score % 100 == 0)
System.out.printf(“Continue with ” + (nbDice) + ” dice? Current score: ” + (countPoints) + ” points (Y/N)”);
if (score % 100 != 0)
System.out.printf(“Super you scored ” + (countPoints) + ” for the round!! Your new score is ” + (score));
System.out.printf(“You have to continue, the score is not a multiple of 100”);
}
}
import static org.junit.Assert.*;
import org.junit.*;
// imports for IO, TestDice
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Random;
import java.util.Scanner;
public class Project01StudentTest {
// given messages (with new lines removed)
private static final String LOSE_MESSAGE = “tDice values: %s, Sorry, you scored 0 for the round”;
private static final String THROW_DICE_MESSAGE = “tDice values: %s, %d + %d = %d points!n”;
private static final String ALL_DICE_SCORED_MESSAGE = “tYou have to relaunch the %d dice”;
private static final String NOT_MULTIPLE_OF_100_MESSAGE = “tYou have to continue, ”
+ “the score is not a multiple of 100”;
private static final String CONTINUE_MESSAGE = “tContinue with %d dice? Current score: %d points (Y/N)n”;
private static final String ROUND_SCORE_MESSAGE = “tSuper you scored %d for the round!! Your new score is
%d”;
// standard I/O
private InputStream stdin;
private PrintStream stdout;
// Player for testing
private Player p;
private String name;
@Before
public void setUp() throws Exception {
name = “John Doe”;
p = new Player(name);
stdin = System.in;
stdout = System.out;
}
@After
public void cleanUp() {
System.setIn(stdin);
System.setOut(stdout);
}
// Test case for constructor
@Test (timeout = 100)
// @ScoringWeight(.05)
public void testConstructor() {
String message1 = “Player(): Be sure you properly set the player’s name”;
String message2 = “Player(): Be sure you properly initialized the player’s score”;
String message3 = “Player(): Be sure you properly initialize your dice”;
assertEquals(message1, p.name, name);
assertEquals(message2, p.score, 0);
// check initialization of dice by calling a method
try {
p.dice.nextInt();
} catch (Exception e) {
assertTrue(message3, false);
}
}
// Test cases for throwDice() method
@Test(timeout = 100)
// @ScoringWeight(.05)
public void testThrowDiceCorrectAmount() {
String message = “throwDice(): Be sure you roll as many dice as are passed to the method”;
String exceptionMessage = “throwDice(): Be sure your method returns a String”;
String dice = “”;
int nDice = 100;
try {
dice = p.throwDice(nDice);
} catch (Exception e) {
assertTrue(exceptionMessage, false);
}
// count number of dice rolls in String returned from throwDice()
Scanner scan = new Scanner(dice);
int diceFound = 0;
while (scan.hasNext()) {
int temp = scan.nextInt();
diceFound++;
}
assertEquals(message, nDice, diceFound);
}
@Test(timeout = 100)
// @ScoringWeight(.05)
public void testThrowDiceCheckRange() {
String message = “throwDice(): Be sure your method’s dice values in the proper range of 1-6”;
String exceptionMessage = “throwDice(): Be sure your method returns a String”;
String dice = “”;
int nDice = 100;
boolean inRange = true;
try {
dice = p.throwDice(nDice);
} catch (Exception e) {
assertTrue(exceptionMessage, false);
}
// check for dice in range 1-6
Scanner scan = new Scanner(dice);
while (inRange && scan.hasNext()) {
int temp = scan.nextInt();
inRange = temp > 0 && temp < 7; } assertTrue(message, inRange); } // Test cases for nbDiceScored() method @Test (timeout = 100) // @ScoringWeight(.075) public void testNbDiceScoredCorrectDice() { String message = "nbDicesScored(): Make sure you are counting all 1s and 5s"; String dice = "1 2 3 4 5 6 5 4 3 2 1 "; int nScored = 4; assertEquals(message, nScored, p.nbDiceScored(dice)); } @Test(timeout = 100) // @ScoringWeight(.075) public void testNbDiceScoredIncorrectDice() { String message = "nbDicesScored(): Make sure you are only counting 1s and 5s"; String dice = "2 3 4 6 4 3 2 "; int nScored = 0; assertEquals(message, nScored, p.nbDiceScored(dice)); } // Test cases for countPoints() method @Test(timeout = 100) // @ScoringWeight(.0375) public void testCountPoints1() { String dice = "1 "; String message = "countPoints(): Be sure to count 1s as 100pts each"; int score = 100; assertEquals(message, score, p.countPoints(dice)); } @Test(timeout = 100) // @ScoringWeight(.0375) public void testCountPoints5() { String dice = "5 "; String message = "countPoints(): Be sure to count 5s as 50pts each"; int score = 50; assertEquals(message, score, p.countPoints(dice)); } @Test(timeout = 100) // @ScoringWeight(.0375) public void testCountPoints0() { String dice = "2 3 4 6 "; String message = "countPoints(): Be sure you are only counting points for 1s and 5s"; int score = 0; assertEquals(message, score, p.countPoints(dice)); } @Test(timeout = 100) // @ScoringWeight(.0375) public void testCountPointsCombination() { String dice = "1 1 5 5 6 5 "; String message = "countPoints(): Be sure you are adding the scores together properly"; int score = 350; assertEquals(message, score, p.countPoints(dice)); } // Test cases for continueRound() method @Test(timeout = 100) // @ScoringWeight(.0625) public void testContinueRoundPrompt() { String message = "continueRound(): Be sure to prompt the user for input"; String data = "Y"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); int score = p.score; p.continueRound(1); // check that the prompt to the user was output assertEquals(message, String.format(CONTINUE_MESSAGE, 1, score), output.toString()); } catch (Exception e) { String exceptionMessage = "continueRound(): Be sure your method handles all I/O properly"; assertTrue(exceptionMessage, false); } } @Test(timeout = 100) // @ScoringWeight(.0625) public void testContinueRoundAffirmative() { String message = "continueRound(): 'Y' should be affirmative input"; String data = "Y"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); assertTrue(message, p.continueRound(1)); } catch (Exception e) { String exceptionMessage = "continueRound(): Be sure your method handles all I/O properly"; assertTrue(exceptionMessage, false); } } @Test(timeout = 100) // @ScoringWeight(.0625) public void testContinueRoundNegative() { String message = "continueRound(): 'N' should be negative input"; String data = "N"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); assertFalse(message, p.continueRound(1)); } catch (Exception e) { String exceptionMessage = "continueRound(): Be sure your method handles all I/O properly"; assertTrue(exceptionMessage, false); } } @Test(timeout = 100) // @ScoringWeight(.0625) public void testContinueRoundValidation() { String message1 = "continueRound(): Be sure to check for valid input"; String message2 = "continueRound(): Be sure to check for valid input until you get some"; String data = "abcd nefghijk nlmnopqrstuvwx nY"; try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); boolean b = p.continueRound(1); assertTrue(message2, b); } catch (Exception e) { assertTrue(message1, false); } } // Test cases for playRound() method @Test(timeout = 100) // @ScoringWeight(.05) public void testPlayRoundLaunchDice() { String message = "playRound(): Be sure to begin by launching the dice.n"; message = message + "THROW_DICE_MESSAGE may be missing or improperly formatted.n"; message = message + "Make sure you updated the round score correctly."; String message2 = "playRound(): Be sure to identify when the round score is zero."; String data = "N"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 6 5 4 "); p.playRound(); // check for launched dice message Scanner scan = new Scanner(output.toString()); String launchLine = scan.nextLine() + "n"; String expectedLine = String.format(THROW_DICE_MESSAGE, "1 2 3 4 5 ", 0, 150, 150); assertEquals(message, expectedLine, launchLine); // Reset output stream for second case: zero score output = new ByteArrayOutputStream(); System.setOut(new PrintStream(output)); p.dice = new TestDice("2 3 4 6 4 "); p.playRound(); // check for zero round message scan = new Scanner(output.toString()); launchLine = ""; while (launchLine.equals("")) launchLine = scan.nextLine(); expectedLine = String.format(LOSE_MESSAGE, "2 3 4 6 4 "); assertEquals(message2, expectedLine, launchLine); } catch (Exception e) { assertTrue(message, false); } } @Test(timeout = 100) // @ScoringWeight(.05) public void testPlayRoundContinue() { String message1 = "playRound(): Be sure round continues when round score is not multiple of 100"; String message2 = "playRound(): Be sure to ask user to continue when round score is multiple of 100."; String message3 = "playRound(): Be sure to continue when the user tells you to"; message1 = message1 + "This test case may have also failed if the above test case failed."; message2 = message2 + "This test case may have also failed if the above test case failed."; message3 = message3 + "This test case may have also failed if the above test case failed."; String data = "N"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 2 3 4 "); p.playRound(); // look for "not multiple of 100" continue message Scanner scan = new Scanner(output.toString()); scan.nextLine(); assertEquals(message1, NOT_MULTIPLE_OF_100_MESSAGE, scan.nextLine()); //Reset output stream for second case: must ask user to continue output = new ByteArrayOutputStream(); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 5 5 "); p.playRound(); scan = new Scanner(output.toString()); scan.nextLine(); String actualLine = scan.nextLine() + "n"; assertEquals(message2, String.format(CONTINUE_MESSAGE, 2, 0), actualLine); //Reset output stream for third case: user wants to continue data = "Y"; output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 5 5 2 3 "); p.playRound(); scan = new Scanner(output.toString()); scan.nextLine(); scan.nextLine(); actualLine = scan.nextLine(); while (actualLine.equals("")) actualLine = scan.nextLine(); assertEquals(message3, String.format(LOSE_MESSAGE, "2 3 "), actualLine); } catch (Exception e) { assertTrue("playRound(): an exception was thrown", false); } } @Test(timeout = 100) // @ScoringWeight(.05) public void testPlayRoundUpdateDice() { String data = "N"; String message1 = "playRound(): Be sure you are updating the number of dice properly.n"; String message2 = "playRound(): Be sure to relaunch the dice if all are scored.n"; String message3 = "playRound(): Be sure you continue updating the number of dicen"; message1 = message1 + "This test case may have also failed if the above test cases failed."; message2 = message2 + "This test case may have also failed if the above test cases failed."; message3 = message3 + "This test case may have also failed if the above test cases failed."; try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 2 3 4 "); p.playRound(); // search for appropriate lose message at end of round Scanner scan = new Scanner(output.toString()); String updateLine1 = scan.nextLine(); updateLine1 = scan.nextLine(); updateLine1 = scan.nextLine(); while (updateLine1.equals("")) updateLine1 = scan.nextLine(); String expectedLine1 = String.format(LOSE_MESSAGE, "2 3 4 "); assertEquals(message1, expectedLine1, updateLine1); //Reset output stream for second case: all dice score, subsequent zero score output = new ByteArrayOutputStream(); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 1 1 5 2 3 4 6 4 "); p.playRound(); // search for all-dice scored message scan = new Scanner(output.toString()); scan.nextLine(); scan.nextLine(); String actualLine = ""; while (actualLine.equals("")) actualLine = scan.nextLine(); actualLine = scan.nextLine(); assertEquals(message2, String.format(ALL_DICE_SCORED_MESSAGE, 5), actualLine); // search for lose message at end of round actualLine = ""; while (actualLine.equals("")) actualLine = scan.nextLine(); assertEquals(message2, String.format(LOSE_MESSAGE, "2 3 4 6 4 "), actualLine); //Reset output stream for third case: all dice score, subsequent roll scores output = new ByteArrayOutputStream(); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 1 1 5 1 2 3 4 5 2 3 4 "); p.playRound(); // search for lose message at end of round scan = new Scanner(output.toString()); scan.nextLine(); scan.nextLine(); while (scan.nextLine().equals("")); scan.nextLine(); scan.nextLine(); while (scan.nextLine().equals("")); scan.nextLine(); actualLine = scan.nextLine(); while (actualLine.equals("")) actualLine = scan.nextLine(); assertEquals(message3, String.format(LOSE_MESSAGE, "2 3 4 "), actualLine); //stdout.println(output.toString()); } catch (Exception e) { assertTrue("playRound(): an exception was thrown", false); } } @Test(timeout = 100) // @ScoringWeight(.05) public void testPlayRoundUpdateScore() { String message = "playRound(): Be sure to update the score properly."; message = message + "This test case may have also failed if the above test cases failed."; String data = "N"; // "user input" try { // replace input/output streams ByteArrayOutputStream output = new ByteArrayOutputStream(); System.setIn(new ByteArrayInputStream(data.getBytes())); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 1 1 5 2 3 4 6 4 "); p.playRound(); // check for 0 score assertEquals(message, 0, p.score); //Reset output output = new ByteArrayOutputStream(); System.setOut(new PrintStream(output)); p.dice = new TestDice("1 2 3 4 5 5 3 1 "); p.playRound(); // look for round score message at end of round Scanner scan = new Scanner(output.toString()); String actualLine = ""; while (!(actualLine = scan.nextLine()).equals("")); while ((actualLine = scan.nextLine()).equals("")); scan.nextLine(); assertEquals(message, String.format(ROUND_SCORE_MESSAGE, 300, 300), scan.nextLine()); assertEquals(message, 300, p.score); } catch (Exception e) { assertTrue("playRound(): an exception was thrown", false); } } // Test cases for isWinner() method @Test(timeout = 100) // @ScoringWeight(.025) public void testIsWinnerThreshold() { String message1 = "isWinner(): Check that you have a threshold of 5000 points"; p.score = 4999; assertFalse(message1, p.isWinner()); p.score = 5000; assertTrue(message1, p.isWinner()); } @Test(timeout = 100) // @ScoringWeight(.025) public void testIsWinnerExceedsThreshold() { String message = "isWinner(): Check that you consider exceeding the threshold as winning the game"; p.score = 5001; assertTrue(message, p.isWinner()); } // TestDice to replace random dice in students submissions private class TestDice extends Random { private Scanner scan; public TestDice(String s) { scan = new Scanner(s); } public int nextInt(int x) { if (scan.hasNext()) return scan.nextInt() - 1; else return -1; } } } import java.util.Scanner; public class Player { public Player(String name) { Player sophia = new Player("Sophia"); Player riya = new Player("Riya"); Player nicole = new Player("Nicole"); } public String throwDice(int nbDice) { String diceVal = ""; int die1; int die2; int die3; int die4; int die5; die1 = (int) (Math.random() * 6) + 1; die2 = (int) (Math.random() * 6) + 1; die3 = (int) (Math.random() * 6) + 1; die4 = (int) (Math.random() * 6) + 1; die5 = (int) (Math.random() * 6) + 1; diceVal = (die1 + " " + die2 + " " + die3 + " " + die4 + " " + die5 + " "); return diceVal; } public int nbDiceScored(String diceValues) { int val = 0; Scanner scan = new Scanner(diceValues); if (diceValues.equals("1")) val += 1; if (diceValues.equals("5")) val += 1; return val; } public int countPoints(String diceValues) { int val = 0; if (diceValues.equals("1")) val += 100; if (diceValues.equals("5")) val += 50; return val; } public boolean continueRound(int nbDice, int countPoints) { if (nbDice >= 1 && nbDice = 5000)
return true;
else {
return false;
}
}
public void playRound(int score, int diceVal, int countPoints, int nbDice) {
int die1;
int die2;
int die3;
int die4;
int die5;
die1 = (int) (Math.random() * 6) + 1;
die2 = (int) (Math.random() * 6) + 1;
die3 = (int) (Math.random() * 6) + 1;
die4 = (int) (Math.random() * 6) + 1;
die5 = (int) (Math.random() * 6) + 1;
if (die1 == 1)
score += 100;
if (die2 == 1)
score += 100;
if (die3 == 1)
score += 100;
if (die4 == 1)
score += 100;
if (die5 == 1)
score += 100;
if (die1 == 5)
score += 50;
if (die2 == 5)
score += 50;
if (die3 == 5)
score += 50;
if (die4 == 5)
score += 50;
if (die5 == 5)
score += 50;
if (score == 0)
System.out.printf(“Dice values: ” + (diceVal) + ” Sorry, you scored 0 for the round”);
if ((die1 == 1 | die1 == 5) && (die2 == 1 | die2 == 5) && (die3 == 1 | die3 == 5) && (die4 == 1 | die4 == 5) &&
(die5 == 1 | die5 == 5))
System.out.printf(“You have to relaunch the dice”);
if (score % 100 == 0)
System.out.printf(“Continue with ” + (nbDice) + ” dice? Current score: ” + (countPoints) + ” points (Y/N)”);
if (score % 100 != 0)
System.out.printf(“Super you scored ” + (countPoints) + ” for the round!! Your new score is ” + (score));
System.out.printf(“You have to continue, the score is not a multiple of 100”);
}
}

attachment

#### Why Choose Us

• 100% non-plagiarized Papers
• Affordable Prices
• Any Paper, Urgency, and Subject
• Will complete your papers in 6 hours
• On-time Delivery
• Money-back and Privacy guarantees
• Unlimited Amendments upon request
• Satisfaction guarantee

#### How it Works

• Click on the “Place Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
• Fill in your paper’s requirements in the "PAPER DETAILS" section.