-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjs.js
186 lines (149 loc) · 5.16 KB
/
js.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
var tiles ; // the number of shown tiles
var tileId;
var falseCh; //the false choice counter
var trueCh; //the true choice counter
var score = 0;
var loseEl = document.getElementById("lose"); //lose message element
var winEl = document.getElementById("win"); //win message element
var endEl = document.getElementById("end"); // end element
//the essential actions before running the main function
function options(){
document.getElementById('start').style.display = '';
document.getElementById('end').style.display = 'none';
tilesNumber = document.querySelector('input[name=tiles-number]:checked').value;
console.log(typeof(tilesNumber));
createEl(tilesNumber-1);
var boardWidth = Math.sqrt(tilesNumber) * 90 + 16; //
document.getElementById('board').style.width = `${boardWidth}px`;
document.getElementById('board').classList.add('show');
document.getElementById('options').style.opacity = '0';
switch(tilesNumber){
case '9' : tiles = 4;
break;
case '16' : tiles = 7;
break;
case '25' : tiles = 10;
break;
}
starter();
}
function starter(){
document.getElementById('tiles').innerHTML= `tiles : ${tiles}`;
var allTiles = document.querySelectorAll(".tile");
allTiles.forEach(item => item.addEventListener('click',function(){
choose(Number(item.id));
this.classList.add('hover');
document.getElementById('card-effect').play();
}));
}
//the main function
function matrix() {
document.getElementById('start').style.display = 'none';
falseCh = 2;
trueCh = tiles;
setTimeout(function(){showTiles()},500);
setTimeout(function(){clear()},1500);
}
// creating a random number
function getRandomNumber(max){
return Math.floor(Math.random() * max);
}
//creating an array of random numbers
function getId(){
var id=[];
for(var i = 0 ; i < tiles ; i++){
var a = getRandomNumber(tilesNumber);
if(id.indexOf(a) > -1){
i--;
}else{
id[i] = a;
}
}
return id;
}
function showTiles(){
tileId = getId();
for(var i=0 ; i<tileId.length ; i++){
var box = document.getElementById(tileId[i]);
box.classList.add('hover');
}
}
//checking the input and compare with random numbers array
function choose(num){
var scoreEl=document.getElementById('score');
var a=tileId.indexOf(num);
const clickedTile = document.getElementById(num);
if(!clickedTile.classList.contains('hover')){
if(a>-1){
//the true choice
clickedTile.querySelector('.back').style.backgroundColor = "#2ECC71";
trueCh--;
score += 10;
if(trueCh == 0){
endEl.style.display = "block";
winEl.style.display = "block";
scoreEl.innerHTML=`score : ${score}`;
}
}else{
//the false choice
clickedTile.querySelector('.back').style.backgroundColor = "#ED5565";
falseCh--;
score -=20;
if(falseCh == 0){
endEl.style.display = "block";
loseEl.style.display = "block";
scoreEl.innerHTML=`score : ${score}`;
}
}
}
}
//back to default
function clear(){
var hoverTiles = document.querySelectorAll(".hover");
hoverTiles.forEach(item => {
item.classList.remove('hover');
item.querySelector('.back').removeAttribute('style');
});
}
//checking the elements status (default or changed)
function check(){
var hoverTiles = document.querySelectorAll(".hover");
return (hoverTiles.length > 0) ? false : true;
}
//playing again
function continu(){
endEl.style.display = "none";
loseEl.style.display = "none";
winEl.style.display = "none";
clear();
if(check()){
setTimeout(matrix,500);
}
}
// create html elements of matrix
function createEl(n){
for(var i= 0 ; i <= n ; i++){
var t = document.createElement("div");
t.id = i;
t.className = "tile";
var flipper = document.createElement('div');
flipper.className = "flipper";
var front = document.createElement('div');
front.className = "front";
var back = document.createElement("div");
back.className = "back";
flipper.appendChild(front);
flipper.appendChild(back);
t.appendChild(flipper);
document.querySelector('.matrix').appendChild(t);
}
}
function removeEl(){
var t = document.querySelectorAll('.tile');
console.log(t);
for(var i= 0 ; i < t.length ; i++){
document.querySelector('.matrix').removeChild(t[i]);
}
document.getElementById('board').classList.remove('show');
document.getElementById('options').style.opacity = '1';
}