update
This commit is contained in:
parent
bf8d402aa7
commit
139266aae4
@ -19,10 +19,12 @@ Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this temp
|
||||
<label id="playerPoints"></label><br>
|
||||
<label id="playerCards"></label><br>
|
||||
<label id="cardsInPlay"></label>
|
||||
<label id="winAnnounce"></label><br>
|
||||
<input type="button" id="startButton" onclick="startGame()" value="Start Game">
|
||||
<input type="button" id="continueButton" onclick="draw()" value="Continue Round">
|
||||
<input type="button" id="endButton" onclick="endRound()" value="End Round">
|
||||
<script>
|
||||
var timesLooped = 0;
|
||||
var cpuPoints = 0, playerPoints = 0;
|
||||
var cpuCards = 0, playerCards = 0;
|
||||
var totalCpuPoints = 0, totalPlayerPoints = 0;
|
||||
@ -30,48 +32,50 @@ Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this temp
|
||||
playerTimesWon = 0;
|
||||
var isStarted = false;
|
||||
// Card array. This will allow us to remember if a card is in play.
|
||||
var rows = 12;
|
||||
var rows = 13;
|
||||
var cols = 3;
|
||||
var cardArray = Array.from({length: rows}, () =>
|
||||
Array.from({length: cols}, () => false)
|
||||
Array.from({length: cols}, () => [0, 0, false])
|
||||
);
|
||||
hiddenCard.hidden = true;
|
||||
continueButton.hidden = true;
|
||||
endButton.hidden = true;
|
||||
cardsInPlay.hidden = true;
|
||||
function checkEntireArray() {
|
||||
var numOfBools = (cardArray.length * cardArray[0].length);
|
||||
console.log("Number of bools: " + numOfBools);
|
||||
//console.log("Number of bools: " + numOfBools);
|
||||
var trueCount = 0;
|
||||
var returnBool = false;
|
||||
for (let i = 0; i < cardArray.length; i++) {
|
||||
for (let j = 0; j < cardArray[i].length; j++) {
|
||||
if (cardArray[i][j]) {
|
||||
if (cardArray[i][j][2]) {
|
||||
trueCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log("Number of bools set to true: " + trueCount);
|
||||
//console.log("Number of bools set to true: " + trueCount);
|
||||
if (trueCount == numOfBools) {
|
||||
returnBool = true;
|
||||
}
|
||||
return returnBool;
|
||||
}
|
||||
function getCardNumber() {
|
||||
console.log("Times looped " + ++timesLooped);
|
||||
let exitLoop = false;
|
||||
let cardNumber = 0; //prompt("Card Number:");
|
||||
let cardType = 0; //prompt("Card Type: ");
|
||||
do {
|
||||
console.log(cardType + " " + cardNumber);
|
||||
//console.log(cardType + " " + cardNumber);
|
||||
if (checkEntireArray()) {
|
||||
console.log('Ran out of cards in deck! Breaking!');
|
||||
alert("Cannot draw more cards!");
|
||||
return false;
|
||||
}
|
||||
if (!cardArray[cardNumber][cardType]) {
|
||||
cardArray[cardNumber][cardType] = true;
|
||||
if (!cardArray[cardNumber][cardType][2]) {
|
||||
cardArray[cardNumber][cardType] = [cardNumber, cardType, true];
|
||||
exitLoop = true;
|
||||
} else {
|
||||
cardNumber = Math.trunc((Math.random() * 12));
|
||||
cardNumber = Math.trunc((Math.random() * 13));
|
||||
cardType = Math.trunc((Math.random() * 3));
|
||||
console.log("Ran into existing card, generating new numbers! " + cardType + " " + cardNumber);
|
||||
exitLoop = false;
|
||||
@ -79,6 +83,7 @@ Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this temp
|
||||
} while (!exitLoop);
|
||||
printArray();
|
||||
checkEntireArray();
|
||||
console.log(cardType + " " + cardNumber);
|
||||
return [cardNumber, cardType];
|
||||
|
||||
}
|
||||
@ -177,61 +182,72 @@ Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this temp
|
||||
document.getElementById("cpuPoints").innerHTML += "Total Points: " + totalCpuPoints;
|
||||
document.getElementById("cpuGamesWon").innerHTML = "Dealer cards: (times won: " + cpuTimesWon + ")";
|
||||
}
|
||||
function startGame() {
|
||||
// Dealer and player have two cards.
|
||||
if (!isStarted) {
|
||||
document.getElementById("playerCards").innerHTML = "";
|
||||
document.getElementById("cpuCards").innerHTML = "";
|
||||
cpuPoints = 0;
|
||||
playerPoints = 0;
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
|
||||
drawCards('cpu', cardNumber, cardType, false);
|
||||
cpuPoints += cpuCards;
|
||||
drawCards('cpu', 0, 0, true);
|
||||
for (let i = 0; i < 2; i++) {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
drawCards('player', cardNumber, cardType, false);
|
||||
playerPoints += playerCards + 1;
|
||||
function resetArray() {
|
||||
for (let i = 0; i < cardArray.length; i++) {
|
||||
for (let j = 0; j < cardArray[i].length; j++) {
|
||||
cardArray[i][j] = [0, 0, false];
|
||||
}
|
||||
isStarted = true;
|
||||
startButton.hidden = true;
|
||||
continueButton.hidden = false;
|
||||
endButton.hidden = false;
|
||||
start.hidden = true;
|
||||
}
|
||||
updatePoints();
|
||||
}
|
||||
function draw() {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
// if ((playerPoints + cardNumber) < 21)
|
||||
{
|
||||
drawCards('player', cardNumber, cardType, false);
|
||||
playerPoints += playerCards + 1;
|
||||
updatePoints();
|
||||
}
|
||||
function startGame() {
|
||||
resetArray();
|
||||
// Dealer and player have two cards.
|
||||
document.getElementById("winAnnounce").innerHTML = "";
|
||||
if (!isStarted) {
|
||||
document.getElementById("playerCards").innerHTML = "";
|
||||
document.getElementById("cpuCards").innerHTML = "";
|
||||
cpuPoints = 0;
|
||||
playerPoints = 0;
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
|
||||
}
|
||||
function endRound() {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
drawCards('cpu', cardNumber, cardType, false);
|
||||
cpuPoints += cpuCards + 1;
|
||||
updatePoints();
|
||||
startButton.hidden = false;
|
||||
continueButton.hidden = true;
|
||||
endButton.hidden = true
|
||||
isStarted = false;
|
||||
// Determine winner
|
||||
if (playerPoints <= 21 & (cpuPoints < playerPoints)) {
|
||||
totalPlayerPoints += playerPoints;
|
||||
playerTimesWon++;
|
||||
} else {
|
||||
totalCpuPoints += cpuPoints;
|
||||
cpuTimesWon++;
|
||||
}
|
||||
updatePoints();
|
||||
drawCards('cpu', cardNumber, cardType, false);
|
||||
cpuPoints += cpuCards;
|
||||
drawCards('cpu', 0, 0, true);
|
||||
for (let i = 0; i < 2; i++) {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
drawCards('player', cardNumber, cardType, false);
|
||||
playerPoints += playerCards + 1;
|
||||
}
|
||||
isStarted = true;
|
||||
startButton.hidden = true;
|
||||
continueButton.hidden = false;
|
||||
endButton.hidden = false;
|
||||
start.hidden = true;
|
||||
}
|
||||
updatePoints();
|
||||
}
|
||||
function draw() {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
// if ((playerPoints + cardNumber) < 21)
|
||||
{
|
||||
drawCards('player', cardNumber, cardType, false);
|
||||
playerPoints += playerCards + 1;
|
||||
updatePoints();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
function endRound() {
|
||||
var [cardNumber, cardType] = getCardNumber();
|
||||
drawCards('cpu', cardNumber, cardType, false);
|
||||
cpuPoints += cpuCards + 1;
|
||||
updatePoints();
|
||||
startButton.hidden = false;
|
||||
continueButton.hidden = true;
|
||||
endButton.hidden = true
|
||||
isStarted = false;
|
||||
// Determine winner
|
||||
if (playerPoints <= 21 & (cpuPoints < playerPoints)) {
|
||||
totalPlayerPoints += playerPoints;
|
||||
playerTimesWon++;
|
||||
document.getElementById("winAnnounce").innerHTML = "You win!";
|
||||
} else {
|
||||
totalCpuPoints += cpuPoints;
|
||||
cpuTimesWon++;
|
||||
document.getElementById("winAnnounce").innerHTML = "The dealer wins!";
|
||||
}
|
||||
updatePoints();
|
||||
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -5,58 +5,110 @@ Click nbfs://nbhost/SystemFileSystem/Templates/Other/html.html to edit this temp
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<title>Point Finder</title>
|
||||
<title id="title"></title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<script>
|
||||
// Change the dialog labels if computation changes
|
||||
function onComputationSelect(computation) {
|
||||
alert("Selected " + computation);
|
||||
if (computation === "area") {
|
||||
document.getElementById('input1Text').innerHTML = "Enter the base: ";
|
||||
document.getElementById('input2Text').innerHTML = "Enter the height: ";
|
||||
} else if (computation === "pointDistance") {
|
||||
document.getElementById('output').innerHTML;
|
||||
}
|
||||
}
|
||||
function onClick() {
|
||||
// Determine what the user wants us to do.
|
||||
if (selectComputation.value === "area") {
|
||||
getArea();
|
||||
} else if (selectComputation.value === "pointDistance") {
|
||||
getPointDistance();
|
||||
}
|
||||
}
|
||||
function getInput() {
|
||||
var input1Val = parseFloat(input1.value);
|
||||
var input2Val = parseFloat(input2.value);
|
||||
return [input1Val, input2Val];
|
||||
}
|
||||
function getArea() {
|
||||
var [baseVal, heightVal] = getInput();
|
||||
var area = (.5 * baseVal) * heightVal;
|
||||
document.getElementById('output').innerHTML = "The area of " + baseVal + " and " + heightVal + " is " + area;
|
||||
}
|
||||
function getPointDistance() {
|
||||
var [baseVal, heightVal] = getInput();
|
||||
|
||||
}
|
||||
function calculatePower() {
|
||||
alert("Not implemented!");
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<label><label id="input1Text">Select a computation type to continue.</label><input type="text" id="input1" name="input1"></label><br>
|
||||
<label><label id="input2Text">Select a computation type to continue.</label><input type="text" id="input2" name="input2"></label><br>
|
||||
<label id="input1Field"><label id="input1Text"></label><input type="text" id="input1" name="input1" value=""><br></label>
|
||||
<label id="input2Field"><label id="input2Text"></label><input type="text" id="input2" name="input2" value=""><br></label>
|
||||
<label id="input3Field"><label id="input3Text"></label><input type="text" id="input3" name="input3" value=""><br></label>
|
||||
<label id="input4Field"><label id="input4Text"></label><input type="text" id="input4" name="input4" value=""><br></label>
|
||||
<label for="selectComputation">What do you want to do?</label>
|
||||
<select name="selectComputation" id="selectComputation"><br>
|
||||
<option value="" selected>-- Select computation type --</option>
|
||||
<option onclick="onComputationSelect(this.value);" value="nothingSelected" selected>-- Select computation type --</option>
|
||||
<option onclick="onComputationSelect(this.value);" value="calculatePower">Get magnitude of power</option>
|
||||
<option onclick="onComputationSelect(this.value);" value="area">Get Area</option>
|
||||
<option onclick="onComputationSelect(this.value);" value="pointDistance">Get Point Distance</option>
|
||||
</select>
|
||||
<br>
|
||||
<label id="output"></label><br>
|
||||
<input type="button" value="Compute!" onclick="onClick()">
|
||||
<script>
|
||||
onComputationSelect("nothingSelected");
|
||||
|
||||
// Change the dialog labels if computation changes
|
||||
function onComputationSelect(computation) {
|
||||
//alert("Selected " + computation);
|
||||
if (computation === "nothingSelected") {
|
||||
console.log("Hiding text box...");
|
||||
document.getElementById("input1").hidden = true;
|
||||
} else {
|
||||
console.log("Unhiding text box...");
|
||||
document.getElementById("input1").hidden = false;
|
||||
}
|
||||
switch (computation) {
|
||||
case "nothingSelected":
|
||||
document.getElementById('title').innerHTML = "Calculator";
|
||||
input1Field.hidden = false;
|
||||
input2Field.hidden = true;
|
||||
input3Field.hidden = true;
|
||||
input4Field.hidden = true;
|
||||
document.getElementById('input1Text').innerHTML = "Select a computation type to continue. ";
|
||||
break;
|
||||
case "area":
|
||||
document.getElementById('title').innerHTML = "Calculator | Area";
|
||||
input1Field.hidden = false;
|
||||
input2Field.hidden = false;
|
||||
input3Field.hidden = true;
|
||||
input4Field.hidden = true;
|
||||
document.getElementById('input1Text').innerHTML = "Enter the base: ";
|
||||
document.getElementById('input2Text').innerHTML = "Enter the height: ";
|
||||
break;
|
||||
case "pointDistance":
|
||||
document.getElementById('title').innerHTML = "Calculator | Point Distance";
|
||||
input1Field.hidden = false;
|
||||
input2Field.hidden = false;
|
||||
input3Field.hidden = false;
|
||||
input4Field.hidden = false;
|
||||
document.getElementById('input1Text').innerHTML = "Enter x1: ";
|
||||
document.getElementById('input2Text').innerHTML = "Enter y1: ";
|
||||
document.getElementById('input3Text').innerHTML = "Enter x2: ";
|
||||
document.getElementById('input4Text').innerHTML = "Enter y2: ";
|
||||
break;
|
||||
case "calculatePower":
|
||||
document.getElementById('title').innerHTML = "Calculator | Power";
|
||||
input1Field.hidden = false;
|
||||
input2Field.hidden = false;
|
||||
input3Field.hidden = true;
|
||||
input4Field.hidden = true;
|
||||
document.getElementById('input1Text').innerHTML = "Enter base: ";
|
||||
document.getElementById('input2Text').innerHTML = "Enter power: ";
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
function onClick() {
|
||||
// Determine what the user wants us to do.
|
||||
switch (selectComputation.value) {
|
||||
case "area":
|
||||
getArea();
|
||||
break;
|
||||
case "pointDistance":
|
||||
getPointDistance();
|
||||
break;
|
||||
case "calculatePower":
|
||||
calculatePower();
|
||||
break;
|
||||
}
|
||||
}
|
||||
function getArea() {
|
||||
var [baseVal, heightVal] = [parseFloat(input1.value), parseFloat(input2.value)];
|
||||
var area = (.5 * baseVal) * heightVal;
|
||||
document.getElementById('output').innerHTML = "The area of " + baseVal + " and " + heightVal + " is " + area;
|
||||
}
|
||||
function getPointDistance() {
|
||||
var [x1, y1, x2, y2] = [parseFloat(input1.value), parseFloat(input2.value), parseFloat(input3.value), parseFloat(input4.value)];
|
||||
var a = (x2 - x1);
|
||||
var b = (y2 - y1);
|
||||
var distance = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
|
||||
document.getElementById('output').innerHTML = "The distance of point (" + x1 + ", " + y1 + ") to point (" + x2 + ", " + y2 + ") is " + distance;
|
||||
}
|
||||
function calculatePower() {
|
||||
var [base, power] = [parseFloat(input1.value), parseFloat(input2.value)];
|
||||
var output = Math.pow(base, power);
|
||||
document.getElementById('output').innerHTML = "The value of " + base + "^" + power + " is " + output;
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
BIN
ZIPs/JavaScript/MP11_CalebFontenot.zip
Normal file
BIN
ZIPs/JavaScript/MP11_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user