forked from rocketacademy/basics-blackjack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
387 lines (367 loc) · 6.81 KB
/
script.js
File metadata and controls
387 lines (367 loc) · 6.81 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
var myOutputValue;
var playerHand = [];
var computerHand = [];
var main = function (input) {
// User clicks submit button and gets two cards
var computerCard1 = deck.pop();
var playerCard1 = deck.pop();
var computerCard2 = deck.pop();
var playerCard2 = deck.pop();
console.log(computerHand);
console.log(playerHand);
playerHand.push(playerCard2, playerCard1);
computerHand.push(computerCard1, computerCard2);
var playerScore = countScore(playerHand);
var computerScore = countScore(computerHand);
// The cards drawn are displayed to the user.
myOutputValue =
"Computer drew: " +
computerCard1.name +
" of " +
computerCard1.suit +
" and " +
computerCard2.name +
" of " +
computerCard2.suit +
"whereas user drew " +
playerCard1.name +
" of " +
playerCard1.suit +
" and " +
playerCard2.name +
" of " +
playerCard2.suit;
console.log(myOutputValue);
// if computer score is less or equals to 17, computer takes another card and card is pushed to computer hand
if (Number(computerScore) <= 17) {
var computerCard3 = deck.pop();
computerHand.push(computerCard3);
myOutputValue = myOutputValue + `computer hits another card: ${computerCard3.name} of ${computerCard3.suit}`
computerScore = countScore(computerHand);
}
console.log(computerHand);
console.log(playerHand);
// blackjack functions
// if computerscore is exactly 21, computer wins
// if user is exactly 21, user wins
// if both are exactly 21, nobody wins
// if computerscore is more than 21 and user is less than 21 , computer burst and user wins
// if computerscore is more than 21 and userscore is more than 21, both loses
// if computerscore and player score is less than 21 and computer score is higher, computer wins
// if computerscore and player score is less than 21 and player score is higher, player wins
//
// if (Number(playerScore) > Number(computerScore) && Number(computerHand) <= 21) {
// myOutputValue = myOutputValue + `player wins!!!`
// }
// else if (Number(playerHand.rank) < Number(computerHand.rank) && Number(computerHand.rank) <= 21) {
// myOutputValue = myOutputValue + `computer wins!!!`
// }
// // game continues until we are out of cards
// playerScore = 0;
// computerScore = 0;
// if (deck.length == 0) {
// myOutputValue = "game over"
return myOutputValue;
};
// Then begins a new action, where the user has to decide whether to hit or stand, using the submit button to submit their choice.
// When the user makes a decision the cards are analyzed for any winning conditions.They are also analyzed for losing conditions, since it's possible for any player to lose now.
// The computer also decides to hit or stand automatically based on game rules.
// Either the game ends or continues.
// Deck is shuffled - global state
var getRandomIndex = function (size) {
return Math.floor(Math.random() * size);
};
// cards is an array of card objects
var shuffleCards = function (deck) {
var currentIndex = 0;
// loop over the entire cards array
while (currentIndex < deck.length) {
// select a random position from the deck
var randomIndex = getRandomIndex(deck.length);
// get the current card in the loop
var currentItem = deck[currentIndex];
// get the random card
var randomItem = deck[randomIndex];
// swap the current card and the random card
deck[currentIndex] = randomItem;
deck[randomIndex] = currentItem;
currentIndex = currentIndex + 1;
}
// give back the shuffled deck
return deck;
};
var countScore = function (hand) {
var index = 0;
var score = 0;
while (index < hand.length) {
index += 1;
console.log(index);
score = score + hand[index].rank;
console.log(hand[1]);
console.log(hand[index].rank)
}
return score
};
var deck = [
{
name: "ace",
suit: "hearts",
rank: 1,
},
{
name: "2",
suit: "hearts",
rank: 2,
},
{
name: "3",
suit: "hearts",
rank: 3,
},
{
name: "4",
suit: "hearts",
rank: 4,
},
{
name: "5",
suit: "hearts",
rank: 5,
},
{
name: "6",
suit: "hearts",
rank: 6,
},
{
name: "7",
suit: "hearts",
rank: 7,
},
{
name: "8",
suit: "hearts",
rank: 8,
},
{
name: "9",
suit: "hearts",
rank: 9,
},
{
name: "10",
suit: "hearts",
rank: 10,
},
{
name: "jack",
suit: "hearts",
rank: 10,
},
{
name: "queen",
suit: "hearts",
rank: 10,
},
{
name: "king",
suit: "hearts",
rank: 10,
},
{
name: "ace",
suit: "diamonds",
rank: 1,
},
{
name: "2",
suit: "diamonds",
rank: 2,
},
{
name: "3",
suit: "diamonds",
rank: 3,
},
{
name: "4",
suit: "diamonds",
rank: 4,
},
{
name: "5",
suit: "diamonds",
rank: 5,
},
{
name: "6",
suit: "diamonds",
rank: 6,
},
{
name: "7",
suit: "diamonds",
rank: 7,
},
{
name: "8",
suit: "diamonds",
rank: 8,
},
{
name: "9",
suit: "diamonds",
rank: 9,
},
{
name: "10",
suit: "diamonds",
rank: 10,
},
{
name: "jack",
suit: "diamonds",
rank: 10,
},
{
name: "queen",
suit: "diamonds",
rank: 10,
},
{
name: "king",
suit: "diamonds",
rank: 10,
},
{
name: "ace",
suit: "clubs",
rank: 1,
},
{
name: "2",
suit: "clubs",
rank: 2,
},
{
name: "3",
suit: "clubs",
rank: 3,
},
{
name: "4",
suit: "clubs",
rank: 4,
},
{
name: "5",
suit: "clubs",
rank: 5,
},
{
name: "6",
suit: "clubs",
rank: 6,
},
{
name: "7",
suit: "clubs",
rank: 7,
},
{
name: "8",
suit: "clubs",
rank: 8,
},
{
name: "9",
suit: "clubs",
rank: 9,
},
{
name: "10",
suit: "clubs",
rank: 10,
},
{
name: "jack",
suit: "clubs",
rank: 10,
},
{
name: "queen",
suit: "clubs",
rank: 10,
},
{
name: "king",
suit: "clubs",
rank: 10,
},
{
name: "ace",
suit: "spades",
rank: 1,
},
{
name: "2",
suit: "spades",
rank: 2,
},
{
name: "3",
suit: "spades",
rank: 3,
},
{
name: "4",
suit: "spades",
rank: 4,
},
{
name: "5",
suit: "spades",
rank: 5,
},
{
name: "6",
suit: "spades",
rank: 6,
},
{
name: "7",
suit: "spades",
rank: 7,
},
{
name: "8",
suit: "spades",
rank: 8,
},
{
name: "9",
suit: "spades",
rank: 9,
},
{
name: "10",
suit: "spades",
rank: 10,
},
{
name: "jack",
suit: "spades",
rank: 10,
},
{
name: "queen",
suit: "spades",
rank: 10,
},
{
name: "king",
suit: "spades",
rank: 10,
},
];
var shuffledDeck = shuffleCards(deck)