-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
107 lines (95 loc) · 2.54 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// game.js
import { createDeck, drawCards } from "./deck.js";
let deckId;
let playerHand = [];
let dealerHand = [];
let playerScore = 0;
let dealerScore = 0;
let gameOver = false;
export async function startNewGame() {
deckId = await createDeck();
playerHand = await drawCards(deckId, 2);
dealerHand = await drawCards(deckId, 2);
gameOver = false;
updateScores();
updateUI();
document.getElementById("result").innerText = "";
}
export async function hit() {
if (!gameOver) {
const newCard = await drawCards(deckId, 1);
playerHand.push(newCard[0]);
updateScores();
updateUI();
checkForBust();
}
}
export async function stand() {
if (!gameOver) {
gameOver = true;
while (dealerScore < 17) {
const newCard = await drawCards(deckId, 1);
dealerHand.push(newCard[0]);
updateScores();
updateUI();
if (dealerScore >= 17) {
checkForWinner();
}
}
}
}
function updateScores() {
playerScore = calculateScore(playerHand);
dealerScore = calculateScore(dealerHand);
document.getElementById("player-score").innerText = `Score: ${playerScore}`;
document.getElementById("dealer-score").innerText = `Score: ${dealerScore}`;
}
function calculateScore(hand) {
let score = 0;
let hasAce = false;
hand.forEach((card) => {
if (card.value === "ACE") {
hasAce = true;
score += 11;
} else if (["KING", "QUEEN", "JACK"].includes(card.value)) {
score += 10;
} else {
score += parseInt(card.value);
}
});
if (score > 21 && hasAce) {
score -= 10;
}
return score;
}
function updateUI() {
const playerCardsDiv = document.getElementById("player-cards");
const dealerCardsDiv = document.getElementById("dealer-cards");
playerCardsDiv.innerHTML = "";
dealerCardsDiv.innerHTML = "";
playerHand.forEach((card) => {
const img = document.createElement("img");
img.src = card.image;
playerCardsDiv.appendChild(img);
});
dealerHand.forEach((card) => {
const img = document.createElement("img");
img.src = card.image;
dealerCardsDiv.appendChild(img);
});
}
function checkForBust() {
if (playerScore > 21) {
document.getElementById("result").innerText = "You bust! Dealer wins!";
gameOver = true;
}
}
function checkForWinner() {
if (dealerScore > 21) {
document.getElementById("result").innerText = "Dealer busts! You win!";
} else if (dealerScore >= playerScore) {
document.getElementById("result").innerText = "Dealer wins!";
} else {
document.getElementById("result").innerText = "You win!";
}
}