-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
83 lines (69 loc) · 1.86 KB
/
index.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
/* jshint asi: true */
var _ = require('lodash')
var Shuffle = require('shuffle')
var chalk = require('chalk')
var deck = [];
_.times(10, function() {
deck = deck.concat(Shuffle.shuffle().cards)
});
var blackjackDeck = Shuffle.shuffle({deck: deck})
function unicard(card) {
return card.toShortDisplayString()
.replace('C', chalk.bold.black('♣︎'))
.replace('S', chalk.bold.black('♠︎'))
.replace('H', chalk.red('♥︎'))
.replace('D', chalk.red('♦︎'));
}
var players = {}
var dealer = []
function handTotal(hand, total){
if(hand.length === 0) return total
if(!total) total = 0;
card = hand[0];
if(card.sort === 14) { // Ace
var fullValue = handTotal(_.rest(hand), total + 11)
if(fullValue <= 21) {
return fullValue
} else {
return handTotal(_.rest(hand), total + 1)
}
} else {
return handTotal(_.rest(hand), total + Math.min(card.sort, 10))
}
}
function checkBust(player) {
return handTotal(player) > 21;
}
function hit(player) {
blackjackDeck.deal(1, players[player])
return checkBust(players[player]);
}
function dealerTurn() {
if(checkBust(dealer)) { // Dealer Bust
// Handle bust
} else if(handTotal(dealer) >= 17) { // Dealer stands
compareHands();
} else { // Dealer hits
blackjackDeck.deal(1, [dealer]);
dealerTurn();
}
}
function compareHands() {
if (checkBust(dealer)) {
return players.length
}
var wins = 0,
pushes = 0,
losses = 0
for (var playerName in players) {
var playerScore = handTotal(player)
var dealerScore = handTotal(dealer)
if (playerScore > dealerScore) {
wins++
} else if (playerScore < dealerScore) {
losses++
} else {
pushes++
}
}
}