-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
165 lines (145 loc) · 4.95 KB
/
script.js
File metadata and controls
165 lines (145 loc) · 4.95 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const deck = [];
const suits = ["♠", "♥", "♣", "♦"];
const ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
const values = { "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, "J": 10, "Q": 10, "K": 10, "A": 11 };
let playerHand = [];
let dealerHand = [];
let wins = 0;
let losses = 0;
const dealerCards = document.getElementById('dealer-cards');
const playerCards = document.getElementById('player-cards');
const dealerScoreDisplay = document.getElementById('dealer-score');
const playerScoreDisplay = document.getElementById('player-score');
const messageDisplay = document.getElementById('message');
const winsDisplay = document.getElementById('wins');
const lossesDisplay = document.getElementById('losses');
const hitButton = document.getElementById('hit-button');
const standButton = document.getElementById('stand-button');
const restartButton = document.getElementById('restart-button');
const blackjackTable = document.getElementById('blackjack-table');
function createDeck() {
suits.forEach(suit => {
ranks.forEach(rank => {
deck.push({ suit, rank, value: values[rank] });
});
});
deck.sort(() => Math.random() - 0.5);
}
function startGame() {
createDeck();
playerHand = [drawCard(), drawCard()];
dealerHand = [drawCard(), drawCard()];
updateDisplay();
checkForBlackjack();
}
function drawCard() {
return deck.pop();
}
function updateDisplay() {
dealerCards.innerHTML = dealerHand.map((card, i) => {
return `<div class="card">${i === 0 && !standButton.disabled ? '?' : `${card.rank}${card.suit}`}</div>`;
}).join('');
playerCards.innerHTML = playerHand.map(card => {
return `<div class="card">${card.rank}${card.suit}</div>`;
}).join('');
dealerScoreDisplay.textContent = standButton.disabled ? calculateScore(dealerHand) : '?';
playerScoreDisplay.textContent = calculateScore(playerHand);
}
function calculateScore(hand) {
let score = hand.reduce((acc, card) => acc + card.value, 0);
let aceCount = hand.filter(card => card.rank === 'A').length;
while (score > 21 && aceCount > 0) {
score -= 10;
aceCount--;
}
return score;
}
function checkForBlackjack() {
const playerScore = calculateScore(playerHand);
const dealerScore = calculateScore(dealerHand);
if (playerScore === 21 && dealerScore === 21) {
messageDisplay.textContent = "It's a tie with Blackjack!";
endGame();
} else if (playerScore === 21) {
messageDisplay.textContent = "Blackjack! You win!";
blackjackTable.classList.add('win');
wins++;
updateStats();
endGame();
} else if (dealerScore === 21) {
messageDisplay.textContent = "Dealer has Blackjack! You lose.";
blackjackTable.classList.add('lose');
losses++;
updateStats();
endGame();
}
}
function playerHit() {
playerHand.push(drawCard());
updateDisplay();
const playerScore = calculateScore(playerHand);
if (playerScore > 21) {
messageDisplay.textContent = "You bust! Dealer wins.";
blackjackTable.classList.add('lose');
losses++;
updateStats();
endGame();
}
}
function dealerTurn() {
while (calculateScore(dealerHand) < 17) {
dealerHand.push(drawCard());
}
updateDisplay();
const dealerScore = calculateScore(dealerHand);
const playerScore = calculateScore(playerHand);
if (dealerScore > 21) {
messageDisplay.textContent = "Dealer busts! You win!";
blackjackTable.classList.add('win');
wins++;
updateStats();
} else if (dealerScore > playerScore) {
messageDisplay.textContent = "Dealer wins.";
blackjackTable.classList.add('lose');
losses++;
updateStats();
} else if (dealerScore < playerScore) {
messageDisplay.textContent = "You win!";
blackjackTable.classList.add('win');
wins++;
updateStats();
} else {
messageDisplay.textContent = "It's a tie!";
}
endGame();
}
function endGame() {
hitButton.disabled = true;
standButton.disabled = true;
restartButton.style.display = 'inline-block';
updateDisplay();
}
function updateStats() {
winsDisplay.textContent = `🏆 ${wins}`;
lossesDisplay.textContent = `❌ ${losses}`;
}
function restartGame() {
deck.length = 0;
playerHand.length = 0;
dealerHand.length = 0;
messageDisplay.textContent = '';
hitButton.disabled = false;
standButton.disabled = false;
restartButton.style.display = 'none';
blackjackTable.classList.remove('win', 'lose');
// Reset box shadow to default
blackjackTable.style.boxShadow = '0px 0px 20px rgba(0, 0, 0, 0.7)';
startGame();
}
hitButton.addEventListener('click', playerHit);
standButton.addEventListener('click', () => {
standButton.disabled = true;
dealerTurn();
});
restartButton.addEventListener('click', restartGame);
restartGame();