-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
400 lines (293 loc) · 10.9 KB
/
app.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
var bet = 0;
window.onload = function() {
var card = function(suit, cardValue, nameOfCard, cardImage) { //constructor for cards
return {
suit: suit,
cardValue: cardValue,
nameOfCard: nameOfCard,
cardImage: cardImage,
}
}
var player = function(chips) {
return {
chips: 100,
}
}
var deckOfCards = [
aceOfHearts = new card("Hearts", 1 || 11, "Ace of Hearts", "CSS/images/AceImage.jpg"),
twoOfHearts = new card("Hearts", 2, "Two of Hearts"),
threeOfHearts = new card("Hearts", 3, "Three of Hearts"),
fourOfHearts = new card("Hearts", 4, "Four of Hearts"),
fiveOfHearts = new card("Hearts", 5, "Five of Hearts"),
sixOfHearts = new card("Hearts", 6, "Six of Hearts"),
sevenOfHearts = new card("Hearts", 7, "Seven of Hearts"),
eightOfHearts = new card("Hearts", 8, "Eight of Hearts"),
nineOfHearts = new card("Hearts", 9, "Nine of Hearts"),
tenOfHearts = new card("Hearts", 10, "Ten of Hearts"),
jackofHearts = new card("Hearts", 10, "Jack of Hearts"),
queenOfHearts = new card("Hearts", 10, "Queen of Hearts"),
kingOfHearts = new card("Hearts", 10, "King of Hearts"),
aceOfClubs = new card("Clubs", 1 || 11, "Ace of Clubs", "CSS/images/AceImage.jpg"),
twoOfClubs = new card("Clubs", 2, "Two of Clubs"),
threeOfClubs = new card("Clubs", 3, "Three of Clubs"),
fourOfClubs = new card("Clubs", 4, "Four of Clubs"),
fiveOfClubs = new card("Clubs", 5, "Five of Clubs"),
sixOfClubs = new card("Clubs", 6, "Six of Clubs"),
sevenOfClubs = new card("Clubs", 7, "Seven of Clubs"),
eightOfClubs = new card("Clubs", 8, "Eight of Clubs"),
nineOfClubs = new card("Clubs", 9, "Nine of Clubs"),
tenOfClubs = new card("Clubs", 10, "Ten of Clubs"),
jackofClubs = new card("Clubs", 10, "Jack of Clubs"),
queenOfClubs = new card("Clubs", 10, "Queen of Clubs"),
kingOfClubs = new card("Clubs", 10, "King of Clubs"),
aceOfDiamonds = new card("Diamonds", 1 || 11, "Ace of Diamonds", "CSS/images/AceImage.jpg"),
twoOfDiamonds = new card("Diamonds", 2, "Two of Diamonds"),
threeOfDiamonds = new card("Diamonds", 3, "Three of Diamonds"),
fourOfDiamonds = new card("Diamonds", 4, "Four of Diamonds"),
fiveOfDiamonds = new card("Diamonds", 5, "Five of Diamonds"),
sixOfDiamonds = new card("Diamonds", 6, "Six of Diamonds"),
sevenOfDiamonds = new card("Diamonds", 7, "Seven of Diamonds"),
eightOfDiamonds = new card("Diamonds", 8, "Eight of Diamonds"),
nineOfDiamonds = new card("Diamonds", 9, "Nine of Diamonds"),
tenOfDiamonds = new card("Diamonds", 10, "Ten of Diamonds"),
jackofDiamonds = new card("Diamonds", 10, "Jack of Diamonds"),
queenOfDiamonds = new card("Diamonds", 10, "Queen of Diamonds"),
kingOfDiamonds = new card("Diamonds", 10, "King of Diamonds"),
aceOfSpades = new card("Spades", 1 || 11, "Ace of Spades", "CSS/images/AceImage.jpg"),
twoOfSpades = new card("Spades", 2, "Two of Spades"),
threeOfSpades = new card("Spades", 3, "Three of Spades"),
fourOfSpades = new card("Spades", 4, "Four of Spades"),
fiveOfSpades = new card("Spades", 5, "Five of Spades"),
sixOfSpades = new card("Spades", 6, "Six of Spades"),
sevenOfSpades = new card("Spades", 7, "Seven of Spades"),
eightOfSpades = new card("Spades", 8, "Eight of Spades"),
nineOfSpades = new card("Spades", 9, "Nine of Spades"),
tenOfSpades = new card("Spades", 10, "Ten of Spades"),
jackofSpades = new card("Spades", 10, "Jack of Spades"),
queenOfSpades = new card("Spades", 10, "Queen of Spades"),
kingOfSpades = new card("Spades", 10, "King of Spades")
];
// Test: console.log(deckOfCards);
chipCounter = 100;
numberCounter = 0;
dealerNumberCounter = 0;
var usedCards = [];
// var betFunction = function() {
// if (chipCounter === 100) {
// document.getElementbyId("bet-submit");
// $("#chipCounterID").html("bet")
// }
// }
// $("#chipCounterID").append(betFunction);
// function total() {
// var bet = document.forms["betForm"]["sum1"].value;
// var display=document.getElementbyId("display");
// display.innerHTML=parseInt(bet);
// return false;
// }
$("#submit").click(function () {
bet = $("#sum1").val();
// console.log($("#sum1").val());
chipCounter -= bet;
console.log(chipCounter);
var $yourScoreDiv = $('#yourScore');
console.log($yourScoreDiv);
var $chipCounterIDDiv = $("#chipCounterID");
// $chipCounterIDDiv.empty();
var $gameDialogue = $('#game-dialogue');
$gameDialogue.empty();
$gameDialogue.html("You have " + chipCounter + " chips left.");
// var $betCounterDiv = $("#betCounter");
// $betCounterDiv.append($chipCounterIDDiv);
var $chipCounterDiv = $('#chipCounter');
// $betCounterDiv.append($chipCounter;
console.log(chipCounter);
});
// var $bet = $("#sum1").val();
// console.log($bet);
// Start of game -- First hit
$("#pCard1").click(function() {
//deals the player's card on clicking on player's card image/div
// Tested: alert("this works");
var random = (Math.floor(Math.random() * deckOfCards.length));
//get a random card from deck by calling a random number and finding that point in the array, then logging its name and adding its value to the player's total
// console.log(random);
// this.innerHTML = (deckOfCards[random].cardImage);
this.innerHTML = (deckOfCards[random].nameOfCard);
usedCards.push(deckOfCards[random]);
// console.log(usedCards);
// if (usedCards)
//makes div innerHTML name of card
var newNumber = (deckOfCards[random].cardValue);
//finds card value of random card
// if (newNumber = 1 || 11) {
// alert("this works")
// } else {
// }
var numberPlusCard = (numberCounter += newNumber);
// finds new number (old score plus new card value)
// Tested: console.log(numberPlusCard);
$("#counter").empty().append(numberPlusCard); //<----.empty = personal triumph
//print new score to div
// Test: var random = (Math.floor(Math.random() * deckOfCards.length));
// Test: console.log(random);
// var removeThis = deckOfCards[random];
// Test: console.log(removeThis);
if (numberPlusCard === 21) {
alert("You win!");
(player.chips +=$("#sum1").val());
console.log(player.chips);
} else if (numberPlusCard > 21) {
alert("Your luck's run out.")
}
var removed = deckOfCards.splice(random, 1);
//removes dealt card
//Test: console.log(removed.name);
//Test: console.log(deckOfCards);
});
//========New function=======
// $("#pCard1").click(function() {
//dealerNumberCounter = 0; //dealing card to dealer
// var nextRound = function() {
$("#dCard1").click(function() {
var dealerRandom = (Math.floor(Math.random() * deckOfCards.length));
// console.log(dealerRandom);
this.innerHTML = (deckOfCards[dealerRandom].nameOfCard);
var newDealerNumber = (deckOfCards[dealerRandom].cardValue);
// console.log(newDealerNumber)
var numberDealerPlusCard = (dealerNumberCounter += newDealerNumber);
console.log(numberDealerPlusCard);
$("#dealerCounter").empty().append(numberDealerPlusCard);
if (numberDealerPlusCard < 17) {
$("#dealerCounter").empty().append(numberDealerPlusCard);
} else if (numberDealerPlusCard > 21) {
alert("You win!");
chipCounter = chipCounter + (bet * 2);
console.log(chipCounter);
var $gameDialogue = $('#game-dialogue');
$gameDialogue.empty();
$gameDialogue.html("You have " + chipCounter + " chips left.");
}
})
$("#stayCard").click(function() {
var dealerRandom = (Math.floor(Math.random() * deckOfCards.length));
// console.log(dealerRandom);
this.innerHTML = (deckOfCards[dealerRandom].nameOfCard);
var newDealerNumber = (deckOfCards[dealerRandom].cardValue);
// console.log(newDealerNumber)
var numberDealerPlusCard = (dealerNumberCounter += newDealerNumber);
console.log(numberDealerPlusCard);
$("#dealerCounter").empty().append(numberDealerPlusCard);
if (numberDealerPlusCard < 17) {
$("#dealerCounter").empty().append(numberDealerPlusCard);
} else if (numberDealerPlusCard > 21) {
alert("You win!");
chipCounter = chipCounter + (bet * 2);
console.log(chipCounter);
var $gameDialogue = $('#game-dialogue');
$gameDialogue.empty();
$gameDialogue.html("You have " + chipCounter + " chips left.");
}
// } else
// // if (numberDealerPlusCard >= 17 && numberDealerPlusCard <= 21) {
// if (numberDealerPlusCard === 17 || 18 || 19 || 20) {
// $("#dealerCounter").empty().append(17)
// }
// $("#chipCounterID").empty().append(betFunction);
// var removed = deckOfCards.splice(random, 1);
// };
// });
});
}
// });
// }
//=======================Get Winner Function==========================
// var getWinner = function {
// if (playerTotal === 21) {
// alert("You won this hand, partner.")
// } else if {
// (dealerTotal >= 17) {
// // disable their ability to play
// }
// } else if {
// (playerTotal < 21) {
// alert("You lose.")
// }
// }
// }
//===========================Code Graveyard=====================
//Create cards
//each array can get a class and have an image associated with it
// var cards = [[Ac, 2c, 3c, 4c, 5c, 6c, 7c, 8c, 9c, 10c, Jc, Qc, Kc]
// []
// ];
// function Card(suit, value) {
// this.suit = spades;}
// function getSuit() {
// return Math.floor((Math.random() * 4) + 1);
// }
// function getNumber() {
// return Math.floor((Math.random() * 13) + 1);
// }
// function getRandom(arr) {
// var random1 = Math.floor((Math.random() * (arr.length)));
// return arr[random1][Math.floor((Math.random() * (arr[random1].length)))];
// }
// function Card(number) {
// this.number = (number % 13)+1;
// this.realNumber = number;
// }
// var deck = [];
// for (i = 0; i < 52; i++) {
// deck[i] = new Card(i);
// }
// var newDeck = function() {
// for (i = 0; i < 52; i++) {
// deck[i] = new Card(i);
// }
// };
// var randomCard = function() {
// var card = Math.floor(Math.random()*deck.length);
// return deck.splice(card, 1)[0];
// };
// var testCards = (deckOfCards[3].nameOfCard);
// var play = function() {
// if (turn) {
// $(pCard1).innertext(testCards);
// }
// }
// document.getElementbyId("counter").textContent = (numberPlusCard);
// var usedCards = []; //stores cards after they've been dealt
// while (deckOfCards.length > 0);
// $("#dCard1").click(function() {
// if (numberPlusCard === numberDealerPlusCard) {
// alert("It's a push!")
// }
// if (numberPlusCard > 21) {
// alert("You lost this one.");
// var chipCounter = (chipCounter - bet);
// }
// if (numberPlusCard === 21) {
// alert("You win!");
// var chipCounter = (chipCounter + (bet * 2));
// }
// });
// }
// $("#pCard1").click(function() { //this works but also sucks
// $("#counter").innerHTML = (numberPlusCard);
// });
// $("#counter").innerHTML = ("numberPlusCard");
// var pointsCounter = function(){
// if ($("#pCard1") != null) {
// $("#counter").innertext = (numberPlusCard);
// }
// }
// var firstCard = function() {
// pCard2.innerHTML = (deckOfCards[random].nameOfCard);
// deckOfCards.splice(firstCard);
// }
// console.log(pCard2.innerHTML);
// hitMe.onclick = function (event) { //then send card to player,
// while pCard1.innerHTML = (deckOfCards[random].nameOfCard);
// }
// // then one to dealer, then repeat