Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Javascript/HTML I\'m making a yachtzee style program. Basically there is 6 dice

ID: 3794385 • Letter: J

Question

Javascript/HTML

I'm making a yachtzee style program. Basically there is 6 dice (d0,d1,d2,d3,d4,d5) all held in an array called diceValues. When ever you click roll, the 6 dice randomly generate a number between 1 and 6 by calling the getRandomInt function (which works). However, whenever you roll the dice I have a function called rollDice() which is what puts values into the diceValue array for each dice as well as print off the 6 randomly generated dice. Another thing it does is heldValues which is started off as an array of false (heldValues = (false, false, false, false, false, false). When clicked upon, the certain dice thats clicked on is turned highlighted green and selected as held. What I also need to do is when ever a user clicks on any of the dice during the roll to "hold" the values, then whenever the user clicks roll, the dice that are held, stay the same value and the other dice randomly generate again.

Example:

diceValues = (0, 0, 0, 0 ,0, 0)

heldValues = (false, false, false, false, false, false)

user clicks roll - getrandomint creates 6 numbers

diceValues = (2, 4, 6, 1, 4, 1)

user clicks on d1, and d4 to be held

heldValues should change to = (false, true, false, false, true, false)

and whenever the user clicks roll again the 2 true values should stay the same

diceValues = (3, 4, 2, 5, 4, 6)

here's my sample code EVERYTHING WORKS BESIDES THE rollDice() function

Explanation / Answer

//Code Through Javascript & HTML

<HTML>
   <HEAD>

       <SCRIPT LANGUAGE="JavaScript">
           var die = 6;
           var dice = 3;
           function dice_roll(die, dice) {
               var roll = 0;
               for (loop=0; loop < dice; loop++) {
                   roll = roll + Math.round(Math.random() * die) % die + 1;
               }
               document.form.text.value = roll;
           }
       </script>
   </HEAD>
   <BODY>
       <center>
           <form name=form>
               <table border=2 cellpadding=5>
                   <tr>
                       <td colspan=2 align=middle>What dice type?</td>
                       <td colspan=2 align=middle>How many dice to roll?</td>
                   </tr>
                   <tr>
                       <td valign=top align=middle>
                           <p><input type=radio name=sides>3 Sided
                           <p><input type=radio name=sides>4 Sided
                           <p><input type=radio name=sides>5 Sided
                           <p><input type=radio checked name=sides>6 Sided
                           <p><input type=radio name=sides>8 Sided
                       </td>
                       <td valign=top align=middle>
                           <p><input type=radio name=sides>10 Sided
                           <p><input type=radio name=sides>12 Sided
                           <p><input type=radio name=sides>20 Sided
                           <p><input type=radio name=sides>30 Sided
                           <p><input type=radio name=sides>100 Sided
                       </td>
                       <td valign=top align=middle>
                           <p><input type=radio name=number>1
                           <p><input type=radio name=number>2
                           <p><input type=radio checked name=number>3
                           <p><input type=radio name=number>4
                           <p><input type=radio name=number>5
                       </td>
                       <td valign=top align=middle>
                           <p><input type=radio name=number>6
                           <p><input type=radio name=number>7
                           <p><input type=radio name=number>8
                           <p><input type=radio name=number>9
                           <p><input type=radio name=number>10
                       </td>
                   </tr>
                   <tr>
                       <td align=middle colspan=4>
                           <input type=button value="Roll Dice" name=button>
                           <input type=text size=10 name=text>
                       </td>
                   </tr>
               </table>
           </form>
       </center>
   </BODY>
</HTML>  
  
//Code Through C++

#include<iostream>
#include<ctime>

using namespace std;

int getRepetitions();
int rollDie(int);
void doRolling(int[], int, int);
void showSummary(int[], int, int);

double i;


int main()
{
   int uInput;
   uInput = getRepetitions();
   rollDie(uInput);
   doRolling();
   showSummary();
  
   system("pause");
   return 0;
}

int getRepetitions()
{
   int userInput = 1;
   while(userInput > 0)
   {
       cout << "How many times do you want to roll the dice? "<< endl;
       cin >> userInput;
   }
   return userInput;

}

int rollDie(int userInput)
{
   int counts[Max_number_of_rolls]={0};
   srand(time(NULL));
   int dice;
   dice= 1+ rand()%6;
   return dice;
}

void doRolling(int rollTimes[], int rollsRequired, int NumberOfSides)
{
   cout << "----------------Rolls of the first and second dice--------------" << endl;
   for(int i=0; i<rollDie; i++)
   {
       srand(time(NULL));
       int dice;
       dice= 1+ rand()%6;
       counts[i]=dice;
       cout << "Roll # " << i+1 << " first dice is: " << dice << endl;
       dice= 1+ rand()%6;
       counts[i]=counts[i]+dice;
       cout << "Roll # " << i+1 << " second dice is: " << dice << endl;
       cout << "The sum is: " << counts[i] << endl;
   }
   cout<< "-------------Results/Statistics----------------" << endl;
   for(i=2;i<13;i++)
   {
       int c =getRepetitions(counts,rollDie,i);
       cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
       percentage= ((double)c/(double)rollDie)*100;
       cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
   }
   cout << endl << endl;
}

void showSummary(int[], int, int)
{
   cout<< "-------------Results/Statistics----------------" << endl;
   for(i=2;i<13;i++)
   {
       int c =getRepetitions(counts,rollDie,i);
       cout << "The total value of " << i <<" is repeated " << c << " out of " << rollDie << " times." << endl;
       percentage= ((double)c/(double)rollDie)*100;
       cout << "Percentage of the time the total value was rolled is: " << percentage << "%" << endl;
   }
   cout << endl << endl;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote