-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathblackjack.js
293 lines (246 loc) · 6.86 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
/*
Blackjack 21
A simple game developed using Javascript, HTML and CSS
@author Stayko Chalakov
@version 1.0
@date 29.06.2017
*/
//namespacing
var BlackjackJS = (function() {
/**************
Card class
***************/
/*
Constructor
@param {String} rank
@param {String} suit
*/
function Card(rank, suit){
this.rank = rank;
this.suit = suit;
}
/*
Gets the value or points of the card
@param {Integer} currentTotal - The current total score of the
player's hand
*/
Card.prototype.getValue = function(currentTotal){
var value = 0;
if (this.rank == 'A' && currentTotal < 11){
value = 11;
} else if (this.rank == 'A'){
value = 1;
} else if (this.rank == 'J' || this.rank == 'Q' || this.rank == 'K'){
value = 10;
} else {
value = parseInt(this.rank);
}
return value;
}
/*******************
Renders the card
*******************/
Card.prototype.view = function(){
var htmlEntities = {
'hearts' : '♥',
'diamonds' : '♦',
'clubs' : '♣',
'spades' : '♠'
}
return `
<div class="card ` + this.suit + `">
<div class="top rank">` + this.rank + `</div>
<div class="suit">` + htmlEntities[this.suit] + `</div>
<div class="bottom rank">` + this.rank + `</div>
</div>
`;
}
/*************************** End of Card class ********************************/
/***************
Player class
***************/
/*
Constructor
@param {String} element - The DOM element
@param {Array} hand - the array which holds all the cards
*/
function Player(element, hand){
this.hand = hand;
this.element = element;
}
/*
Hit player with new card from the deck
@param {Card} card - the card to deal to the player
*/
Player.prototype.hit = function(card){
this.hand.push(card);
}
/*
Returns the total score of all the cards in the hand of a player
*/
Player.prototype.getScore = function(){
var points = 0;
for(var i = 0; i < this.hand.length; i++){
if(i == 0) points = this.hand[i].getValue(0);
else points += this.hand[i].getValue(points);
}
return points;
}
/*
Returns the array (hand) of cards
*/
Player.prototype.showHand = function(){
var hand = "";
for(var i = 0; i < this.hand.length; i++){
hand += this.hand[i].view();
}
return hand;
}
/*************************** End of Player class ******************************/
/*************************
Deck - Singleton class
*************************/
var Deck = new function(){
this.ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
this.suits = ['hearts', 'spades', 'diamonds','clubs'];
this.deck;
/*
Fills up the deck array with cards
*/
this.init = function(){
this.deck = []; //empty the array
for(var s = 3; s >= 0; s--){
for(var r = 12; r >= 0; r--){
this.deck.push(new Card(this.ranks[r], this.suits[s]));
}
}
}
/*
Shuffles the cards in the deck randomly
*/
this.shuffle = function(){
var j, x, i;
for (i = this.deck.length; i; i--) {
j = Math.floor(Math.random() * i);
x = this.deck[i - 1];
this.deck[i - 1] = this.deck[j];
this.deck[j] = x;
}
}
}
/**************************** End of Deck class *******************************/
/*************************
Game - Singleton class
**************************/
var Game = new function(){
/*
Deal button event handler
*/
this.dealButtonHandler = function(){
Game.start();
this.dealButton.disabled = true;
this.hitButton.disabled = false;
this.standButton.disabled = false;
}
/*
Hit button event handler
*/
this.hitButtonHandler = function(){
//deal a card and add to player's hand
var card = Deck.deck.pop();
this.player.hit(card);
//render the card and score
document.getElementById(this.player.element).innerHTML += card.view();
this.playerScore.innerHTML = this.player.getScore();
//if over, then player looses
if(this.player.getScore() > 21){
this.gameEnded('You lost!');
}
}
/*
Stand button event handler
*/
this.standButtonHandler = function(){
this.hitButton.disabled = true;
this.standButton.disabled = true;
//deals a card to the dealer until
//one of the conditions below is true
while(true){
var card = Deck.deck.pop();
this.dealer.hit(card);
document.getElementById(this.dealer.element).innerHTML += card.view();
this.dealerScore.innerHTML = this.dealer.getScore();
var playerBlackjack = this.player.getScore() == 21,
dealerBlackjack = this.dealer.getScore() == 21;
//Rule set
if(dealerBlackjack && !playerBlackjack) {
this.gameEnded('You lost!');
break;
} else if(dealerBlackjack && playerBlackjack) {
this.gameEnded('Draw!');
break;
} else if(this.dealer.getScore() > 21 && this.player.getScore() <= 21) {
this.gameEnded('You won!');
break;
} else if(this.dealer.getScore() > this.player.getScore() && this.dealer.getScore() <= 21 && this.player.getScore() < 21) {
this.gameEnded('You lost!');
break;
}
//TODO needs to be expanded..
}
}
/*
Initialise
*/
this.init = function(){
this.dealerScore = document.getElementById('dealer-score').getElementsByTagName("span")[0];
this.playerScore = document.getElementById('player-score').getElementsByTagName("span")[0];
this.dealButton = document.getElementById('deal');
this.hitButton = document.getElementById('hit');
this.standButton = document.getElementById('stand');
//attaching event handlers
this.dealButton.addEventListener('click', this.dealButtonHandler.bind(this));
this.hitButton.addEventListener('click', this.hitButtonHandler.bind(this));
this.standButton.addEventListener('click', this.standButtonHandler.bind(this));
}
/*
Start the game
*/
this.start = function(){
//initilaise and shuffle the deck of cards
Deck.init();
Deck.shuffle();
//deal one card to dealer
this.dealer = new Player('dealer', [Deck.deck.pop()]);
//deal two cards to player
this.player = new Player('player', [Deck.deck.pop(), Deck.deck.pop()]);
//render the cards
document.getElementById(this.dealer.element).innerHTML = this.dealer.showHand();
document.getElementById(this.player.element).innerHTML = this.player.showHand();
//renders the current scores
this.dealerScore.innerHTML = this.dealer.getScore();
this.playerScore.innerHTML = this.player.getScore();
this.setMessage("Hit or Stand");
}
/*
If the player wins or looses
*/
this.gameEnded = function(str){
this.setMessage(str);
this.dealButton.disabled = false;
this.hitButton.disabled = true;
this.standButton.disabled = true;
}
/*
Instructions or status of game
*/
this.setMessage = function(str){
document.getElementById('status').innerHTML = str;
}
}
//Exposing the Game.init function
//to the outside world
return {
init: Game.init.bind(Game)
}
})()