-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
308 lines (249 loc) · 10.5 KB
/
index.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
let qntNumber = [];//What's the index from arr pokemon put in the span's array
let arrStoreCardValue= []; //Used to store the values of the titile spans clickd
let arrStoreCardIndex = []; //Used to store the value of the index of each card clicked
let arrayPokes = []; //To Store the image URL of each pokemon
let arrayPokeNames = []; //Store the name of each pokemon
//To player
let player = 0; //Flag the players is playing now
let playerObj = {
player0: { id: 'Player 1', score: 0, scoreBoard: 0},
player1: {id: 'Player 2', score: 0, scoreBoard: 0},
}
let isReset = true; //Flag to use the buttons New Game and Reset
/*------------------GET VALUE FROM SESSION STORAGE and CHOOSE THE POKEMONS TO APPEAR---------------------------- */
function storePokeArr(){
let pokeLength = Object.keys(sessionStorage).length;
for ( let i = 0; i < pokeLength; i++){
if((Object.keys(sessionStorage)[i]) !== 'IsThisFirstTime_Log_From_LiveServer'){
arrayPokes.push(sessionStorage.getItem(Object.keys(sessionStorage)[i]));
arrayPokeNames.push(Object.keys(sessionStorage)[i]);
}
}
}
setTimeout(() => {storePokeArr()}, 2000);
function storeSelectedPokemons() {
let arrSelectedPokemons = [];
let arrAux = [];
for(let i = 0; i < 28; i++){
arrAux[i] = arrayPokes[i];
}
for(let i = 0; i < 14 ; i++){
let randNumb = Math.floor(Math.random() * arrAux.length);
arrSelectedPokemons.push(arrAux[randNumb]);
arrAux.splice(randNumb, 1);
}
return arrSelectedPokemons;
}
//------------------------------------------START------------------------------------------------------------------
window.start = function(){
if(isReset === true){
let cards = document.getElementsByClassName('card');
for(let i = 0; i < cards.length; i++){
cards[i].classList.add('clicked');
}
putPokemonInCard();
startClickEvt();
player = Math.floor(Math.random() * 2); //Choose begining player
document.getElementById('nPlayer0').textContent = playerObj['player0']['id'];
document.getElementById('nPlayer1').textContent = playerObj['player1']['id'];
document.getElementById('scoreP0').textContent = playerObj['player0']['score'] = 0;
document.getElementById('scoreP1').textContent = playerObj['player1']['score'] = 0;
document.getElementById(`nPlayer${player}`).style.color = 'red';
document.getElementById('newGame').style.backgroundColor = 'gray';
document.getElementById('reset').style.backgroundColor = 'red';
isReset = false;
}
}
// ------------------------------------Put the pokemon in the right card-------------------------------
function isPokemonAppeared(qntNumber, arrIndexPokemon){
qntNumber.push(arrIndexPokemon); //put the value arrIndexPokemon in a array
//How many times the number shows in array qntNumber
let qnt = qntNumber.filter(item => item == arrIndexPokemon).length;
if(qnt === 2){
let val = 2;
let numb = arrIndexPokemon;
//Delete the value from array
while(val > 0){
let index = qntNumber.indexOf(arrIndexPokemon);
qntNumber.splice(index, 1);
val--;
}
//if number greater than the value, update the value subtracting 1 of the values
let arrUpdated = qntNumber.map((item) => {
if(item > numb){
return (item - 1);
}else {
return item
}
});
// arr qntNumber updated
for(let i=0; i < arrUpdated.length ; i++){
qntNumber[i] = arrUpdated[i];
}
return true;
}else{
return false;
}
};
function putPokemonInCard(){
let arrSelecetedPokemons = storeSelectedPokemons();
let arrCards = document.getElementsByClassName('card');
for(let i = 0; i < arrCards.length; i++){
let randNumber = Math.floor(Math.random() * (arrSelecetedPokemons.length));
let rtnNumber = isPokemonAppeared(qntNumber, randNumber);
arrCards[i].title = arrSelecetedPokemons[randNumber];
arrCards[i].style.backgroundColor = '#a6ce39';
if(rtnNumber === true){
arrSelecetedPokemons.splice(randNumber, 1);
}
}
}
// ---------------------------------------------------VERIFY THE CARDS CLICKED ------------------------------------------------------
function isCardEqual(value, indexCard){
let cards = document.getElementsByClassName('card');
arrStoreCardValue.push(value);
arrStoreCardIndex.push(indexCard);
if(value !== "" || value !== " " ){
if(arrStoreCardIndex.length === 2 ){
let hasClassClicked = cards[indexCard - 1].classList.contains('clicked');
if((arrStoreCardValue[0] === arrStoreCardValue[1]) && (arrStoreCardIndex[0] !== arrStoreCardIndex[1]) && hasClassClicked){
//To get the score for any player
playerObj[`player${player}`]['score'] = playerObj[`player${player}`]['score'] + 1;
document.getElementById(`scoreP${player}`).textContent = playerObj[`player${player}`]['score'];
let scoreP1 = playerObj['player0']['score'];
let scoreP2 = playerObj['player1']['score'];
for(let i = 0; i < arrStoreCardIndex.length; i++){
cards[arrStoreCardIndex[i] - 1].classList.remove('clicked');
cards[arrStoreCardIndex[i] - 1].removeEventListener('click', eventClick, false);
}
showModal(3);
if((scoreP1 + scoreP2) === 14){
updateScoreBoard(scoreP1, scoreP2);
}
arrStoreCardIndex = [];
arrStoreCardValue= [];
}else{
//Change the player playing
if(player === 1){
player = 0;
document.getElementById(`nPlayer0`).style.color = "red";
document.getElementById(`nPlayer1`).style.color = "black";
}else{
player = 1;
document.getElementById(`nPlayer1`).style.color = "red";
document.getElementById(`nPlayer0`).style.color = "black";
}
showModal(0);
}
}
}
}
// Return the cards to pokebola img if they are not equals
function returnToBackCards(){
let cards = document.getElementsByClassName('card');
for(let i = 0; i < arrStoreCardIndex.length; i++){
cards[arrStoreCardIndex[i] - 1].style.backgroundImage = `url(./assets/pokebola.jpg)`;
}
arrStoreCardValue= [];
arrStoreCardIndex = [];
}
//To all cards allows the event click
function startClickEvt(){
let card = document.querySelectorAll('.clicked');
card.forEach((item) => {
item.addEventListener('click', eventClick, false);
});
}
//Action of the event of card
var eventClick = (evt) => {
let url = evt.target.title;
let indexCard = evt.target.textContent;
evt.target.style.backgroundImage = `url(${url})`;
isCardEqual(url, indexCard);
};
// --------------------------------------------------SHOW AND HIDE MODAL-------------------------------------------
let modal = document.getElementsByClassName('modal');
function showModal(modalNumber){
modal[`${modalNumber}`].style.display = 'block';
}
window.hideModal = function(){
modal[3].style.display = 'none';
}
window.hideModalErr = function(){
modal[0].style.display = 'none';
returnToBackCards();
}
window.hideModalEnd = function(){
modal[1].style.display = 'none';
}
window.hideModalDraw = function(){
modal[2].style.display = 'none';
}
window.onclick = function(evt){
if(evt.target.classList[0] === 'modal'){
switch(evt.target.classList[1]){
case 'modal0':
hideModalErr();
break;
case 'modal1':
hideModalEnd();
break;
case 'modal2':
hideModalDraw();
break;
case 'modal3':
hideModal();
break;
}
}
}
// -------------------------------------------------- Scoreboard --------------------------------------------------
window.onload = () => {
storeScoreLocalStorage();
}
function storeScoreLocalStorage(){
localStorage.setItem('player1', playerObj.player0.id);
localStorage.setItem('player2', playerObj.player1.id);
localStorage.setItem('scoreBP1', playerObj.player0.scoreBoard);
localStorage.setItem('scoreBP2', playerObj.player1.scoreBoard);
}
function updateScoreBoard(scoreP1, scoreP2){
let nameP1 = playerObj['player0']['id'];
let nameP2 = playerObj['player1']['id'];
if(scoreP1 === scoreP2){
modal[2].style.display = 'block';
}else if(scoreP1 > scoreP2){
modal[1].style.display = 'block';
document.getElementById('name').textContent = nameP1;
playerObj.player0.scoreBoard += 1;
}else{
modal[1].style.display = 'block';
document.getElementById('name').textContent = nameP2;
playerObj.player1.scoreBoard += 1;
}
storeScoreLocalStorage();
}
// -------------------------------------------------- RESET THE VALUES --------------------------------------------
window.reset = function(){
let arrCards = document.getElementsByClassName('card');
let cardClicked = document.querySelectorAll('.clicked');
cardClicked.forEach((item) => {
item.removeEventListener('click', eventClick, false);
});
for(let i = 0; i < arrCards.length; i++){
arrCards[i].style.backgroundImage = "url(./assets/pokebola.jpg)";
arrCards[i].classList.remove('clicked');
}
document.getElementById('scoreP0').textContent = playerObj['player0']['score'] = 0;
document.getElementById('scoreP1').textContent = playerObj['player1']['score'] = 0;
document.getElementById('nPlayer0').style.color = 'black';
document.getElementById('nPlayer1').style.color = 'black';
document.getElementById('newGame').style.backgroundColor = 'red';
document.getElementById('reset').style.backgroundColor = 'gray';
arrStoreCardValue= [];
arrStoreCardIndex = [];
qntNumber = [];
isReset = true;
}
window.onunload = () => { localStorage.clear() }
// export {playerObj};