Answered! 4. [40 pts] 111/p5/DiceGame.html and DiceGame.js Download DiceGame.zip: https://drive.google.com/open?id=0BwECDRBexWG0YXlEcW9pdVRxOEU…

4. [40 pts] 111/p5/DiceGame.html and DiceGame.js Download DiceGame.zip: https://drive.google.com/open?id=0BwECDRBexWG0YXlEcW9pdVRxOEU

For this problem, you will create a web page that simulates rolling five dice. You will use the code you wrote in Dice.js

to complete this task. Follow the instructions in the downloaded code files. Your finished solution should look and work as follows:

Rollem! You rolled 15!

If no user input is given, “Roll’em!” rolls five dice and prints the sum at the bottom of the page..

3

If user input is given, “Roll’em!” rolls dice until the input sum, between 5 and 30, is rolled and prints the number of times it took to roll it. In both cases, the most recent roll of the five dice is displayed on the page.

jQuery.attr() function is used to display the images when the page loads and when the dice image needs to be changed.

When the user enters a target number and clicks Roll ’em!, the images are updated die-namically and the results displayed in a div on the page…

Expert Answer

 There are no changes in css

DiceGame.js:

//Copy your Dice class from Dice.js here.
//You will then be able to use it to solve
//the rest of the exercise.

//top-level function
function rollSum(n, sum) {
//roll n dice until you hit sum
//return the number of rolls it took

};

function displayFiveDiceRoll(arrDice) {

//display the corresponding die images
//which represent the die rolls in the
//given array, arrDice, in the HTML table

//use the following jQuery: $(selector).attr(src,file_path)

};

function rollTheDice() {

//check if user entered a sum to roll
var userInput = parseInt($(“#sumToRoll”).val());
var dieRoll = [], sum, iCounter = 0, game_message = “”;
if (typeof userInput !== “undefined” && userInput) {
while (sum !== userInput) {
dieRoll = Dice.rollNDice(5);
sum = Dice.sumNDice(dieRoll);
iCounter++;
}
game_message = “It took ” + iCounter + ” times to roll ” + sum + “!”;
} else {
if (userInput === 0 && userInput < 5) {
game_message = “Input should be greater than 5!!!”;
} else {
//if sum to roll was not entered
//roll 5 dice
dieRoll = Dice.rollNDice(5);
sum = Dice.sumNDice(dieRoll);
game_message = “You rolled ” + sum + “!”;
}
}

//display the corresponding die images
//report the sum rolled in a div
$(“#game_message”).html(game_message);

//display the corresponding die images
for (var i = 0; i < dieRoll.length; i++) {
$(“#die” + (i + 1)).attr(“src”, Dice.getDiceImagePath(dieRoll[i]));
}

};

$(document).ready(function () {

//display the default image on each die, on the HTML page
//use the following jQuery: $(selector).attr(src,file_path)

$(“.rolledDice”).attr(“src”, Dice.getDiceImagePath(6));
//using jQuery, register a handler, rollTheDice()
//to the Roll’em! button
$(“#roll”).click(rollTheDice);

});

Dice.js:

“use strict”;
//define a Dice object, properties and methods
var Dice = {
sides: 6,
currentDiceRollsArray: [],
defaultImagePath: “images/3d_dice.png”,
// Rolls the dice returns a random number
rollDie: function () {
return Math.floor(Math.random() * this.sides) + 1;
},
// Returns array of numbers of dice
rollNDice: function (n) {
var results = [];
for (var i = 0; i < n; i++) {
results.push(this.rollDie());
}
return results;
},
// Sum all the numbers of current roll or the array provided
sumNDice: function (diceAry) {
var sum = 0;
for (var i = 0; i < diceAry.length; i++) {
sum += diceAry[i];
}
return sum;
},
// Maximum number of current roll
maxRoll: function (diceAry) {
var max = 0;
for (var i = 0; i < diceAry.length; i++) {
if (diceAry[i] > max) {
max = diceAry[i];
}
}
return max;
},

isAllOfAKind: function (diceAry) {
if (diceAry.length <= 1) {
return true;
}
var value = diceAry[0];

for (var i = 1; i < diceAry.length; i++) {
if (diceAry[i] != value) {
return false;
}
}
return true;
},
// Returns image path of dice as per the number
getDiceImagePath: function (p) {
return “images/red_dice” + p + “.png”
}
}

HTML:

<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Dice Game</title>
<!– link your css file here –>
<link href=”DiceGame.css” rel=”stylesheet” />
<!– link the jQuery library here –>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js”></script>
<!– link your JavaScript file here –>
<script src=”Dice.js”></script>
<script src=”DiceGame.js”></script>

</head>
<body>
<div>
<table class=”container”>
<tr><td colspan=”5″><img src=”images/diceLogo.png” /></td></tr>
<tr>
<td><img class=”rolledDice” id=”die1″ width=”120″ height=”100″ src=”” /></td>
<td><img class=”rolledDice” id=”die2″ width=”120″ height=”100″ src=”” /></td>
<td><img class=”rolledDice” id=”die3″ width=”120″ height=”100″ src=”” /></td>
<td><img class=”rolledDice” id=”die4″ width=”120″ height=”100″ src=”” /></td>
<td><img class=”rolledDice” id=”die5″ width=”120″ height=”100″ src=”” /></td>
</tr>
<tr>
<td colspan=”5″>
<input class=”numToRoll” id=”sumToRoll” type=”text” value=””></input>
</td>
</tr>
<tr>
<td colspan=”5″>
<button id=”roll”>
Roll’em!
</button>
</td>
</tr>
<tr>
<td colspan=”5″>
<div class=”game_message” id=”game_message”></div>
</td>
</tr>
</table>
</div>
</body>
</html>

Output:

Still stressed from student homework?
Get quality assistance from academic writers!