-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (153 loc) · 4.31 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
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
const $ = document.querySelector.bind(document);
const $$ = selector => {
return Array.from(document.querySelectorAll.call(document, selector));
};
const arrayCreator = length => Array.from({ length }).map((_, idx) => idx);
const app = $("#app");
const container = $(".container");
const setNewStarter = currentNode => {
path = path || [];
starter = currentNode;
starter.className = "component ele starter";
stones
.filter(stone => stone !== starter)
.forEach(stone => {
stone.classList.remove("starter");
});
const index = path.indexOf(starter);
if (index !== -1) {
invalidPath = path.filter((_, idx) => idx > index);
invalidPath.forEach(node => {
node.className = "component ele";
});
path = path.filter((_, idx) => idx <= index);
}
};
const coordsCreator = (row, col) => {
return arrayCreator(row)
.map(row => {
return arrayCreator(col).map(col => {
return { x: row, y: col };
});
})
.reduce((accum, arr) => accum.concat(arr), []);
};
const originCoords = coordsCreator(3, 3);
console.log(originCoords);
let starterCoord = { x: 1, y: 1 };
let starter;
let path;
const getCoordinates = component => {
return [component.parentNode.dataset.row, component.dataset.col];
};
const handleClick = function(event) {
const myLocation = event.changedTouches[0];
const currentNode = document.elementFromPoint(
myLocation.clientX,
myLocation.clientY
);
if (!this.contains(currentNode)) {
return;
}
const nodeExisted = path.some(node => {
return node === currentNode;
});
if (nodeExisted) {
setNewStarter(currentNode);
}
const nodeCoodinates = getCoordinates(currentNode).map(Number);
const starterCoordinates = getCoordinates(starter).map(Number);
if (
starterCoordinates[0] === nodeCoodinates[0] &&
starterCoordinates[1] + 1 === nodeCoodinates[1] &&
currentNode.classList.contains("ele")
) {
starter.classList.add("right");
path.push(currentNode);
setNewStarter(currentNode);
}
if (
starterCoordinates[0] - 1 === nodeCoodinates[0] &&
starterCoordinates[1] === nodeCoodinates[1] &&
currentNode.classList.contains("ele")
) {
starter.classList.add("top");
path.push(currentNode);
setNewStarter(currentNode);
}
if (
starterCoordinates[0] + 1 === nodeCoodinates[0] &&
starterCoordinates[1] === nodeCoodinates[1] &&
currentNode.classList.contains("ele")
) {
starter.classList.add("down");
path.push(currentNode);
setNewStarter(currentNode);
}
if (
starterCoordinates[0] === nodeCoodinates[0] &&
starterCoordinates[1] - 1 === nodeCoodinates[1] &&
currentNode.classList.contains("ele")
) {
starter.classList.add("left");
path.push(currentNode);
setNewStarter(currentNode);
}
if (path.length === originCoords.length) {
setTimeout(alert, 500, "You win");
setNewStarter(path[0]);
}
};
function component(col) {
return `
<div class="component" data-col=${col}>
</div>
`;
}
function createRow(rowNumber, rowHTML) {
const row = document.createElement("div");
row.className = `row`;
row.dataset.row = rowNumber;
row.innerHTML = rowHTML;
return row;
}
const getX = arr => arr.x;
const getY = arr => arr.y;
const rowsNum = Math.max(...originCoords.map(getX));
const colsNum = Math.max(...originCoords.map(getY));
const rowsArray = arrayCreator(rowsNum + 1);
const colsArray = arrayCreator(colsNum + 1);
const backgroundDivs = rowsArray.map(row => {
const rowsHTMLString = colsArray
.map(col => {
return component(col);
})
.join("");
return createRow(row, rowsHTMLString);
});
const renderDivs = divsArr => {
divsArr.forEach(div => {
container.appendChild(div);
});
};
renderDivs(backgroundDivs);
const components = $$(".component");
const stones = components.filter(component => {
const [row, col] = getCoordinates(component);
return originCoords.find(coord => {
return coord.x == row && coord.y == col;
});
});
stones.forEach(component => {
component.classList.add("ele");
});
container.addEventListener("touchstart", handleClick);
container.addEventListener("touchmove", handleClick);
starter =
starter ||
$$(".ele").find(stone => {
const [row, col] = getCoordinates(stone);
return row == starterCoord.x && col == starterCoord.y;
});
path = path || [starter];
setNewStarter(starter);