-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
285 lines (231 loc) · 5.02 KB
/
main.js
File metadata and controls
285 lines (231 loc) · 5.02 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { createCard, randomCard } from "./card.js";
import error from "./error.js";
import "./fullscreen.js";
let balance = 500;
let bet = 0;
let playing = false;
/** @type {String[]} */
const bankCards = [];
/** @type {String[]} */
const playerCards = [];
/**
* Elements to target by ID faster
* @type {Object<string, HTMLElement>}
*/
const elements = {};
[
10,
20,
50,
100
].forEach(n => {
const id = "bet-" + n;
const element = document.getElementById(id);
elements[id] = element;
element.addEventListener("click", () => addBet(n));
});
[
"bet",
"reset",
"bank-cards",
"bank-total",
"player-cards",
"player-total",
"player-bet",
"player-balance",
"controls-playing",
"controls-betting"
].forEach(id => elements[id] = document.getElementById(id));
// Reset bet button
elements["reset"].addEventListener("click", () => {
if (playing) return;
balance += bet;
bet = 0;
update();
});
// Confirm bet button
elements["bet"].addEventListener("click", () => {
if (playing) return;
if (!bet) return error("You must bet some money!");
playing = true;
startGame();
});
/**
* Add a bet from balance
* @param {Number} bet - The bet to add
*/
function addBet(toBet) {
if (playing) return;
if (toBet > balance) return error("You do not have enough money!");
balance -= toBet;
bet += toBet;
update();
}
/** Update UI with up-to-date values */
function update() {
elements["player-bet"].innerText = bet;
elements["player-balance"].innerText = balance;
elements["bank-total"].innerText = getTotal("bank");
elements["player-total"].innerText = getTotal("player");
elements["bank-cards"].innerHTML = "";
elements["player-cards"].innerHTML = "";
for (const card of bankCards) {
elements["bank-cards"].appendChild(createCard(card));
}
for (const card of playerCards) {
elements["player-cards"].appendChild(createCard(card));
}
if (playing) {
elements["controls-playing"].style.display = "";
elements["controls-betting"].style.display = "none";
} else {
elements["controls-playing"].style.display = "none";
elements["controls-betting"].style.display = "";
}
}
/** Start a game */
async function startGame() {
playerCards.push(randomCard());
update();
let playerIsPlaying = true;
// Check for blackjack
if (getTotal("player") === 21) {
bankCards.push(randomCard());
update();
if (getTotal("bank") === 21) {
error("You lost.");
} else {
balance += bet * 4;
error("BLACKJACK!");
}
return resetGame();
}
while (playerIsPlaying) {
const action = await playerAction();
switch (action) {
case "hit":
playerCards.push(randomCard());
break;
case "stay":
playerIsPlaying = false;
break;
case "double":
if (bet > balance) {
error("You don't have enough money left!");
break;
}
balance -= bet;
bet *= 2;
playerCards.push(randomCard());
playerIsPlaying = false;
break;
}
update();
if (getTotal("player") > 21) break;
}
if (getTotal("player") > 21) {
error("You lost.");
return resetGame();
}
while (true) {
bankCards.push(randomCard());
update();
const bankTotal = getTotal("bank");
const playerTotal = getTotal("player");
if (bankTotal > 21) {
error("You won!");
balance += bet * 2;
break;
}
if (bankTotal > 15) {
if (bankTotal > playerTotal) {
error("You lost.");
} else if (bankTotal < playerTotal) {
error("You won!");
balance += bet * 2;
} else {
error("Tie!");
balance += bet;
}
break;
}
}
resetGame();
}
/**
* Get the move of the player
* @returns {Promise<"stay"|"hit"|"double">} Action the player want to do
*/
function playerAction() {
const actions = [
"stay",
"hit",
"double"
];
/** @type {Object<string, Function>} */
const functions = {};
return new Promise(resolve => {
for (const action of actions) {
const fn = () => {
for (const action of actions) {
document.getElementById(action).removeEventListener("click", functions[actions]);
}
resolve(action);
}
functions[action] = fn;
document.getElementById(action).addEventListener("click", fn);
}
});
};
/** Reset the cards and be ready for a new game */
function resetGame() {
bet = 0;
bankCards.length = 0;
playerCards.length = 0;
bankCards.push(randomCard());
playerCards.push(randomCard());
playing = false;
update();
}
resetGame();
/**
* Get total value of cards
* @param {"bank"|"player"} who
* @returns {Number}
*/
function getTotal(who) {
/** @type {String[]} */
let cards;
let total = 0;
switch (who) {
case "bank":
cards = bankCards;
break;
case "player":
cards = playerCards;
break;
default:
throw new Error("'who' must be 'bank' or 'player', not '" + who + "'.");
}
for (const card of cards) {
let value = parseInt(card);
if (value) {
total += value;
continue;
}
switch (card) {
case "A":
total += 1;
break;
case "J":
case "Q":
case "K":
total += 10;
break;
default:
throw new Error("Unknown card");
}
}
if (cards.includes("A") && total < 12) total += 10;
return total;
}