-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.js
153 lines (132 loc) · 3.27 KB
/
types.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
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
const BUST_LIMIT = 21;
// Fake enum in js
class Suit {
constructor(name) {
this.name = name;
}
static CLUBS = new Suit("clubs");
static DIAMONDS = new Suit("diamonds");
static HEARTS = new Suit("hearts");
static SPADES = new Suit("spades");
}
// Fake enum in js
class Rank {
constructor(name, value) {
this.name = name;
this.value = value;
}
static TWO = new Rank("two", 2);
static THREE = new Rank("three", 3);
static FOUR = new Rank("four", 4);
static FIVE = new Rank("five", 5);
static SIX = new Rank("six", 6);
static SEVEN = new Rank("seven", 7);
static EIGHT = new Rank("eight", 8);
static NINE = new Rank("nine", 9);
static TEN = new Rank("ten", 10);
static JACK = new Rank("jack", 10);
static QUEEN = new Rank("queen", 10);
static KING = new Rank("king", 10);
static ACE = new Rank("ace", [1, 11]);
}
class Deck {
constructor() {
this.cards = [];
}
init() {
// Fill deck with standard 52 cards of every rank and suit
this.cards = [];
for (let rank in Rank) {
for (let suit in Suit) {
this.cards.push(new Card(Rank[rank], Suit[suit]));
}
}
}
deal() {
return this.cards.pop()
}
shuffle() {
// Following Fisher-Yates method
for (let i = this.cards.length-1; i>0; i--) {
const randIndex = ~~(Math.random()*(i+1));
const swap = this.cards[i];
this.cards[i] = this.cards[randIndex];
this.cards[randIndex] = swap;
}
}
}
class Card {
constructor(rank, suit, show=true) {
this.rank = rank,
this.suit = suit,
this.show = show
}
flip() {
this.show = !this.show;
}
}
class Hand {
constructor(cards) {
this.cards = [...cards];
}
isBust() {
return this.getTotal() > BUST_LIMIT;
}
getTotal() {
let total = 0;
let aces = []
for (let card of this.cards) {
if (card.rank.name === "ace") aces.push(card);
else total += card.rank.value;
}
// Handle aces last
for (let ace of aces) {
const aceValues = ace.rank.value;
total += total+aceValues[1] > BUST_LIMIT ? aceValues[0] : aceValues[1];
}
return total
}
draw(card) {
this.cards.push(card);
}
clear() {
this.cards = [];
}
}
class Blackjack {
constructor() {
this.deck = new Deck();
this.playerHand = new Hand([]);
this.dealerHand = new Hand([]);
this.money = 1000;
this.bet = 100;
}
resetTable() {
this.playerHand.clear();
this.dealerHand.clear();
this.deck.init();
this.deck.shuffle();
}
playerHit(show=true) {
this.hit(this.playerHand, show);
}
dealerHit(show=true) {
this.hit(this.dealerHand, show);
}
hit(hand, show=true) {
let card = this.deck.deal();
if (!show) card.flip();
hand.draw(card);
}
setBet(value) {
this.bet = value;
}
win() {
this.money += this.bet;
this.bet = 100;
}
lose() {
this.money -= this.bet;
this.bet = 100;
}
}