forked from ErykFryderyk/Sort-Ball-Game-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (73 loc) · 2.86 KB
/
script.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
const $reloadBtn = document.querySelector('.reload-btn');
const $box = document.querySelector('.box');
const $rectangle = document.querySelector('.rectangle');
const $allRectangle = document.querySelectorAll('.rectangle');
const $numberOfRectangles = $allRectangle.length - 1;
const $bodyOverlay = document.querySelector('.body-overlay');
let $activeBall = null;
let $score= 0;
$reloadBtn.addEventListener('click', () => {
location.reload();
});
const chooseBall = (e) =>{
const parent = e.target.parentElement;
if($activeBall === null){
if(parent.childElementCount > 0){
parent.firstElementChild.classList.toggle('ball-active');
$activeBall = parent.firstElementChild;
if($activeBall.classList.contains('ball-error')){
$activeBall.classList.remove('ball-error');
}
}else if(e.target.classList.contains('rectangle')){
e.target.firstElementChild.classList.toggle('ball-active');
$activeBall = e.target.firstElementChild;
}
}else {
moveBallToSelectedRectangle(e);
}
}
const moveBallToSelectedRectangle = (e) =>{
target = e.target;
if (target === $activeBall || target === $activeBall.parentElement.children[1] || target === $activeBall.parentElement.children[2]){
$activeBall.classList.remove('ball-active');
$activeBall=null;
}
if (target.classList.contains('rectangle')){
const el = document.createElement('div')
el.setAttribute('title', $activeBall.title);
el.setAttribute('class', $activeBall.classList);
el.classList.remove('ball-active');
target.prepend(el);
$activeBall.remove();
$activeBall=null;
}else if(target.parentElement.classList.contains('rectangle')){
const el = document.createElement('div')
el.setAttribute('title', $activeBall.title);
el.setAttribute('class', $activeBall.classList);
el.classList.remove('ball-active');
target.parentElement.prepend(el);
$activeBall.remove();
$activeBall = null;
}
}
const checkingSortBalls = () => {
const countParents = $allRectangle.length;
const colorOfRectangles = [];
for (let i = 0; i < countParents; i++) {
const element = $allRectangle[i]
if(element.childElementCount === 5){
const titleBall = element.children[1].title;
if (titleBall === element.firstElementChild.title){
$score++;
colorOfRectangles.push(titleBall);
}
}
if(colorOfRectangles.length === $numberOfRectangles){
$bodyOverlay.style.display = "none";
}
}
}
$box.addEventListener('click', e =>{
chooseBall(e)
checkingSortBalls();
});