-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_grid.html
155 lines (138 loc) Β· 4.09 KB
/
memory_grid.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="urf-8">
<meta name="viewport" content="width=device-width, initial-scale=.8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Memory Grid - Game</title>
<link rel="icon" type="image/png" href="/images/rahul3v.png">
<style type="text/css">
*,
*::before,
*::after {box-sizing: border-box;}
body {margin:0;background: #f5f5f5;user-select:none;-moz-user-select:none;touch-action: manipulation;}
body,html,#game{height: 100%;}
.grid {
display:grid;
height:100%;
place-content: center;
grid-template: repeat(4,16vmin)/repeat(6,16vmin);
grid-gap: .66vmin;
}
.card {
position: relative;
transition: all .4s linear;
transform-style: preserve-3d;
}
.card,.back,.front {
height: 100%;
width: 100%;
}
.back,.front {
position: absolute;
backface-visibility: hidden;
}
.front {
background: #42FAB3 url('/images/rahul3v.png') no-repeat center center / contain scroll; /*#FAB942*/
cursor:pointer;
}
.back {
transform: rotateY(180deg);
font-size: 10vmin;
display: flex;
justify-content:center;
align-items:center;
background-color: #f27d9a;
}
.selected {transform: rotateY(180deg);}
.match .front {
visibility: hidden;
pointer-events: none; /**/
}
.fixed{position:fixed;top:0;left:0;font-size:20px;background-color:#42FAB3;padding:5px 16px;box-shadow:0 2px 4px rgba(0,0,0,0.4);}
</style>
</head>
<body>
<div class="fixed">Memory Grid - Game</div>
<div id="game"></div>
<script>
const cardsArray = [
{'name': 'a','text':'π'},
{'name': 'b','text':'π'},
{'name': 'c','text':'π
'},
{'name': 'd','text':'π'},
{'name': 'e','text':'π'},
{'name': 'f','text':'π'},
{'name': 'g','text':'π'},
{'name': 'h','text':'π'},
{'name': 'i','text':'π'},
{'name': 'j','text':'π'},
{'name': 'k','text':'π'},
{'name': 'l','text':'π'}
];
var gameGrid = cardsArray.concat(cardsArray);
var firstGuess = secondGuess = '',
count = score=0,
chance=-12,
delay = 800,
game = document.getElementById('game'),
grid = document.createElement('section');
grid.setAttribute('class', 'grid');
var creategrid = ()=>{
game.appendChild(grid);
gameGrid=gameGrid.sort(() => 0.5 - Math.random());
//console.log(gameGrid);
gameGrid.forEach(item => {
let card = document.createElement('div');
card.classList.add('card');
card.dataset.name = item.name;
let front = document.createElement('div');
front.classList.add('front');
let back = document.createElement('div');
back.classList.add('back');
back.textContent=item.text;
grid.appendChild(card);
card.appendChild(front);
card.appendChild(back);
});
};
creategrid();
var match = () => {
let selected = document.querySelectorAll('.selected');
selected.forEach(card => { card.classList.add('match'); });
};
var resetGuesses = () => {
firstGuess = secondGuess = '';
count = 0;
let selected = document.querySelectorAll('.selected');
selected.forEach(card => { card.classList.remove('selected'); });
};
var resetAll = () => {
count = score=0;
chance=-12;
grid.innerHTML="";
creategrid();
};
grid.addEventListener('click', e => {
var clicked = e.target;
//console.log(clicked.nodeName);
if (clicked.nodeName === 'SECTION' || clicked.parentNode.classList.contains('selected') || clicked.parentNode.classList.contains('match')|| clicked.parentNode.classList.contains('grid')) {
return;
}
if (count < 2) {
count++;
clicked.parentNode.classList.add('selected');
if (count === 1) {
firstGuess = clicked.parentNode.dataset.name; //console.log(firstGuess);
} else {
secondGuess = clicked.parentNode.dataset.name; //console.log(secondGuess);
chance++;
if (firstGuess === secondGuess)setTimeout(match, delay),score++;
setTimeout(resetGuesses, delay);
if (score==12) {alert("you win!, after : "+chance+" wrong attempts !"),resetAll();}
}
}
});
</script>
</body>
</html>