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

109 lines
4.9 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="wager"></label><br>
<label id="poolStats"></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><br>
<label>Enter the amount of money to add to the pool (aka the "kitty"): <input id="wagerAmount" type="number"><input type="button" id="makeBet" onclick="determineWager();" value="Make bet"/></label><br>
<label id="input"><input type="button" id="rollDie" onclick="onClick()" value="Roll Die"/></label>
</body>
<script>
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
var cpuDieRoll = 0, playerDieRoll = 0, diceValues = {"playerDie": {}, "cpuDie": {}}, cpuTotalScore = 0, playerTotalScore = 0, moneyPool = 0;
document.getElementById("rollDie").hidden = true;
function onClick() {
playerDieRoll = rollDie("playerDie");
cpuDieRoll = rollDie("cpuDie");
writeValue(determineScore());
document.getElementById("rollDie").hidden = true;
}
function determineWager() {
let playerWager = parseInt(document.getElementById("wagerAmount").value);
let cpuWager = 0;
let determineCpuMove = Math.trunc((Math.random() * 3));
switch (determineCpuMove) {
case 0: // Match bet
document.getElementById("wager").innerHTML = "CPU: I match your bet! 😏";
cpuWager = playerWager;
break;
case 1: // fold
document.getElementById("wager").innerHTML = "CPU: Okay, I'll fold. 😔";
playerTotal = moneyPool;
cpuWager = 0;
moneyPool = 0;
break;
case 2: // Raise bet
document.getElementById("wager").innerHTML = "CPU: I'll raise your bet! 😏";
cpuWager = (playerWager * 1.5);
break;
}
moneyPool = (cpuWager + playerWager);
document.getElementById("poolStats").innerHTML = "Pool: " + formatter.format(moneyPool);
document.getElementById("rollDie").hidden = false;
}
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!";
playerTotalScore += moneyPool;
moneyPool = 0;
} else {
winString = " CPU Won!"
cpuTotalScore = moneyPool;
moneyPool = 0;
}
}
document.getElementById("score").innerHTML = "Total Score: Player: " + formatter.format(playerTotalScore) + "; CPU: " + formatter.format(cpuTotalScore) + winString;
return [playerRollScore, cpuRollScore];
}
</script>
</html>