-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (125 loc) · 3.54 KB
/
app.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
var memoArr = [
"Gryffondor",
"Gryffondor",
"Slytherin",
"Slytherin",
"Hufflepuff",
"Hufflepuff",
"Ravenclaw",
"Ravenclaw",
"Hagrid",
"Hagrid",
"Hermione",
"Hermione",
"Horcruxe",
"Horcruxe",
"Harry",
"Harry",
"Malfoy",
"Malfoy",
"Voldemort",
"Voldemort",
"Sirius Black",
"Sirius Black",
"RON",
"RON",
"Neville",
"Neville",
"Pumpkin juice",
"Pumpkin juice",
"Nagini",
"Nagini"
];
var memoVallues = [];
var memoTileId = [];
var tilesFlipped = 0;
// score :
var gameScore = 0;
// shuffling the array
Array.prototype.memoTile_shuffle = function () {
var i = this.length,
j,
temp;
while (--i > 0) {
j = Math.floor(Math.random() * (i + 1));
temp = this[j];
this[j] = this[i];
this[i] = temp;
}
};
function newBoard() {
tilesFlipped = 0;
var output = "";
memoArr.memoTile_shuffle();
for (var i = 0; i < memoArr.length; i++) {
output +=
'<div id="tile_' +
i +
'" onClick="memoryFlipTiles(this,\'' +
memoArr[i] +
"')\"></div>";
}
document.getElementById("memory_board").innerHTML = output;
}
/* posibilities when clicking two cards:
1- no match ==> return them to the init state
2- a match ==> keep them in front.
+ when fliping cards the background color of the card = white (or any color :) )
*/
function memoryFlipTiles(tile, val) {
if (tile.innerHTML == "" && memoVallues.length < 2) {
tile.style.background = "#FFF";
tile.innerHTML = val;
if (memoVallues.length == 0) {
memoVallues.push(val);
memoTileId.push(tile.id);
} else if (memoVallues.length == 1) {
memoVallues.push(val);
memoTileId.push(tile.id);
if (memoVallues[0] == memoVallues[1]) {
tilesFlipped += 2;
gameScore+=4;
document.querySelector(".gameScore").innerHTML = gameScore;
if(gameScore <0){
document.querySelector(".gameScore").style.color = "red";
}else if(gameScore >=0){
document.querySelector(".gameScore").style.color = "green";
}
//clearing arrays
memoVallues = [];
memoTileId = [];
if (tilesFlipped == memoArr.length) {
alert("WELL DONE HARRY, WELL DONE: NOW TRY AGAIN and BEAT YOUR SCORE: "+gameScore);
document.getElementById("memory_board").innerHTML = "";
gameScore = 0;
document.querySelector(".gameScore").innerHTML = gameScore;
newBoard();
}
} else {
function flipToBack() {
// Flip the 2 tiles back
var tile_1 = document.getElementById(memoTileId[0]);
var tile_2 = document.getElementById(memoTileId[1]);
tile_1.style.background = "rgb(47, 66, 90)";
tile_1.innerHTML = "";
// to return the card at the initial state
tile_2.style.background = "rgb(47, 66, 90)";
tile_2.innerHTML = "";
// to return the card at the initial state
gameScore-=1;
document.querySelector(".gameScore").innerHTML = gameScore;
if(gameScore <0){
document.querySelector(".gameScore").style.color = "red";
}else if(gameScore >=0){
document.querySelector(".gameScore").style.color = "green";
}
//clearing arrays again
memoVallues = [];
memoTileId = [];
}
setTimeout(flipToBack, 400);
// time of flipp
}
}
}
}