question archive 1) An int[] global variable and initialize it to an array that will hold the NUM_DICE dice
Subject:Computer SciencePrice: Bought3
1) An int[] global variable and initialize it to an array that will hold the
NUM_DICE dice.
2) Fill in the function void rollDice()to fill in the array you just created with values.
After the function runs, the array should be filled with NUM_DICE random integers
between 1 and NUM_SIDES (these constants are already defined for you).
3. Fill in the function void showDiceRoll() which will draw the dice roll by calling the
provided drawDie() function NUM_DICE times - once for each die.
Part 2: Re-Roll Some of the Dice
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 program what 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 so far:
need help with the ScoreAs codes
final int NUM_SIDES=6; //Sides on the dice
final int NUM_DICE=5; //The number of dice used
final int NUM_REROLLS=2; //Number of rerolls after the initial roll
//-------- Part 1 -------------------------------------
int[] theDie= new int[NUM_DICE];
void rollDice() {
for(int a=0;a<NUM_DICE;a++)
theDie[a]=int(random(1,6));
} //rollDice
void showDiceRoll() {
for(int j=0; j<NUM_DICE;j++)
drawDie(j,theDie[j]);
} //showDiceRoll
//-------- Part 2 -------------------------------------
void reroll(boolean[] diceToRoll){
for(int a=0;a<NUM_DICE;a++)
theDie[a]=int(random(1,NUM_SIDES));
}//reroll
int scoreAs(int chosenNumber){
}//Scoreas