-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
338 lines (321 loc) · 10.5 KB
/
main.js
File metadata and controls
338 lines (321 loc) · 10.5 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
335
336
337
338
// This creates a standard array to build other arrays from.
let memory_Options = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h', 'i', 'i'];
// This indicates how many previous games have been played.
let game_Next = 0;
// Keeps track of the number of tiles flipped and card letters.
let tiles_Flipped_Even = 0;
let card_One;
let card1number;
let card_Two;
let card2number;
// This generates a random order of memory_Options Array.
let hold_Array = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h', 'i', 'i'];
let random_Set = [];
function shuffle(){
for( let i = 0; i < 18; i++ ){
// This gives a random integer within the memory_Options.length values.
let r = Math.floor((Math.random() * hold_Array.length));
random_Set[i] = hold_Array[r];
hold_Array.splice(r,1);
}
console.log(random_Set);
return random_Set;
}
// This checks to see if the game was won.
// This checks to see if the game was won.
// This checks to see if the game was won.
let game_Is_Over = false;
function game_Over( winLose ){
// This variable should shut down the clock.
game_Is_Over = true;
// This removes the header.
let h2_Elements = document.getElementsByTagName("h2");
while ( 0 < h2_Elements.length ){
h2_Elements[0].remove();
}
// This removes all of the tiles.
let tile_Elements = document.getElementsByTagName("li");
while ( 0 < tile_Elements.length ){
tile_Elements[0].remove();
}
// This removes the life_container.
let z = document.getElementsByClassName("life_Container");
while ( 0 < z.length ){
z[0].remove();
}
// This places a you won message with the time you took to win where the timer was located.
let a = document.querySelector( "#total_board" );
let c = document.createElement( "h1" );
c.setAttribute("class", "end_Message");
// This converts the counter value into min and sec.
let min = Math.floor(counter/60);
let sec = counter%60;
let time_Took;
if( min === 0 ){
time_Took = sec + " sec";
}
else{
time_Took = min + " min " + sec + " sec";
}
let d = '';
if(winLose){
d = "You Won! in " + time_Took;
}
else{
d = "GAME OVER";
c.setAttribute("class", "end_Message lost");
}
c.innerHTML = d ;
a.appendChild( c );
// This lets the game know another game has been played.
game_Next++;
// This makes a new start button appear.
start_Game_Button();
}
// This makes the life counters.
function make_Life_Counters(){
let a = document.querySelector( "#lifeCounterGoesHere" );
// This makes the header.
let e = document.createElement( "div" );
e.setAttribute("class", "life_Container");
a.appendChild( e );
for( let i = 0; i < 18; i++ ){
let b = document.createElement( "div" );
b.setAttribute("class", "heart");
e.appendChild(b);
}
}
let lifeLost = 0;
// This removes the life counters.
function lose_Life(){
lifeLost = lifeLost + 1;
if(lifeLost > 18){
game_Over( false );
console.log("Game Over");
}
else{
let x = document.getElementsByClassName("heart");
x[0].remove();
}
}
// Keeps count of tile sets that are matched.
let tile_Sets_Matched = 0;
// This checks to see if the last 2 cards clicked match.
// This also checks to see if all the cards have been matched.
// If a match then the class for the cards changes to show_Forever.
function checkForMatch(){
console.log("check for match.");
let letter1 = card_One.slice(-1);
let letter2 = card_Two.slice(-1);
if ( letter1 === letter2 && card_One != card_Two){
console.log("Match Made!");
let x = document.getElementsByClassName("show");
while (x.length > 0){
// Removes eventListener.
x[0].removeEventListener("click", clickToFlip);
// Changes match to show forever.
x[0].setAttribute("class", "show_Forever " + letter1);
}
// This checks to see if you won the game.
// This checks to see if you won the game.
// This checks to see if you won the game.
// This checks to see if you won the game.
// This checks to see if you won the game.
tile_Sets_Matched = tile_Sets_Matched + 1;
if (tile_Sets_Matched === 18){
game_Over( true );
console.log("Game Won!");
}
return true;
}
}
function returnFaceDown(){
// Creates an array of all the class of all the show tiles.
let x = document.getElementsByClassName("show");
// Hides all the show tiles.
while (x.length > 0){
x[0].addEventListener('click', clickToFlip);
x[0].setAttribute("class", "hide");
}
}
// This controls the actions of the cards when flipped.
function clickToFlip() {
// This gives the tile number as a string
let num1 = this.id.charAt(4);
let numBoth = this.id.charAt(4) + this.id.charAt(5);
let unknown = this.id.charAt(5);
let trueNum = 0;
if (num1 === '1' && unknown >= 0){
trueNum = numBoth;
}
else{
trueNum = num1;
}
// This makes certain that the tile isn't a show_Forever tile.
if(this.className != "show_Forever" && this.className != "show"){
// This checks to see if 2 cards are shown.
tiles_Flipped_Even += 1;
// console.log("Clicked a tile " + tiles_Flipped + " times.");
switch(tiles_Flipped_Even){
case 1: card_One = this.id;
card1number = trueNum;
break;
case 2: card_Two = this.id;
card2number = trueNum;
break;
case 3:
returnFaceDown();
tiles_Flipped_Even = 1;
card_One = this.id;
default:
}
// This makes the card change class and flip.
this.setAttribute("class", "show " + this.id.slice(-1));
}
// This calls the checkForMatch function. If matched this card becomes show_Forever.
switch(tiles_Flipped_Even%2){
case 0: checkForMatch();
if (checkForMatch()){
this.setAttribute("class", "show_Forever " + this.id.slice(-1));
this.removeEventListener("click", clickToFlip);
}
else{
// This removes a life counter.
lose_Life();
}
}
console.log("This class is " + this.className);
console.log("Tiles e/o: " + tiles_Flipped_Even);
console.log("card_One is " + card_One);
console.log("card_Two is " + card_Two);
// This gives the letter on the tile.
let letterOnCard = this.id.slice(-1);
}
// Code to make tiles appear.
function placeTiles(){
// runs the shuffle function.
shuffle();
// This makes the itemList node attach to the total_board element on the HTML page.
let itemList = document.querySelector( "#total_board" );
// This makes a loop to go through the memor_Options array.
for( let i = 0; i < memory_Options.length; i++ ){
// This creates an li value to act as a tile.
let li = document.createElement( "li" );
// This creates an H2 element.
let liH2 = document.createElement( "h2" );
// This places the random_Set text into the liText.
let liText = document.createTextNode( random_Set[ i ] );
// This makes the tile respond to being clicked.
li.addEventListener('click', clickToFlip);
// This creates a class for the tile and set it to hide.
li.setAttribute("class", "hide");
// This sets each tile with an id based on position on the game board AND the card value.
li.setAttribute("id", "card" + i + random_Set[ i ]);
// This moves the text to the H2.
liH2.appendChild( liText );
// This adds the H2 to the li tile.
li.appendChild( liH2 );
// This adds the li to the itemList.
itemList.appendChild( li );
}
}
// This places the header and the clock.
function placeHeader(){
let itemList = document.querySelector( "#header" );
// This creates a div to hold the header.
let li = document.createElement( "div" );
// This creates an H2 element.
let liH2 = document.createElement( "h2" );
// This places the memor_Options text into the liText.
let liText = document.createTextNode( "Memory Matching" );
// This moves the text to the H2.
liH2.appendChild( liText );
liH2.setAttribute("class", "LiH2");
// This adds the H2 to the li tile.
li.appendChild( liH2 );
// This adds the li to the itemList.
itemList.appendChild( li );
}
// These make the clock appear.
let clockSpot = document.querySelector( "#clockGoesHere" );
let clock = document.createElement( "div" );
let liH2 = document.createElement( "h2" );
// This makes the clock tick.
let counter = 0;//set this to what ever you want the start # to be
function tickTock(){
// This turns the clock off if the game is won.
if (game_Is_Over === true){
console.log("Clock turns off");
return counter;
}
// This actually makes the clock "tick".
counter++;//increment the counter by 1
setTimeout ( "tickTock()", 1000 );//runs itsself after 1000 miliseconds
let min = Math.floor(counter/60);
let sec = counter%60;
let liText;
if( min === 0 ){
liText = sec + " sec";
}
// This sets a time limit of 5 minutes.
else if( min >= 5 ){
liText = "";
game_Over( false );
}
else{
liText = min + " min " + sec + " sec";
}
// This moves the text to the H2.
liH2.innerHTML = liText ;
// This adds the H2 to the li tile.
clock.appendChild( liH2 );
// This adds the li to the itemList.
clockSpot.appendChild( clock );
}
// This code runs when the one opens the page.
function start_Game_Button(){
let a = document.querySelector( "#total_board" );
// This makes the header.
if(game_Next === 0 ){
let e = document.createElement( "h1" );
e.setAttribute("class", "end_Message");
let f = "Memory Matching";
e.innerHTML = f ;
a.appendChild( e );
}
// This makes the start button.
let c = document.createElement( "h1" );
c.setAttribute("class", "start_Message");
let d = "start game";
c.addEventListener('click', game_On);
c.innerHTML = d ;
a.appendChild( c );
}
// This runs when start is clicked.
function game_On(){
// This removes the start button.
let x = document.getElementsByClassName("start_Message");
while ( 0 < x.length ){
x[0].remove();
}
// This removes the header.
let y = document.getElementsByClassName("end_Message");
while ( 0 < y.length ){
y[0].remove();
}
// This resets some values if begin another game.
game_Is_Over = false;
hold_Array = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f', 'g', 'g', 'h', 'h', 'i', 'i'];
random_Set = [];
tiles_Flipped_Even = 0;
tile_Sets_Matched = 0;
counter = 0;
lifeLost = 0;
// This begins the game.
tickTock();
placeHeader();
make_Life_Counters();
placeTiles();
}
// This runs when the game is begun.
start_Game_Button();