ASDV-WebDev/Semester 1/Assignments/JavaScript/MP12_CalebFontenot/public_html/dice.html
2023-08-16 17:31:33 -05:00

84 lines
3.5 KiB
HTML

<!DOCTYPE html>
<!--
Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this template
-->
<html>
<head>
<title>Dice</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<label id="valueOutput"></label><br>
<label id="score"></label><br>
<label id="PlayerText">Your Dice roll:</label><br>
<label id="playerDie"></label><br>
<label id="cpuText">CPU Dice roll:</label><br>
<label id="cpuDie"></label>
<label id="input"><input type="button" onclick="onClick()" value="Roll Die"/></label>
</body>
<script>
var cpuDieRoll = 0, playerDieRoll = 0, diceValues = {"playerDie": {}, "cpuDie": {}}, cpuTotalScore = 0, playerTotalScore = 0;
function onClick() {
playerDieRoll = rollDie("playerDie");
cpuDieRoll = rollDie("cpuDie");
writeValue(determineScore());
}
function printDie(dieNumber, elementId) {
let filename = "DieSet/Die" + dieNumber + ".png";
document.getElementById(elementId).innerHTML += "<image src=" + filename + ">";
//document.getElementById("wager").innerHTML = JSON.stringify(diceValues)
}
function rollDie(id) {
document.getElementById(id).innerHTML = "";
let diceTotal = 0;
for (let i = 0; i < 2; i++) {
let dieValue = Math.trunc((Math.random() * 6) + 1);
printDie(dieValue, id);
if (id === "playerDie") {
diceValues.playerDie["roll" + (i + 1)] = dieValue;
} else {
diceValues.cpuDie["roll" + (i + 1)] = dieValue;
}
diceTotal += dieValue;
}
return diceTotal;
}
function writeValue([playerRollScore, cpuRollScore]) {
document.getElementById("valueOutput").innerHTML = "Your Dice roll: " + playerRollScore + "; "
document.getElementById("valueOutput").innerHTML += "CPU Dice roll: " + cpuRollScore;
}
function determineScore() {
//Add rolls together
let playerRollScore = diceValues.playerDie["roll1"] + diceValues.playerDie["roll2"];
let cpuRollScore = diceValues.cpuDie["roll1"] + diceValues.cpuDie["roll2"];
let winString = "";
if (cpuDieRoll == playerDieRoll) {
winString = " It's a tie, no one won :("
} else {
if (playerRollScore > cpuRollScore) {
winString = " Player won!";
if (diceValues.playerDie["roll1"] == diceValues.playerDie["roll2"]) {
//double is rolled for player
playerRollScore = playerRollScore * 2;
winString = " Player won and rolled a double!";
}
playerTotalScore += playerRollScore;
} else {
winString = " CPU Won!"
if (diceValues.cpuDie["roll1"] == diceValues.cpuDie["roll2"]) {
//double is rolled for cpu
cpuRollScore = cpuRollScore * 2;
winString = " CPU won and rolled a double!"
}
cpuTotalScore += cpuRollScore;
}
}
document.getElementById("score").innerHTML = "Total Score: Player: " + playerTotalScore + "; CPU: " + cpuTotalScore + winString;
return [playerRollScore, cpuRollScore];
}
</script>
</html>