-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame copy.js
More file actions
106 lines (85 loc) · 2.26 KB
/
game copy.js
File metadata and controls
106 lines (85 loc) · 2.26 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
const squares = document.querySelectorAll('.square');
const mole = document.querySelector('.mole');
const score = document.querySelector("#score");
const grid = document.querySelector('.grid');
const play = document.querySelector('.play');
const btn_play = document.querySelector('.btn_play');
let data = {
fill : [],
score : 0,
count : 10,
hitPosition:[]
};
function getRandom(){
const nums = [0,1,2,3,4,5,6,7,8];
let result = [];
function in_array( array, el){
for( let i = 0; i < array.length; i++){
if(array[i]==el) return true;
};
return false
};
function get_rand(array){
let rand = array[Math.floor(Math.random()*array.length)];
if(!in_array(result, rand)){
result.push(rand);
return result;
};
return get_rand(array);
};
for(let i = 0; i < 3;i++){
get_rand(nums);
}
return result;
};
function refill(){
squares.forEach( square => {
square.classList.remove('mole');
});
data.fill = getRandom();
data.hitPosition = [];
for (let i = 0; i < 3; i++){
let temp = squares[data.fill[i]];
temp.classList.add('mole');
data.hitPosition[i] = temp.id;
temp = null;
};
};
function init(){
data.fill = [];
data.score = 0;
data.count = 10;
data.hitPosition = [];
};
squares.forEach( square => {
square.addEventListener('click', function (event) {
if(data.hitPosition.includes(square.id)){
data.score++;
data.count--;
score.textContent = data.score;
data.hitPosition = [];
if(data.count == 0){
grid.style.display="none";
play.style.display='block';
init();
}else{
refill();
};
}else{
data.count--;
data.hitPosition = [];
if(data.count == 0){
grid.style.display="none";
play.style.display='block';
init();
}else{
refill();
};
};
});
});
btn_play.addEventListener('click', function(){
grid.style.display="flex";
play.style.display='none';
});
refill();