-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
130 lines (119 loc) · 4.21 KB
/
main.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
window.onload = function() {
const randomColor = () => {
const color = [
["#f1a9a0", "#e08283"],
["#ffcb05", "#f9690e"],
["#e4f1fe", "#c5eff7"],
["#87d37c", "#26c281"],
["#89c4f4", "#3498db"],
["#ffcb05", "#f9b42d"]
];
return color[Math.floor(Math.random() * (5 - 0 + 1)) + 0];
};
document.getElementById("start-button").onclick = function() {
document.getElementById("home").style.display = "none";
document.getElementById("game-board").style.display = "grid";
let newColor = randomColor();
let level = 1;
startGame(newColor, level);
};
function startGame(newColor, level) {
const colors = num => {
let colorScale = d3
.scaleLinear()
.domain([0, 5])
.range(newColor);
return colorScale(num);
};
let selected = [];
let moves = 100;
document.getElementById("counterNum").innerHTML = `<p>${moves}</p>`;
document.getElementById("levelNum").innerHTML = `<p>${level}</p>`;
let gridsArr = new Array(36).fill("").map((e, i) => {
return `<div class="grid" id="item-${i}#" style="background-color: ${colors(
i
)}"></div>`;
});
const orderArr = [...gridsArr];
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
document.querySelector("#grids").innerHTML = gridsArr.join(" ");
setTimeout(() => {
gridsArr = shuffle(gridsArr);
document.querySelector("#grids").innerHTML = gridsArr.join(" ");
}, 800);
setInterval(() => {
gridsArr.forEach((grid, i) => {
return (document.getElementById(`item-${i}#`).onclick = e => {
if (selected.includes(e.toElement)) {
selected = selected.filter(elem => elem !== e.toElement);
e.toElement.classList.toggle("clickedGrid");
e.toElement.classList.toggle("grid");
} else if (selected.length < 2) {
selected.push(e.toElement);
e.toElement.classList.toggle("clickedGrid");
e.toElement.classList.toggle("grid");
if (selected.length === 2) move(selected);
}
});
});
}, 100);
function move(gridsArrSelect) {
let position1;
let position2;
gridsArr.forEach((elem, i) => {
let element = elem.substring(
elem.lastIndexOf("m-") + 2,
elem.lastIndexOf("#")
);
let elSelect1 = gridsArrSelect[0].outerHTML.substring(
gridsArrSelect[0].outerHTML.lastIndexOf("m-") + 2,
gridsArrSelect[0].outerHTML.lastIndexOf("#")
);
let elSelect2 = gridsArrSelect[1].outerHTML.substring(
gridsArrSelect[1].outerHTML.lastIndexOf("m-") + 2,
gridsArrSelect[1].outerHTML.lastIndexOf("#")
);
if (element == elSelect1) position1 = i;
if (element == elSelect2) position2 = i;
});
let newArray = ([gridsArr[position1], gridsArr[position2]] = [
gridsArr[position2],
gridsArr[position1]
]);
document.querySelector("#grids").innerHTML = gridsArr.join(" ");
if (moves <= 1) gameOver();
else moves--;
document.getElementById("counterNum").innerHTML = `<p>${moves}</p>`;
if (gridsArr.toString() === orderArr.toString()) {
level++;
gridsArr = [];
let newColor = randomColor();
startGame(newColor, level);
document.getElementById("levelNum").innerHTML = `<p>${level}</p>`;
console.log(level);
}
selected = [];
}
function gameOver() {
document.getElementById("home").style.display = "none";
document.getElementById("game-board").style.display = "none";
document.getElementById("game-over-display").style.display = "flex";
}
document.getElementById("reset-button").onclick = function() {
gridsArr = [];
startGame(newColor, level);
};
document.getElementById("abort-button").onclick = function() {
gridsArr = [];
document.getElementById("home").style.display = "grid";
document.getElementById("game-board").style.display = "none";
};
document.getElementById("random-button").onclick = function() {
gridsArr = [];
let newColor = randomColor();
startGame(newColor, level);
};
}
};