-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.js
96 lines (86 loc) · 3.36 KB
/
blackjack.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
/* jslint esversion : 8 */
const readline = require('readline');
const cards = [10, 10, 10, 'ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 'ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 'ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 'ace', 2, 3, 4, 5, 6, 7, 8, 9, 10];
let read = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = (str) => new Promise(resolve => read.question(str, resolve));
const game = async () =>
{
let playerHand = 0;
let dealerHand = 0;
let count = 0;
for (let i = 0; i < 2; i++)
{
let cardValue = cards[Number.parseInt(Math.random() * cards.length)];
if (cardValue != 'ace')
playerHand += cardValue;
else
{
let res = await question("you got an Ace. Do you want 1 or 11? ");
while (res != 11 && res != 1)
{
res = await question("you got an Ace. Do you want 1 or 11? ");
}
playerHand += Number.parseInt(res);
}
console.log("Player hand is: " + playerHand);
}
for (let i = 0; i < 19; i++)
{
let cardValue = cards[Number.parseInt(Math.random() * cards.length)];
if (playerHand == 21) { console.log("Player WIN!!!"); break; }
else if (playerHand > 21) { console.log("Dealer WIN!!!"); break; }
let res = await question("hit (h) or stand (s)? ");
res = String(res).toLowerCase();
while (res != 'stand' && res != 's' && res != 'hit' && res != 'h')
{
res = await question("hit (h) or stand (s)? ");
}
if (res === 'hit' || res === 'h')
{
if (cardValue == 'ace')
{
let res = await question("you got an Ace. Do you want 1 or 11? ");
playerHand += Number.parseInt(res);
} else playerHand += cardValue;
console.log("Player hand is: " + playerHand);
}
else if (res === 'stand' || res === 's')
{
console.log("you are standing and your hand is: " + playerHand);
break;
}
}
if (playerHand < 21)
while (count != 1)
{
let cardValue = cards[Number.parseInt(Math.random() * cards.length)];
if (cardValue == 'ace')
{
if ((dealerHand + 11) > 21) dealerHand += 1;
else dealerHand += 11;
} else dealerHand += cardValue;
console.log("Dealer hand is: " + dealerHand);
if (dealerHand === 21) { console.log("Dealer WIN!!!"); break; }
else if (dealerHand < 21 && dealerHand > playerHand) { console.log("Dealer WIN!!!"); break; }
else if (dealerHand > 21) { console.log("Player WIN!!!"); break; }
else if (dealerHand === playerHand) { console.log("Tie"); break; }
}
if (playerHand > 0 && dealerHand > 0)
console.log(`Dealer hand: ${dealerHand}, Player hand: ${playerHand}`);
let res = await question("Play again? yes (y) no (n): ");
res = String(res).toLowerCase();
while (res != 'yes' && res != 'y' && res != 'no' && res != 'n')
{
res = await question("Play again? yes (y) no (n): ");
}
if (res === 'yes' || res === 'y') return game();
else if (res === 'no' || res === 'n')
{
console.log("Thank you for playing!!");
process.exit(0);
}
};
game();