xxxxxxxxxx
181
let end = false
let hidden = true
let hand = [newCard(), newCard()];
let total = cardToInt(hand[0]) + cardToInt(hand[1]);
let dealer = [newCard(), newCard()];
let dealerTotal = cardToInt(dealer[0]) + cardToInt(dealer[1])
if (dealerTotal > 21 && dealer.includes("Ace")) {
dealerTotal -= 10;
dealer[dealer.indexOf("Ace")] = "Ace\n(One)";
}
while (dealerTotal < 17) {
let toAdd = newCard()
dealerTotal += cardToInt(toAdd)
dealer.push(toAdd)
if (dealerTotal > 21 && dealer.includes("Ace")) {
dealerTotal -= 10;
dealer[dealer.indexOf("Ace")] = "Ace\n(One)";
}
}
function setup() {
createCanvas(800, 600);
}
function drawHand(x, y, array) {
let duplicateCheck = []
for (const card of array) {
textSize(20);
fill(250);
if (duplicateCheck.includes(card)) {
let count = 0;
for (const check of duplicateCheck) {
if (check == card) {
count += 1
}
}
count *= 7
rect(x + 30 + (105 * array.indexOf(card)) - count, y + 30 - count, 100, 140);
}
rect(x + (105 * array.indexOf(card)), 430, 100, 140);
fill(0);
text(card, x + 20 + (105 * array.indexOf(card)), y + 35);
duplicateCheck.push(card);
}
}
function newCard() {
let card = Math.ceil(Math.random() * 13)
switch (card) {
case 1:
return "Ace"
break
case 11:
return "Jack"
break
case 12:
return "Queen"
break
case 13:
return "King"
break
default:
return card
}
}
function cardToInt(card) {
switch (card) {
case "Ace":
return 11
break
case "Jack":
case "Queen":
case "King":
return 10
break
default:
return card
}
}
function between(check, start, range) {
if (check > start && check < start + range) {
return true
} else {
return false
}
}
function draw() {
background(10, 90, 20);
textSize(30);
fill(0);
text("Click the deck to hit\nClick the dealer's hand to stand", 160, 250);
text(`Total: ${total}`, 15, 500);
//drawing deck
for (cardsDrawn = 0; cardsDrawn < 11; cardsDrawn++) {
fill(250);
rect(20, 300 - (cardsDrawn * 3), 100, 140);
fill(220, 50, 50);
rect(25, 305 - (cardsDrawn * 3), 90, 130);
}
//drawing your hand
drawHand(210, 430, hand)
//drawing dealer's hand
textSize(30);
if (hidden == true) {
text(`Total: ??`, 15, 100);
for (i = 0; i < 2; i++) {
fill(250)
rect(210 + (105 * i), 10, 100, 140);
fill(220, 50, 50);
rect(215 + (105 * i), 15, 90, 130);
}
} else {
text(`Total: ${dealerTotal}`, 15, 100);
//drawHand(210,10,dealer)/*
let duplicateCheck = []
for (const card of dealer) {
textSize(20);
fill(250);
if (duplicateCheck.includes(card)) {
let count = 0;
for (const check of duplicateCheck) {
if (check == card) {
count += 1
}
}
count *= 7
rect(240 + (105 * dealer.indexOf(card)) - count, 38 - count, 100, 140);
}
rect(210 + (105 * dealer.indexOf(card)), 10, 100, 140);
fill(0);
text(card, 230 + (105 * dealer.indexOf(card)), 40);
duplicateCheck.push(card);
}
fill(255, 0, 0);
textSize(120)
if (dealerTotal > 21) {
text("Dealer Bust!", 140, 400);
} else if (dealerTotal > total) {
text("Dealer won!", 150, 400);
} else if (dealerTotal == total) {
text("Tie!", 180, 400);
} else {
text("You won!", 180, 400);
}
}
if (total > 21) {
if (hand.includes("Ace")) {
total -= 10;
hand[hand.indexOf("Ace")] = "Ace\n(One)";
} else {
end = true
fill(255, 0, 0);
textSize(120)
text("Bust!", 180, 400);
}
}
}
function mouseClicked() {
if (end != true) {
if (between(mouseX, 20, 100) && between(mouseY, 260, 180)) {
let toAdd = newCard()
total += cardToInt(toAdd)
hand.push(toAdd)
}
if (between(mouseX, 210, 205) && between(mouseY, 10, 140)) {
end = true;
hidden = false;
}
}
}