-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquare-code.js
71 lines (63 loc) · 2.24 KB
/
square-code.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
function init(n, orientations) {
const rowContainer = document.getElementById("row-container");
rowContainer.innerHTML = "";
const w = document.getElementsByClassName("container").item(0).clientWidth - 20;
console.log(w);
for (let i = 0; i < n; i++) {
const row = document.createElement("div");
for (let j = 0; j < n; j++) {
const button = document.createElement("button");
button.id = i + "," + j;
button.innerText = "↑";
button.style.width = w / n + "px";
button.style.height = w / n + "px";
button.style.borderRadius = w / (2 * n) + "px";
button.style.fontSize = w / (2.5 * n) + "px";
button.style.rotate = "0deg";
button.addEventListener("click", () => {
turnNeighbor(n, button, orientations);
});
row.appendChild(button);
}
rowContainer.appendChild(row);
}
}
function turnNeighbor(n, button, orientations) {
const id = button.id;
const parts = id.split(",");
const x = parseInt(parts[0].trim());
const y = parseInt(parts[1].trim());
turnNeighborByPosition(n, x, y, orientations);
}
function turnNeighborByPosition(n, x, y, orientations) {
turnByPositionIfValid(n, x - 1, y - 1, orientations);
turnByPositionIfValid(n, x , y - 1, orientations);
turnByPositionIfValid(n, x + 1, y - 1, orientations);
turnByPositionIfValid(n, x - 1, y , orientations);
turnByPositionIfValid(n, x , y , orientations);
turnByPositionIfValid(n, x + 1, y , orientations);
turnByPositionIfValid(n, x - 1, y + 1, orientations);
turnByPositionIfValid(n, x , y + 1, orientations);
turnByPositionIfValid(n, x + 1, y + 1, orientations);
}
function turn(btn, orientations) {
var deg = parseInt(btn.style.rotate.replace("deg", ""));
btn.style.rotate = (deg + 360 / orientations) + "deg";
}
function turnByPositionIfValid(grid_n, x, y, orientations) {
if (x >= 0 && x < grid_n && y >= 0 && y < grid_n) {
turn(document.getElementById(x + "," + y), orientations);
}
}
function generate() {
n = document.getElementById("size").value
orientations = document.getElementById("orientation").value
init(n, orientations);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
for (let t = 0; t < Math.floor(Math.random() * orientations); t++) {
turnNeighborByPosition(n, i, j, orientations);
}
}
}
}