-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
252 lines (171 loc) · 6.27 KB
/
script.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
/*************************
Blackjack
Github: therochaexperience
*************************/
/*************************
Variable Declarations
Data structure for a generic card?
Create an object that transforms value name to value number?
How to update score on page? Convert string to num?
How to set value in HTML tag to a number type instead of a string?
Place into separate file
To do:
- add database to store all wins and losses (keeps records of all games played, keeps track of scores)
- create local web server that allows playing on browser
- add stay button
- let player choose number of decks used
- add a "purse", how much to bet on each game
- add soft/hard hand feature, allows changing value of ace from 11 to 1
- dealer's hidden card (?)
- dealer must hit until the cards total 17 or more points, the dealer also hits on a "soft" 17
- what happens when dealer and player both hit blackjack?
- what happens when player hits 21? can player still hit? probably not, goes to dealer to start hitting
PUSH WHEN TIED
CHECK FOR BLACKJACK WHEN CARDS FIRST DEALT
DEALER STAYS IF TIED WITH PLAYER AND SCORE GREATER THAN 17
(dealer hit when tied w/ player at 20, busted)
*************************/
// Card Variables
let suits = ["Clubs", "Diamonds", "Hearts", "Spades"];
let values = [{ name: "Ace", points: 1 },
{ name: "King", points: 10 },
{ name: "Queen", points: 10 },
{ name: "Jack", points: 10 },
{ name: "Ten", points: 10 },
{ name: "Nine", points: 9 },
{ name: "Eight", points: 8 },
{ name: "Seven", points: 7 },
{ name: "Six", points: 6 },
{ name: "Five", points: 5 },
{ name: "Four", points: 4 },
{ name: "Three", points: 3 },
{ name: "Two", points: 2 } ];
// DOM Variables
let gameText = document.getElementById('gameText');
let newGameBtn = document.getElementById('newGameBtn');
let hitBtn = document.getElementById('hitBtn');
let stayBtn = document.getElementById('stayBtn');
// Game Variables
let deck = [],
numCardsDealt = 0;
let dealerHand = { cards: [], score: 0,
displayCards: document.getElementById('dealerHand'),
displayScore: document.getElementById('dealerScore')};
let playerHand = { cards: [], score: 0,
displayCards: document.getElementById('playerHand'),
displayScore: document.getElementById('playerScore')};
newGameBtn.addEventListener('click', function(){
newGame();
});
hitBtn.addEventListener('click', function(){
dealCard(playerHand, 1);
});
stayBtn.addEventListener('click', function(){
while (dealerHand.score < 17){
dealCard(dealerHand, 1);
};
if(dealerHand.score <= playerHand.score) {
dealCard(dealerHand, 1);
};
gameOver();
});
/*************************
Function Declarations
Place into separate file
*************************/
function createDeck(){
// Test: confirm that all 52 cards are created every time
for (let suitIdx = 0; suitIdx < suits.length; suitIdx++){
for (let valueIdx = 0; valueIdx < values.length; valueIdx++){
let newCard = {
suit: suits[suitIdx],
valueStr: values[valueIdx].name,
points: values[valueIdx].points
};
newCard.name = `${newCard.valueStr} of ${newCard.suit}`;
deck.push(newCard);
//console.log(newCard);
}
}
}
function getRandomCard(){
// Test: check every card with an existing deck and confirm it's not duplicated
let i = Math.trunc(Math.random() * deck.length);
let card = deck.splice(i,1)[0];
console.log(card.name, " ", numCardsDealt);
return card;
}
function shuffleDeck(deck){
// To do - change system to shuffle deck instead of pulling random card, clearer intent
}
function calcScore(hand, cardIndex){
// When is a hand soft? When does the Ace change values from 11 to 1?
let hasAce = false;
hand.score = 0;
for(let i = 0; i < hand.cards.length; i++){
if(hand.cards[i].valueStr === 'Ace'){
hasAce = true;
}
hand.score += hand.cards[i].points;
}
if(hasAce && hand.score + 10 <= 21){
hand.score += 10;
}
hand.displayScore.innerText = `Score: ${hand.score}`;
}
function dealCard(hand, numCards){
// Deal two cards when game started, one card at a time afterwards
for(let i = 0; i < numCards; i++){
numCardsDealt++;
hand.cards.push(getRandomCard());
let cardIndex = hand.cards.length - 1;
hand.displayCards.innerText += `${hand.cards[cardIndex].name} \n`;
calcScore(hand, cardIndex);
}
console.log(hand.score);
if(hand.score > 21 || numCardsDealt === 52) { gameOver(); }
}
function newGame(){
console.log("Welcome to Blackjack!");
console.log("Cards dealt: ");
// Rebuild deck
deck = [];
numCardsDealt = 0;
createDeck();
// Set buttons and game text
gameText.innerText = "Game Started:"
newGameBtn.style.display = "none";
hitBtn.style.display = "inline";
stayBtn.style.display = "inline";
// Reset dealer hand
clearHand(dealerHand);
dealCard(dealerHand, 2);
// Reset player hand
clearHand(playerHand);
dealCard(playerHand, 2);
}
function gameOver(){
// Set buttons and game text
newGameBtn.style.display = "inline";
hitBtn.style.display = "none";
stayBtn.style.display = "none";
if(playerHand.score <= 21){
if(playerHand.score > dealerHand.score || dealerHand.score > 21){
gameText.innerText = "PLAYER WINS!"
}
else{
gameText.innerText = "DEALER WINS"
}
} else{
gameText.innerText = "DEALER WINS"
}
}
function clearHand(hand){
hand.cards = [];
hand.score = 0;
hand.displayCards.innerText = "";
}
/*************************
Start Program Logic
Separate file
*************************/