-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
209 lines (186 loc) · 5.71 KB
/
index.js
File metadata and controls
209 lines (186 loc) · 5.71 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const VALUES = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "JACK", "QUEEN", "KING", "ACE"]
const SUITS = ["SPADES", "DIAMONDS", "CLUBS", "HEARTS"]
const SYM = { SPADES: "♠", DIAMONDS: "♦", CLUBS: "♣", HEARTS: "♥" }
const COL = { SPADES: "#1c1c2e", DIAMONDS: "#8b0000", CLUBS: "#1c1c2e", HEARTS: "#8b0000" }
let deck = [],
cpuScore = 0,
youScore = 0,
isFlipping = false
const get = (id) => document.getElementById(id)
const newDeckBtn = get("new-deck-btn")
const drawBtn = get("draw-btn")
const playAgainBtn = get("play-again-btn")
const remainingEl = get("remaining-text")
const cpuScoreEl = get("cpu-score")
const youScoreEl = get("you-score")
const resultText = get("result-text")
const cpuPanel = get("cpu-panel")
const youPanel = get("you-panel")
const cpuCardInner = get("cpu-card-inner")
const youCardInner = get("you-card-inner")
const cpuCardImg = get("cpu-card-img")
const youCardImg = get("you-card-img")
const overlay = get("game-over-overlay")
function buildDeck() {
const d = []
for (const suit of SUITS) for (const value of VALUES) d.push({ value, suit })
return d
}
function shuffle(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[arr[i], arr[j]] = [arr[j], arr[i]]
}
return arr
}
function makeCardSVG({ value, suit }) {
const lbl = { JACK: "J", QUEEN: "Q", KING: "K", ACE: "A" }[value] || value
const sym = SYM[suit],
col = COL[suit]
const svg =
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 110 154">' +
'<rect width="110" height="154" rx="8" fill="#fdfaf3" stroke="#d4c9a8" stroke-width="1.5"/>' +
'<text x="9" y="29" font-family="Georgia,serif" font-size="22" font-weight="bold" fill="' +
col +
'">' +
lbl +
"</text>" +
'<text x="9" y="49" font-family="Georgia,serif" font-size="17" fill="' +
col +
'">' +
sym +
"</text>" +
'<text x="55" y="90" font-family="Georgia,serif" font-size="46" fill="' +
col +
'" text-anchor="middle" dominant-baseline="middle">' +
sym +
"</text>" +
'<text x="101" y="135" font-family="Georgia,serif" font-size="22" font-weight="bold" fill="' +
col +
'" text-anchor="end">' +
lbl +
"</text>" +
'<text x="101" y="115" font-family="Georgia,serif" font-size="17" fill="' +
col +
'" text-anchor="end">' +
sym +
"</text>" +
"</svg>"
return "data:image/svg+xml;charset=utf-8," + encodeURIComponent(svg)
}
function showResult(text, cls) {
resultText.className = ""
resultText.textContent = text
void resultText.offsetWidth
resultText.className = "visible" + (cls ? " " + cls : "")
}
function bumpScore(el) {
el.classList.remove("bump")
void el.offsetWidth
el.classList.add("bump")
}
function flashPanel(panel, cls) {
panel.classList.remove("winner-flash", "loser-flash")
void panel.offsetWidth
panel.classList.add(cls)
panel.addEventListener("animationend", () => panel.classList.remove(cls), { once: true })
}
const delay = (ms) => new Promise((r) => setTimeout(r, ms))
function newGame() {
newDeckBtn.disabled = true
drawBtn.disabled = true
cpuCardInner.classList.remove("flipped")
youCardInner.classList.remove("flipped")
showResult("Shuffling\u2026", "")
cpuScore = 0
youScore = 0
cpuScoreEl.textContent = "0"
youScoreEl.textContent = "0"
deck = shuffle(buildDeck())
remainingEl.textContent = deck.length + " cards remaining"
setTimeout(function () {
showResult("Draw to begin", "")
drawBtn.disabled = false
newDeckBtn.disabled = false
}, 450)
}
async function drawCards() {
if (deck.length < 2 || isFlipping) return
isFlipping = true
drawBtn.disabled = true
cpuCardInner.classList.remove("flipped")
youCardInner.classList.remove("flipped")
resultText.className = ""
await delay(80)
const cpuCard = deck.pop()
const youCard = deck.pop()
remainingEl.textContent = deck.length + " cards remaining"
cpuCardImg.src = makeCardSVG(cpuCard)
youCardImg.src = makeCardSVG(youCard)
await delay(130)
cpuCardInner.classList.add("flipped")
await delay(220)
youCardInner.classList.add("flipped")
await delay(560)
const ci = VALUES.indexOf(cpuCard.value)
const yi = VALUES.indexOf(youCard.value)
if (ci > yi) {
cpuScore++
cpuScoreEl.textContent = cpuScore
bumpScore(cpuScoreEl)
showResult("Dealer wins the round", "cpu-win")
flashPanel(cpuPanel, "winner-flash")
flashPanel(youPanel, "loser-flash")
} else if (yi > ci) {
youScore++
youScoreEl.textContent = youScore
bumpScore(youScoreEl)
showResult("You win the round!", "you-win")
flashPanel(youPanel, "winner-flash")
flashPanel(cpuPanel, "loser-flash")
} else {
showResult("\u2694 WAR \u2694", "war")
;[cpuPanel, youPanel].forEach(function (p) {
p.classList.add("war-shake")
p.addEventListener(
"animationend",
function () {
p.classList.remove("war-shake")
},
{ once: true },
)
})
}
if (deck.length === 0) {
await delay(1300)
endGame()
return
}
drawBtn.disabled = false
isFlipping = false
}
function endGame() {
get("final-cpu").textContent = cpuScore
get("final-you").textContent = youScore
var title, trophy
if (youScore > cpuScore) {
title = "You Win!"
trophy = "\uD83C\uDFC6"
} else if (cpuScore > youScore) {
title = "Dealer Wins"
trophy = "\uD83D\uDC80"
} else {
title = "It\u2019s a Tie!"
trophy = "\uD83E\uDD1D"
}
get("game-over-title").textContent = title
get("trophy-icon").textContent = trophy
overlay.classList.add("show")
}
newDeckBtn.addEventListener("click", newGame)
drawBtn.addEventListener("click", drawCards)
playAgainBtn.addEventListener("click", function () {
overlay.classList.remove("show")
newGame()
})
newGame()