forked from rocketacademy/high-card-dom-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
334 lines (271 loc) · 9.51 KB
/
script.js
File metadata and controls
334 lines (271 loc) · 9.51 KB
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
// Please implement exercise logic here
let playersTurn = 1; // matches with starting instructions
let player1Cards = [];
let player2Cards = [];
// let player1Card;
let maxNumberOfCards = 3;
// let player1Container;
// let player2Container;
// set timeout for dealing cards
let canClick = true;
let endGame = false;
const player1Button = document.createElement("button");
const player2Button = document.createElement("button");
const label = document.createElement("label");
const inputField = document.createElement("input");
const submitButton = document.createElement("button");
const gameInfo = document.createElement("div");
const linebreak = document.createElement("br");
// Get a random index ranging from 0 (inclusive) to max (exclusive).
const getRandomIndex = (max) => Math.floor(Math.random() * max);
// Shuffle an array of cards
const shuffleCards = (cards) => {
// Loop over the card deck array once
for (let currentIndex = 0; currentIndex < cards.length; currentIndex += 1) {
// Select a random index in the deck
const randomIndex = getRandomIndex(cards.length);
// Select the card that corresponds to randomIndex
const randomCard = cards[randomIndex];
// Select the card that corresponds to currentIndex
const currentCard = cards[currentIndex];
// Swap positions of randomCard and currentCard in the deck
cards[currentIndex] = randomCard;
cards[randomIndex] = currentCard;
}
// Return the shuffled deck
return cards;
};
const makeDeck = () => {
// Initialise an empty deck array
const newDeck = [];
// Initialise an array of the 4 suits in our deck. We will loop over this array.
const suits = ["hearts", "diamonds", "clubs", "spades"];
const suitsSymbol = ["♥", "♦", "♣", "♠"];
// Loop over the suits array
for (let suitIndex = 0; suitIndex < suits.length; suitIndex += 1) {
// Store the current suit in a variable
const currentSuit = suits[suitIndex];
const currentSuitSymbol = suitsSymbol[suitIndex];
let currentColour;
if (currentSuit == "hearts" || currentSuit == "diamonds") {
currentColour = "red";
} else {
currentColour = "black";
}
// Loop from 1 to 13 to create all cards for a given suit
// Notice rankCounter starts at 1 and not 0, and ends at 13 and not 12.
// This is an example of a loop without an array.
for (let rankCounter = 1; rankCounter <= 13; rankCounter += 1) {
// By default, the card name is the same as rankCounter
let cardName = `${rankCounter}`;
let shortName = `${rankCounter}`;
// If rank is 1, 11, 12, or 13, set cardName to the ace or face card's name
if (cardName === "1") {
cardName = "ace";
shortName = "A";
} else if (cardName === "11") {
cardName = "jack";
shortName = "J";
} else if (cardName === "12") {
cardName = "queen";
shortName = "Q";
} else if (cardName === "13") {
cardName = "king";
shortName = "K";
}
// Create a new card with the current name, suit, and rank
const card = {
name: cardName,
suit: currentSuit,
suitSymbol: currentSuitSymbol,
rank: rankCounter,
displayName: shortName,
colour: currentColour,
};
// Add the new card to the deck
newDeck.push(card);
}
}
// Return the completed card deck
return newDeck;
};
const deck = shuffleCards(makeDeck());
// Create a helper function for output to abstract complexity
// of DOM manipulation away from game logic
const output = (message) => {
gameInfo.innerText = message;
};
// const cardInfo = {
// suitSymbol: "♦️",
// suit: "diamond",
// name: "queen",
// displayName: "Q",
// colour: "red",
// rank: 12,
// };
const createCard = (cardInfo) => {
const suit = document.createElement("div");
suit.classList.add("suit");
suit.innerText = cardInfo.suitSymbol;
const name = document.createElement("div");
name.classList.add(cardInfo.displayName, cardInfo.colour);
name.innerText = cardInfo.displayName;
const card = document.createElement("div");
card.classList.add("card");
card.appendChild(name);
card.appendChild(suit);
return card;
};
// const createCard = (card) => {
// const cardEl = document.createElement("p");
// cardEl.innerText = card.name; // also output the other attributes
// return cardEl;
// };
// click functions w/o timeout
// const player1Click = () => {
// if (playersTurn === 1) {
// // Pop player 1's card metadata from the deck
// player1Card = deck.pop();
// // Create card element from card metadata
// const cardElement = createCard(player1Card);
// // Empty cardContainer in case this is not the 1st round of gameplay
// cardContainer.innerHTML = "";
// // Append the card element to the card container
// cardContainer.appendChild(cardElement);
// // Switch to player 2's turn
// playersTurn = 2;
// }
// };
// const player2Click = () => {
// if (playersTurn === 2) {
// // Pop player 2's card metadata from the deck
// const player2Card = deck.pop();
// // Create card element from card metadata
// const cardElement = createCard(player2Card);
// // Append card element to card container
// cardContainer.appendChild(cardElement);
// // Switch to player 1's turn
// playersTurn = 1;
// // Determine and output winner
// if (player1Card.rank > player2Card.rank) {
// output("player 1 wins");
// } else if (player1Card.rank < player2Card.rank) {
// output("player 2 wins");
// } else {
// output("tie");
// }
// }
// };
const checkReachMaxCards = (cardsArray) => {
return cardsArray.length == maxNumberOfCards;
};
const evaluateScore = () => {
// compare the ranks of each hand
const player1rankArray = [];
const player2rankArray = [];
for (let i = 0; i < maxNumberOfCards; i++) {
player1rankArray.push(player1Cards[i].rank);
player2rankArray.push(player2Cards[i].rank);
}
const player1score =
Math.max(...player1rankArray) - Math.min(...player1rankArray); //int
const player2score =
Math.max(...player2rankArray) - Math.min(...player2rankArray);
if (player1score > player2score) {
output("player 1 wins");
} else if (player1score < player2score) {
output("player 2 wins");
} else {
output("tie");
}
};
const player1Click = () => {
if (playersTurn === 1 && canClick === true) {
canClick = false;
const player1Container = document.getElementById("player-1-cards");
const player2Container = document.getElementById("player-2-cards");
// for clearing the playing field
if (endGame) {
player1Container.innerHTML = "";
player2Container.innerHTML = "";
player1Cards = [];
player2Cards = [];
endGame = false;
}
setTimeout(() => {
const cardToPush = deck.pop();
player1Cards.push(cardToPush); // store to player1cards
const cardElement = createCard(cardToPush); // display
player1Container.appendChild(cardElement);
playersTurn = 2;
canClick = true;
}, 1000);
}
};
const player2Click = () => {
if (playersTurn === 2 && canClick === true) {
canClick = false;
setTimeout(() => {
const cardToPush = deck.pop();
player2Cards.push(cardToPush); // store to player2cards
const cardElement = createCard(cardToPush); // display
const player2Container = document.getElementById("player-2-cards");
const player1Container = document.getElementById("player-1-cards");
player2Container.appendChild(cardElement);
if (checkReachMaxCards(player2Cards)) {
// end game, calculate score
evaluateScore();
endGame = true;
}
//continue game
playersTurn = 1;
canClick = true;
}, 1000);
}
};
const submitNumberOfCards = (input) => {
maxNumberOfCards = input;
};
const initGame = () => {
// initialize button functionality
player1Button.innerText = "Player 1 Draw";
document.body.appendChild(player1Button);
player2Button.innerText = "Player 2 Draw";
document.body.appendChild(player2Button);
player1Button.addEventListener("click", player1Click);
player2Button.addEventListener("click", player2Click);
// fill game info div with starting instructions
gameInfo.innerText = "Its player 1 turn. Click to draw a card!";
document.body.appendChild(gameInfo);
player1Container = document.createElement("div");
player1Container.id = "player-1-cards";
document.body.appendChild(player1Container);
player2Container = document.createElement("div");
player2Container.id = "player-2-cards";
document.body.appendChild(player2Container);
};
// const startPage = () => {
// if (maxNumberOfCards === null || maxNumberOfCards === undefined) {
// const newForm = document.createElement("form");
// label.innerText = "Input max no. of cards allowed per player:";
// label.appendChild(linebreak);
// inputField.id = "maxCards";
// inputField.type = "number";
// // inputField.placeholder = "Input max no. of cards";
// inputField.required = true;
// // document.body.appendChild(inputField);
// submitButton.type = "submit";
// submitButton.innerText = "Submit";
// // document.body.appendChild(submitButton);
// newForm.appendChild(label);
// newForm.appendChild(inputField);
// newForm.appendChild(submitButton);
// document.body.appendChild(newForm);
// // submitButton.addEventListener("click", submitNumberOfCards);
// } else {
// initGame();
// }
// };
// console.log(maxNumberOfCards);
// startPage();
initGame();