JSFiddle - React, Tailwind, and code Playground
by frogggeee McFrogggeee
HTML
<!DOCTYPE html>
<html>
<head>
<body>
<h1>Blackjack</h1>
<p>by frogggeee</p>
<button type = "button" style="font: bold 40px Arial;" onclick = "dealAllCards()">play</button>
<p></p>
<canvas id = "canvas" width = "480" height = "380" ></canvas>
<script>
let winMessage = 0;
let stage = 1;
let gameGoing = 0;
let over = 0;
let ask = 0;
let ask2 = 0;
let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
let cardSpriteSheet = new Image();
let cardNum = [1, 2, 3, 4, 5, 6, 7, 8, 9, "t", "j", "q", "k"];
let cardLogo = [0, 1, 2, 3];
let usedCards = [];
let playerCards = [];
let player2Cards = [];
let cn = 0;
let cl = 0;
let total = 0;
let total2 = 0;
ctx.beginPath();
ctx.rect(0, 0, 1000, 1000);
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();
cardSpriteSheet.src = "http://einaregilsson.github.io/cards.js/img/cards.png";
function setCards() { /* deals cards */
usedCards = [];
for (let i = 0; i < 2; i++) {
give1();
give2();
}
}
function hitOrStand() {
let answer = 0;
if (stage == 1) {
if (over == 0 && ask == 0) {
answer = 0;
answer = prompt ("do you want to hit or stay?");
if (answer == 'hit') {
giveCard(1);
} else if (answer == 'stay') {
ask = 1;
stage = 2;
setTimeout(hitOrStand, 500);
} else {
hitOrStand();
}
setTimeout(checkForWinner, 100);
if (ask == 0 && over == 0) {
setTimeout(hitOrStand, 100);
}
}
} else if (stage == 2) {
/*/ computers turn to hit/stand */
setTimeout(computersTurn, 100);
setTimeout(checkForWinner, 100);
if (ask2 == 0 && over == 0) {
setTimeout(hitOrStand, 100);
}
}
}
function checkForWinner() {
if (winMessage == 0) {
if (ask == 1 && ask2 == 1) { // check for winner
if (total > 21) {
if (total2 > 21) {
alert("you and the computer busted so it is a tie!");
} else {
alert ("the computer won because you busted!");
}
} else {
if (total2 > 21) {
alert("you won because the computer busted!");
} else...