-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path21game.js
264 lines (239 loc) · 5.96 KB
/
21game.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
$(document).ready(function() {
// reset button will reset the table
$('#reset').click(function() {
$('li').remove();
$('.pscore p').remove();
$('.score h1').remove();
$('.dealers_cards').css("height", "96px");
});
// start button will reset the table and start the game
$('#start').click(function() {
$('li').remove();
$('.pscore p').remove();
$('.score h1').remove();
playGame();
});
$('#hit').click(function() {
playerHand.hitMe("p");
result = firstResultCheck();
inputUserScore(result);
if(isNumeric(result)){
viewConsole();
} else {
hideConsole();
return;
}
});
$('#stand').click(function() {
while(dealerHand.score() < 17){
countingDealersCards = 0;
dealerHand.hitMe("b");
}
result = finalResultCheck();
$('.dealers_cards li').remove();
revealDealerHand(dealerHand);
inputUserScore(result);
hideConsole();
return;
});
});
//Card face finder
function cardFace(suit, figure){
suits = {1: "clubs", 2: "diamonds", 3: "hearts", 4: "spades"};
figures = {1: "ace", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "jack", 12: "queen", 13: "king"};
var c = figures[figure] + "_of_" + suits[suit] + ".svg";
return c;
}
//Deck_constructor
function deck(){
this.create = function(){
var cardArray = [];
var i = 1;
var j = 1;
for(i = 1; i < 14; i++){
for(j = 1; j < 5; j++){
cardArray.push(new Card(j, i));
}
}
return shuffle(shuffle(cardArray));
};
}
//check The Deck Constructor
function deckChecker(){
var array = new deck();
var array = array.create();
for(i = 0; i < 52; i++){
console.log(array[i].getNumber() + " of suit "+array[i].getSuit());
}
}
//Deck suffling
function shuffle(a) {
for (let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
[a[i - 1], a[j]] = [a[j], a[i - 1]];
}
return a;
}
//Card Constructor
function Card(suit, number){
var CardSuit = suit;
var CardNumber = number;
this.getSuit = function(){
return CardSuit;
};
this.getNumber = function(){
return CardNumber;
};
this.getValue = function(){
if( number === 1) {
return 11;
} else if( number > 9) {
return 10;
} else {
return number;
}
};
}
function revealDealerHand(hand){
var hand = hand.getHand();
for(i=0;i<hand.length;i++){
$('.dealers_cards ul').prepend('<li><a href="#"><img src="cards/' + cardFace(hand[i].getSuit(), hand[i].getNumber()) + '" /></a></li>');
}
}
// Deal function provides players with cards and prepend card images with jQuery
var deal = function(whos){
var newCard = gameDeck.pop();
if(whos == "b"){
countingDealersCards+= 1;
}
// I would like to automate the correct div selection, but it dosn't work for now.
if(whos == "p"){
$('.players_cards ul').prepend('<li><a href="#"><img src="cards/' + cardFace(newCard.getSuit(), newCard.getNumber()) + '" /></a></li>');
} else if(whos == "b" && countingDealersCards < 2) {
$('.dealers_cards').css("height", "");
$('.dealers_cards ul').prepend('<li><a href="#"><img src="cards/' + cardFace(newCard.getSuit(), newCard.getNumber()) + '" /></a></li>');
} else if(whos == "b" && countingDealersCards == 2){
$('.dealers_cards ul').prepend('<li><a href="#"><img src="cards/back.jpg" /></a></li>');
}
return newCard;
};
//Hand Object is keeping the score
function Hand(whos, howManyCards){
var who = whos;
var cardArray = [];
for(i = 0; i < howManyCards; i++) {
cardArray[i] = deal(who);
}
this.getHand = function() {
return cardArray;
};
this.score = function(){
var handSum = 0;
var numofaces = 0;
for(i=0;i<cardArray.length;i++){
handSum += cardArray[i].getValue();
if(cardArray[i].getNumber() === 1){
numofaces+=1;
}
}
if(handSum > 21 && numofaces!=0){
for(i=0;i<numofaces;i++){
if(handSum > 21){
handSum-=10;
}
}
}
return handSum;
};
this.printHand = function(){
var string = "";
for(i=0;i<cardArray.length;i++){
string = string + cardArray[i].getNumber() + " of suit "+cardArray[i].getSuit()+", ";
}
return string;
};
this.hitMe = function(whos){
cardArray.push(deal(whos));
this.getHand();
};
}
var finalResultCheck = function(){
var pS = playerHand.score();
var dS = dealerHand.score();
if(pS > 21){
if( dS >21){
return "Tide";
}
else{
return "Bust";
}
}
else if(dS>21){
return "Win";
}
else if(pS>dS){
return "Win";
}
else if(pS===dS){
return "Tide";
}
else{
return "Bust";
}
};
var inputUserScore = function(input){
$('.pscore p').remove();
$('.pscore').prepend("<p>" + input + "</p>");
}
var firstResultCheck = function(){
pS = playerHand.score();
dS = dealerHand.score();
if(pS > 21){
if( dS >21){
return "TideOver";
}
else{
return "Bust";
}
}
else if(dS>21){
return "Win";
}
else if(pS===21){
return "BJ";
}
else{
return pS;
}
};
var phaseOne = function(){
dealerHand = new Hand("b", 2);
playerHand = new Hand("p", 2);
result = firstResultCheck();
inputUserScore(result);
if(isNumeric(result)){
viewConsole();
} else {
hideConsole();
return;
}
};
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var viewConsole = function(){
$('.gameConsole').css("visibility", "");
}
var hideConsole = function(){
$('.gameConsole').css("visibility", "hidden");
}
var playGame = function(){
var gdeck = new deck();
countingDealersCards = 0;
// global variable
gameDeck = gdeck.create();
phaseOne();
//playerHand = playAsUser();
//dealerHand = playAsDealer();
//declareWinner(playerHand, dealerHand);
};