a. Now modify the loop again so that it no longer stops rolling the first time y
ID: 3533318 • Letter: A
Question
a. Now modify the loop again so that it no longer stops rolling the first time you get an initial roll of 2, but instead stops the first time any initial roll is matched in exactly 1 attempt.
Hint: You want to exit when the number of attempts made is exactly 1, so you want to continue as long as the opposite condition is true. Find the variable that counts the number of attempts and use it as the LCV.
package Dominoes2;
import java.util.Random;
/**
* A class to represent a standard 6-sided die
*/
class Die // die is the singular of "dice"
{
// random number generator object
private static Random r = new Random();
/**
* Roll one die.
*
* @return a random integer from 1 to 6
*/
public int roll()
{
return r.nextInt(6) + 1;
}
}
/**
* Plays the game. I.e., rolls the dice to get the initial roll and counts
* number of additional rolls required to match it. Repeats as long as the user
* wants to play another game
*/
class MatchGame2
{
// instance var's are Die objects
Die d1 = new Die(); // create pair of dice
Die d2 = new Die();
// This method just aligns the output...
private static String format(int number)
{
if (number <= 9) // if single-digit number...
{
return " " + number; // ...pad with one leading space
}
else // 2-digit number...
{
return "" + number; // ...no padding
}
}
/**
* Plays the game. I.e., rolls the dice to get the initial roll and counts
* number of additional rolls required to match it. Repeats as long as the
* user wants to play another game
*/
public void play()
{
int initialRoll = 0; // total of first roll of two dice
int rollCount; // counts number of rolls it takes to match the initial roll
int gameNumber = 0; // counts number of games played
System.out.println();
while ( initialRoll != 2 ) // loop forever! initialRoll != 2
{
//gameNumber = 0 gameNumber <= 10
gameNumber++; // increment # of games played
// get the initial roll
initialRoll = d1.roll() + d2.roll();
// roll dice again to try to match initial roll
int currentRoll = d1.roll() + d2.roll();
// initialize count of number of rolls needed to match
rollCount = 1;
// repeat as long as the initial roll is not matched
// DO NOT MODIFY THIS LOOP! ONLY THE OUTER ONE!
while (currentRoll != initialRoll) // while NOT matched...
{
// ...roll 'em again!
currentRoll = d1.roll() + d2.roll();
// ...increment number of rolls made
rollCount++;
}
// Loop postcondition: initialRoll has been matched
// print stats
System.out.println("Trial #" + format(gameNumber)
+ " Initial roll = " + format(initialRoll)
+ " Matched in " + format(rollCount)
+ " rolls.");
}
}
}
public class GallopingDominoes2
{
public static void main(String[] args)
{
MatchGame2 fun = new MatchGame2();
fun.play();
}
}
Explanation / Answer
count=0;
while((count!=1)&&(initialrole!=2))
{
count++;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.