-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
307 lines (268 loc) · 8.19 KB
/
index.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Amazing maze</title>
<style>
/* Ensure the body takes up the entire viewport */
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
overflow: hidden; /* Prevent scrolling */
}
.container {
text-align: center;
width: 90vw; /* Limit the width of the grid to 90% of the viewport */
max-width: 600px; /* Add a maximum width for large screens */
}
.grid-wrapper {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 90vw; /* Make the wrapper responsive */
max-width: 600px;
aspect-ratio: 1;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr); /* 4 columns with equal size */
grid-template-rows: repeat(4, 1fr); /* 4 rows with equal size */
gap: 5px;
aspect-ratio: 1; /* Maintain a perfect square aspect ratio */
width: 80%; /* Make the grid fill the container's width */
max-width: 90vh; /* Prevent the grid from exceeding the height of the viewport */
margin: 0 auto;
border: 2px solid #333;
}
.grid .cell {
background-color: #fff;
border: 1px dashed #aaa;
display: flex;
justify-content: center;
align-items: center;
}
.fixed {
background-color: #0077cc; /* A distinct color for the fixed square */
pointer-events: none; /* Disable interaction */
cursor: not-allowed;
}
/* Arrow Styles */
.arrow {
position: absolute;
width: 6%;
height: 6%;
background-color: #00aaff;
clip-path: polygon(0% 50%, 100% 0%, 100% 100%);
}
#start-arrow {
top: 77%; /* Align with the grid vertically */
left: 0%; /* Positioned to the left of grid cell 4 */
transform: rotate(180deg);
}
#finish-arrow {
top: 15%; /* Align with grid cell 11 */
left: 94%; /* Positioned to the right of grid cell 11 */
transform: rotate(180deg);
}
.squares {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 5px;
margin-top: 10px;
}
.square {
flex: 1 0 calc(25% - 10px); /* Adjust size of squares based on grid size */
max-width: 40px; /* Prevent them from growing too large on big screens */
height: auto;
aspect-ratio: 1; /* Keep squares as perfect squares */
background-color: #00aaff;
display: flex;
justify-content: center;
align-items: center;
border-radius: 5px;
cursor: pointer;
transition: transform 0.2s ease;
text-align: center;
}
.square svg {
width: 50%;
height: 50%;
fill: none;
stroke: #fff;
stroke-width: 10;
stroke-linecap: round;
}
.square.selected {
border: 2px solid #ff0000;
box-shadow: 0 0 10px rgba(255, 0, 0, 0.5);
}
.rotate-controls {
margin-top: 10px;
text-align: center;
}
#rotate-btn {
background-color: #0077cc;
color: #fff;
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
border-radius: 5px;
outline: none;
transition: background-color 0.3s;
}
#rotate-btn:disabled {
background-color: #aaa;
cursor: not-allowed;
}
#rotate-btn:hover:enabled {
background-color: #005fa3;
}
/* Media query for small screens */
@media (max-height: 600px) {
.container {
width: 95vw;
max-width: 400px;
}
}
</style>
</head>
<body>
<div class="container">
<div id="grid-wrapper" class="grid-wrapper">
<div id="start-arrow" class="arrow"></div>
<div id="grid" class="grid"></div>
<div id="finish-arrow" class="arrow"></div>
</div>
<div id="rotate-controls" class="rotate-controls">
<button id="rotate-btn" disabled>Rotate</button>
</div>
<div id="squares" class="squares"></div>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const grid = document.getElementById("grid");
const squaresContainer = document.getElementById("squares");
const rotateButton = document.getElementById("rotate-btn");
let selectedSquare = null; // To track the currently selected square
const rotationAngles = new Map(); // To store the rotation angle for each square
// Define possible paths (L-shape, vertical, horizontal)
const paths = {
'v': '<path d="M50,0 L50,100" />', // Vertical line
'h': '<path d="M0,50 L100,50" />', // Horizontal line
'bl': '<path d="M0,50 L50,50 L50,100" />', // L-shape bottom left
'tr': '<path d="M50,0 L50,50 L100,50" />', // L-shape top right
'tl': '<path d="M50,0 L50,50 L0,50" />', // L-shape top left
'br': '<path d="M100,50 L50,50 L50,100" />', // L-shape bottom right
'c': '<path d="M50,0 L50,100 L50,50 L0,50 L100,50" />' // cross
};
/*
const whole_path = [
'h', 'h', 'h', 'bl',
'br', 'bl', 'br', 'tl',
'v', 'tr', 'tl', 'br',
'tr', 'h', 'h', 'tl',
];
*/
/*
const whole_path = [
'br', 'h', 'h', 'bl',
'tl', 'br', 'h', 'tl',
'br', 'bl', 'br',
'tr', 'tl', 'tr', 'tl',
];
*/
const whole_path = [
'br', 'bl', 'br', 'h',
'v', 'tr', 'bl',
'tr', 'h', 'tl', 'v',
'h', 'h', 'h', 'tl',
];
const shuffled_whole_path = whole_path
.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value);
const fixedGridId = 6;
// Create 16 grid cells
for (let i = 0; i < 16; i++) {
const cell = document.createElement("div");
cell.classList.add("cell");
cell.setAttribute("data-cell", i);
grid.appendChild(cell);
cell.addEventListener("click", () => {
if (selectedSquare) {
moveSquareToCell(selectedSquare, cell);
}
});
}
const fixedSquare = document.createElement("div");
fixedSquare.classList.add("square", "fixed");
fixedSquare.innerHTML = `<svg viewBox="0 0 100 100">${paths['c']}</svg>`;
grid.children[fixedGridId].appendChild(fixedSquare); // Place in cell 0
// Create 15 selectable squares
for (let i = 0; i < 15; i++) {
const square = document.createElement("div");
square.classList.add("square");
square.textContent = i + 1;
square.setAttribute("data-square", i);
//square.style.width = document.querySelector('[data-cell="0"]').offsetWidth;
// Add an SVG line or L-shape
const randomPath = paths[shuffled_whole_path[i]];
square.innerHTML = `<svg viewBox="0 0 100 100">${randomPath}</svg>`;
// Set a random initial rotation
const randomRotation = Math.floor(Math.random() * 4) * 90; // 0, 90, 180, or 270
rotationAngles.set(square, randomRotation);
square.querySelector("svg").style.transform = `rotate(${randomRotation}deg)`;
square.addEventListener("click", () => selectSquare(square));
squaresContainer.appendChild(square);
}
rotateButton.addEventListener("click", () => {
if (selectedSquare) {
rotateSquare(selectedSquare);
}
});
function selectSquare(square) {
// Deselect currently selected square, if any
if (selectedSquare) {
selectedSquare.classList.remove("selected");
}
// Select the new square
selectedSquare = square;
square.classList.add("selected");
// Enable rotate button
rotateButton.disabled = false;
}
function moveSquareToCell(square, cell) {
// Only move if the cell is empty
if (!cell.firstChild) {
square.classList.remove("selected"); // Remove selection
selectedSquare = null;
// Move square to cell
cell.appendChild(square);
// Reset styles for proper display inside the grid
square.style.position = "relative";
square.style.left = "0";
square.style.top = "0";
}
}
function rotateSquare(square) {
let currentAngle = rotationAngles.get(square) || 0;
currentAngle = (currentAngle + 90) % 360; // Increment rotation by 90°
rotationAngles.set(square, currentAngle);
// Apply rotation to the square's SVG
const svg = square.querySelector("svg");
svg.style.transform = `rotate(${currentAngle}deg)`;
}
});
</script>
</body>
</html>