-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathblackjack.js
302 lines (254 loc) · 8.23 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
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
var bj = (function() {
function Game(status,playerTurn,dealerHand,playerHand){
this.status = status; // 0 game does not exist, 1 unfinished game exits
this.playerTurn = playerTurn // true is player, false is dealer
this.deck;
this.bust = 21;
this.winner = null; // 0 = dealer, 1 = player, 2 = tie
this.message;
this.dealer = {
hand : dealerHand,
handTotal : 0,
bust : false
}
this.player = {
hand : playerHand,
handTotal : 0,
bust : false
}
// WINING CASES
var BJ_WIN = "blackjack";
var POINT_WIN = "afterHit";
this.createDeck = function(){
var initialDeck = new casino.StandardShuffledDeck();
var deck = new casino.ShuffledShoe(initialDeck, 5);
this.adjustValues = function(){
for (var i = 0; i < deck.length; i++){
var card_gameValue = deck[i].extra.game_value;
if (card_gameValue >= 10 && card_gameValue <= 13){
deck[i].extra.game_value = 10;
}
}
for (var i = 0; i < deck.length; i++){
var card_gameValue = deck[i].extra.game_value;
if (card_gameValue === 14){
deck[i].extra.game_value = 11;
}
}
}
this.adjustValues();
this.deck = deck;
}
this.deal = function(){
var DEAL_COUNT = 4;
var player = true;
for (var i = 0; i < DEAL_COUNT; i++){
var newCard = casino.draw(1, this.deck);
if (player) {
this.player.hand = casino.Hand(newCard, this.player.hand);
this.faceUp(this.player.hand);
player = false;
} else {
this.dealer.hand = casino.Hand(newCard, this.dealer.hand);
player = true;
}
}
this.faceUp(this.dealer.hand);
this.dealer.hand[1].extra.face_up = false;
this.player.handTotal = this.getHandTotal(this.player.hand);
this.dealer.handTotal = this.getHandTotal(this.dealer.hand);
this.checkPoints(BJ_WIN); // check if any player wins with a natural Blackjack
}
this.hit = function(){
var newCard = casino.draw(1, this.deck);
if (this.playerTurn){
this.player.hand = casino.Hand(newCard, this.player.hand);
this.faceUp(this.player.hand, this.player.hand.length-1);
this.player.handTotal = this.getHandTotal(this.player.hand);
this.isBust(this.player.handTotal);
} else {
this.dealer.hand = casino.Hand(newCard, this.dealer.hand);
this.faceUp(this.dealer.hand, this.dealer.hand.length-1);
this.dealer.handTotal = this.getHandTotal(this.dealer.hand);
this.isBust(this.dealer.handTotal);
this.dealerAction();
}
}
this.getHandTotal = function(hand){
this.hand = hand;
var totalValue = 0;
for (var i = 0; i < this.hand.length; i++){
var cardValue = this.hand[i].extra.game_value;
totalValue = totalValue + cardValue;
}
if (totalValue > this.bust){
totalValue = this.adjustForAces(this.hand, totalValue);
}
this.hand = [];
return totalValue;
}
this.stand = function(){
this.toggleTurn();
this.faceUp(this.dealer.hand, 1); // turn the second dealer's card face up
this.dealerAction();
}
///// HELPERS /////
// determine dealer's action based on handTotal
this.dealerAction = function(){
var dealerHandTotal = this.dealer.handTotal;
if (dealerHandTotal <= 16){
this.hit();
} else if (dealerHandTotal >= 17 && dealerHandTotal <= this.bust){
this.checkPoints(BJ_WIN);
if (this.winner === null){
this.checkPoints(POINT_WIN);
}
}
}
// toggle player's turn
this.toggleTurn = function(){
this.playerTurn = !this.playerTurn;
}
// adjust hand value if hand total exceeds 21 and Aces are found
this.adjustForAces = function(hand, totalValue){
var count = 0;
for (var i = 0; i < hand.length; i++){
if (hand[i].extra.game_value === 11){
count++;
}
}
if (count > 0){
deductPoints();
while (totalValue > this.bust && count > 0){
deductPoints();
}
}
function deductPoints(){
totalValue = totalValue - 10;
count--;
}
return totalValue;
}
// check if hand total exceeds 21
this.isBust = function(handTotal){
// if it's player's turn, and he has busted, dealer wins
// if it's dealer's turn, and he has busted, player wins
if (handTotal > this.bust && this.playerTurn){
this.player.bust = true;
this.faceUp(this.dealer.hand,1);
this.declareWinner("dealer");
} else if (handTotal > this.bust && !this.playerTurn) {
this.dealer.bust = true;
this.declareWinner("player");
}
}
// if no bust, check who has a better hand value
this.checkPoints = function(situation){
if (situation === "blackjack"){
if (this.player.handTotal === this.bust && this.dealer.handTotal !== this.bust){
this.declareWinner("playerBJ");
this.faceUp(this.dealer.hand,1);
} else if (this.player.handTotal === this.bust && this.dealer.handTotal === this.bust){
this.declareWinner("tieBJ");
this.faceUp(this.dealer.hand,1);
} else if (this.dealer.handTotal === this.bust && this.player.handTotal !== this.bust){
this.declareWinner("dealerBJ");
this.faceUp(this.dealer.hand,1);
}
} else if (situation === "afterHit"){
if (this.dealer.handTotal > this.player.handTotal || this.dealer.handTotal === this.bust){
this.declareWinner("dealer");
this.faceUp(this.dealer.hand,1);
} else if (this.dealer.handTotal < this.player.handTotal || this.player.handTotal === this.bust){
this.declareWinner("player");
this.faceUp(this.dealer.hand,1);
} else {
this.declareWinner("tie");
this.faceUp(this.dealer.hand,1);
}
}
}
// store the announcement on who's the winner and how
this.declareWinner = function(situation){
var message;
if (situation === "playerBJ"){
this.winner = 1;
message = "You won with a Blackjack";
} else if (situation === "dealerBJ"){
this.winner = 0;
message = "Dealer won with a Blackjack";
} else if (situation === "tieBJ"){
message = "It's a tie of two Blackjack pairs";
} else if (situation === "dealer"){
this.winner = 0;
if (this.player.handTotal > this.bust){
message = "You busted! Dealer won with a score " + this.dealer.handTotal + ':' + this.player.handTotal;
} else {
message = "Dealer won with a score " + this.dealer.handTotal + ':' + this.player.handTotal;
}
} else if (situation === "player") {
this.winner = 1;
if (this.dealer.handTotal > this.bust) {
message = "Dealer busted! You won with a score " + this.player.handTotal + ':' + this.dealer.handTotal;
} else {
message = "You won with a score " + this.player.handTotal + ':' + this.dealer.handTotal;
}
} else if (situation === "tie"){
this.winner = 2;
message = "It's a tie with a score " + this.player.handTotal + ':' + this.dealer.handTotal;
}
this.warning(message);
}
// formulate a message to show
this.warning = function(message){
this.message = message;
}
// change value of face-up for any card
this.faceUp = function(hand, index){
if (index === undefined){
index = hand.length-1;
}
for (var i = index; i >= 0; i--){
hand[i].extra.face_up = true;
}
}
// create the new Game
this.newGame = function(){
this.dealer.hand = casino.clearHand();
this.dealer.handTotal = 0;
this.player.hand = casino.clearHand();
this.player.handTotal = 0;
this.playerTurn = true;
this.status = 1;
this.winner = null;
this.createDeck();
}
// removes cards from newly created deck that players already have in their hands
this.adjustDeck = function(){
var allCards = this.dealer.hand.concat(this.player.hand)
for (var i = 0; i < allCards.length; i++){
var value = allCards[i].value;
var suit = allCards[i].suit;
function findCard(card){
return card.value === value && card.suit === suit
}
var cardToDelete = this.deck.find(findCard);
var index = this.deck.indexOf(cardToDelete);
this.deck.splice(index,1);
}
}
///// GAME START /////
if (status === 0){
this.newGame();
} else if (status === 1){
this.createDeck();
this.adjustDeck();
this.dealer.handTotal = this.getHandTotal(this.dealer.hand); // calculate handTotals
this.player.handTotal = this.getHandTotal(this.player.hand);
}
} // Game object end
var module = {
"Game": Game
}
return module
})();