-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
67 lines (57 loc) · 1.42 KB
/
util.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
export function getBlackjackHandTotal(player)
{
// sum values of cards in `player` hand
// ace can count for either 1 or 11
// automatically count as 1 if counting as 11 would cause a bust
let hand = player.getHand()
let noAcesHand = hand.filter(card => card.getValue() !== 'ace')
let acesInHand = hand.filter(card => card.getValue() === 'ace')
// console.log(noAcesHand)
// console.log(acesInHand)
let total = 0
for (let card of noAcesHand)
{
total += +card.getValue() ? +card.getValue() : 10
}
for (let card of acesInHand)
{
if (total > 10)
{
total += 1
}
else
{
total += 11
}
}
// console.log(total)
return total
}
export function compareHands(player, dealer)
{
let playerTotal = getBlackjackHandTotal(player)
let dealerTotal = getBlackjackHandTotal(dealer)
// if player busts they lose
// if dealer busts, player only wins if player hasn't busted
if (playerTotal > 21)
playerTotal = 0
else if (playerTotal <= 21 && dealerTotal > 21)
dealerTotal = 0
if (playerTotal > dealerTotal)
// player wins - normal win or Blackjack
{
if (playerTotal === 21)
return 2
return 1
}
else if (playerTotal < dealerTotal)
// dealer wins
{
return -1
}
else
// tie
{
return 0
}
}