question archive The player is now allowed to make two additional rolls
Subject:Computer SciencePrice: Bought3
The player is now allowed to make two additional rolls. Each time, some of the dice can be left
as they are, and the rest are re-rolled.
The objective is to get as many as possible that are showing the same number. Once the
player has re-rolled the dice twice, the program will show all of the possible scores for the
current set of dice. The score is calculated by summing only those dice that are showing the
same number. For example, if the dice are showing 5-3-2-3-3 there are four possible scores:
• 2 (there's one 2),
• 9 (add up the 3's only),
• 5 (there's one 5),
• and 0 (there are no 1's, 4's, or 6's).
The functions that control the overall operation of the game, and draw all of the graphics, are
supplied. Fill-in the following two small functions to create a program that will allow the
user to re-roll the dice.
1. Fill-in the function void reroll(boolean[] diceToRoll) which will accept an
array of NUM_DICE boolean values.
• Dice for which diceToRoll[i] is true should be re-rolled. All the rest should
be left as they are.
• Recall that the value of each die is stored in a global integer array that you
created in Part 1. To re-roll a die, just assign it a new random value between 1
and NUM_SIDES.
2. Fill-in the function int scoreAs(int chosenNumber) which will calculate and
return the score for the given roll.
• All dice showing the value chosenNumber are added up, and all others are
ignored. Return this sum.
My code:
int[] theDie= new int[NUM_DICE];
void rollDice() {
for(int i=0;i<NUM_DICE;i++)
theDie[i]=int(random(1,6));
} //rollDice
void showDiceRoll() {
for(int j=0; j<NUM_DICE;j++)
drawDie(j,theDie[j]);
} //showDiceRoll