forked from rocketacademy/high-card-dom-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
227 lines (194 loc) · 6.23 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
// Please implement exercise logic here
// get a random index from an array given it's size
const getRandomIndex = (size) => Math.floor(Math.random() * size);
// cards is an array of card objects
const shuffleCards = (cards) => {
// loop over the entire cards array
for (let currentIndex = 0; currentIndex < cards.length; currentIndex += 1) {
// select a random position from the deck
const randomIndex = getRandomIndex(cards.length);
// get the current card in the loop
const currentItem = cards[currentIndex];
// get the random card
const randomItem = cards[randomIndex];
// swap the current card and the random card
cards[currentIndex] = randomItem;
cards[randomIndex] = currentItem;
}
// give back the shuffled deck
return cards;
};
//makes a deck of cards
const makeDeck = () => {
// create the empty deck at the beginning
const newDeck = [];
const suits = ['hearts', 'diamonds', 'clubs', 'spades'];
for (let suitIndex = 0; suitIndex < suits.length; suitIndex += 1) {
// make a variable of the current suit
const currentSuit = suits[suitIndex];
console.log(`current suit: ${currentSuit}`);
// loop to create all cards in this suit
// rank 1-13
for (let rankCounter = 1; rankCounter <= 13; rankCounter += 1) {
// Convert rankCounter to string
let cardName = `${rankCounter}`;
let cardVal = `${rankCounter}`
// 1, 11, 12 ,13
if (cardName === '1') {
cardName = 'ace';
cardVal = 'A'
} else if (cardName === '11') {
cardName = 'jack';
cardVal = 'J'
} else if (cardName === '12') {
cardName = 'queen';
cardVal = 'Q'
} else if (cardName === '13') {
cardName = 'king';
cardVal = 'K'
}
//suit symbol
let color;
let suitSymbol;
if (suits[suitIndex] == 'hearts') {
color = 'red'
suitSymbol = '♥'
} else if (suits[suitIndex] == 'diamonds') {
suitSymbol = '♦'
color = 'red'
} else if (suits[suitIndex] == 'clubs') {
suitSymbol = '♣'
color = 'black'
} else if (suits[suitIndex] == 'spades') {
suitSymbol = '♠'
color = 'black'
}
// make a single card object variable
const card = {
name: cardName,
suit: currentSuit,
rank: rankCounter,
color: color,
suitSymbol: suitSymbol,
cardVal: cardVal
};
console.log(`rank: ${rankCounter} ${suitSymbol} ${color} ${cardName}`);
// add the card to the deck
newDeck.push(card);
}
}
return newDeck;
};
const output = (message) => {
gameInfo.innerText = message;
};
const createCard = (cardInfo) => {
const suit = document.createElement('div');
suit.classList.add('suit', cardInfo.color);
suit.innerText = cardInfo.suitSymbol;
const name = document.createElement('div');
name.classList.add('name');
name.innerText = cardInfo.cardVal;
const card = document.createElement('div');
card.classList.add('card');
card.appendChild(name);
card.appendChild(suit);
return card;
};
const deck = shuffleCards(makeDeck());
const player1Click = (num) => {
if (playersTurn === 1 && canClick === true) {
canClick = false;
cardContainer1.innerHTML = ''
cardContainer2.innerHTML = ''
for (let i = 0; i < num; i += 1){
player1Card = deck.pop();
player1CardElement = createCard(player1Card)
player1CardElement.classList.add('player1')
cardContainer1.appendChild(player1CardElement);
if (player1Card.rank > maxRank1) {
maxRank1 = player1Card.rank
}
if (player1Card.rank < minRank1) {
minRank1 = player1Card.rank
}
playersTurn = 2;
canClick = true
}
console.log(`minimum rank is ${minRank1}`)
console.log(`max rank is ${maxRank1}`)
setTimeout(() => {
output("It's player 2's turn. Click to draw a card")
}, 1000);
};
}
const player2Click = (num) => {
if (playersTurn === 2) {
let player2Card = null;
cardContainer2.innerHTML = ''
let maxRank2 = 0
let minRank2 = 15
for (let i = 0; i < num; i += 1){
player2Card = deck.pop();
cardContainer2.appendChild(createCard(player2Card));
if (player2Card.rank > maxRank2) {
maxRank2 = player2Card.rank
}
if (player2Card.rank < minRank2) {
minRank2 = player2Card.rank
}
}
playersTurn = 1;
console.log(`minimum rank2 is ${minRank2}`)
console.log(`max rank2 is ${maxRank2}`)
if ((maxRank1 - minRank1) > (maxRank2 - minRank2) ) {
output('player 1 wins');
} else if ((maxRank1 - minRank1) < (maxRank2 - minRank2) ) {
output('player 2 wins');
} else {
output('tie');
}
}
setTimeout(() => {
output("It's player 1's turn. Click to draw a card!")
}, 1500)
// reset max and min values
maxRank1 = 0;
minRank1 = 13;
};
let player1Card
let playersTurn = 1; // matches with starting instructions
let cardContainer1;
let cardContainer2;
let canClick = true
let numCards = 3
let maxRank1 = 0;
let minRank1 = 13;
//creating permanent elements
const mainDiv = document.createElement('div')
mainDiv.classList.add('mainDiv')
const player1Button = document.createElement('button');
const player2Button = document.createElement('button');
const gameInfo = document.createElement('div');
const initGame = () => {
document.body.appendChild(mainDiv)
// card container for first player
cardContainer1 = document.createElement('div');
mainDiv.appendChild(cardContainer1)
cardContainer1.classList.add('card-container1');
//card container for second player
cardContainer2 = document.createElement('div');
mainDiv.appendChild(cardContainer2)
cardContainer2.classList.add('card-container2');
// 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(numCards)});
player2Button.addEventListener('click', () => {player2Click(numCards)});
// fill game info div with starting instructions
gameInfo.innerText = "It's player 1's turn. Click to draw a card!";
document.body.appendChild(gameInfo);
}
initGame()