-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabyrinth.html
435 lines (403 loc) · 13.3 KB
/
Labyrinth.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cognitive Labyrinth</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 0;
background-color: #f5f5f5;
}
/* Scale the entire page to 55% only on screens wider than 600px (desktop) */
@media screen and (min-width: 601px) {
body {
transform: scale(0.55);
transform-origin: top left;
}
}
h1, p {
text-align: center;
margin: 0.5rem;
}
#maze {
display: grid;
grid-template-columns: repeat(20, 1fr); /* Desktop default: 20x20 cells */
gap: 2px;
width: 90vw;
max-width: 90vw;
margin-top: 10px;
border: 2px solid #333;
background-color: #333;
position: relative;
}
.cell {
/* The padding-top ensures the height is proportional to width, keeping cells square */
width: 100%;
padding-top: 100%;
position: relative;
background-color: #eee;
box-sizing: border-box;
}
.cell.current {
background-color: #4CAF50;
border: 2px solid #388E3C;
}
.cell.path {
background-color: #eee;
}
.node {
background-color: #8e44ad;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
}
.end {
background-color: #ff6f61;
color: white;
font-weight: bold;
text-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.wall {
background-color: #333;
}
#prompt {
margin-top: 20px;
padding: 10px;
border: 1px solid #333;
width: 90vw;
max-width: 400px;
background-color: #ffffff;
border-radius: 8px;
}
#prompt p {
margin: 5px 0;
}
textarea, button, #apiKey {
width: 100%;
margin-top: 10px;
padding: 10px;
border-radius: 4px;
border: 1px solid #888;
font-size: 1rem;
box-sizing: border-box;
}
#controls {
display: flex;
justify-content: space-around;
width: 90vw;
max-width: 400px;
margin-top: 10px;
}
.control-btn {
background-color: #3f51b5;
color: white;
padding: 15px;
border: none;
border-radius: 8px;
font-size: 1.2rem;
flex: 1;
margin: 0 5px;
cursor: pointer;
}
#winMessage {
margin-top: 10px;
padding: 10px;
border: 1px solid #4CAF50;
background-color: #C8E6C9;
width: 90vw;
max-width: 400px;
border-radius: 8px;
text-align: center;
display: none;
}
@media screen and (max-width: 600px) {
/* Adjust the layout and maze size for smaller screens */
#maze {
grid-template-columns: repeat(10, 1fr);
}
/* Do not scale on mobile devices */
body {
transform: scale(1);
}
}
</style>
</head>
<body>
<h1>Cognitive Labyrinth</h1>
<p>Navigate the maze and engage with reflective prompts at each node. Find the end to complete the journey.</p>
<input id="apiKey" type="password" placeholder="Enter your OpenAI API Key" />
<div id="maze"></div>
<div id="prompt">
<p id="promptText">Start your journey by moving through the maze.</p>
<textarea id="response" placeholder="Your reflection here..."></textarea>
<button onclick="submitResponse()">Submit</button>
</div>
<div id="controls">
<button class="control-btn" onclick="move('up')">↑</button>
<button class="control-btn" onclick="move('left')">←</button>
<button class="control-btn" onclick="move('down')">↓</button>
<button class="control-btn" onclick="move('right')">→</button>
</div>
<div id="winMessage">Congratulations! You've reached the end of the Cognitive Labyrinth.</div>
<script>
const mazeContainer = document.getElementById("maze");
const promptText = document.getElementById("promptText");
let playerPosition = { x: 0, y: 0 };
// Determine device type and set appropriate maze size
const isMobile = window.innerWidth <= 600;
const mazeSize = isMobile ? 10 : 20; // 10x10 on mobile, 20x20 on desktop
let maze = Array.from({ length: mazeSize }, () => Array(mazeSize).fill("wall"));
// Generate a perfect maze using DFS algorithm
function generateMaze() {
const stack = [];
let currentCell = { x: 0, y: 0 };
maze[0][0] = "path";
stack.push(currentCell);
while (stack.length) {
const { x, y } = currentCell;
const neighbors = getUnvisitedNeighbors(x, y);
if (neighbors.length > 0) {
stack.push(currentCell);
// Choose a random neighbor
const { nx, ny } = neighbors[Math.floor(Math.random() * neighbors.length)];
carvePath(x, y, nx, ny);
currentCell = { x: nx, y: ny };
} else {
currentCell = stack.pop();
}
}
// Mark end cell
maze[mazeSize - 1][mazeSize - 1] = "end";
// **Minimal Fix**: Ensure the end cell is accessible by making sure one of its neighbors is a path
if (maze[mazeSize - 2] && maze[mazeSize - 2][mazeSize - 1] === "wall" &&
maze[mazeSize - 1][mazeSize - 2] === "wall") {
// If both the cell above and the cell to the left are walls, make the cell above a path
maze[mazeSize - 2][mazeSize - 1] = "path";
}
// Mark random path cells as nodes (fewer nodes on smaller mazes)
const nodeCount = isMobile ? 3 : 5;
for (let i = 0; i < nodeCount; i++) {
let nodeX, nodeY;
do {
nodeX = Math.floor(Math.random() * mazeSize);
nodeY = Math.floor(Math.random() * mazeSize);
} while (
maze[nodeY][nodeX] !== "path" ||
(nodeX === 0 && nodeY === 0) ||
(nodeX === mazeSize - 1 && nodeY === mazeSize - 1)
);
maze[nodeY][nodeX] = "node";
}
}
function getUnvisitedNeighbors(x, y) {
const neighbors = [];
if (y - 2 >= 0 && maze[y - 2][x] === "wall") neighbors.push({ nx: x, ny: y - 2 });
if (y + 2 < mazeSize && maze[y + 2][x] === "wall") neighbors.push({ nx: x, ny: y + 2 });
if (x - 2 >= 0 && maze[y][x - 2] === "wall") neighbors.push({ nx: x - 2, ny: y });
if (x + 2 < mazeSize && maze[y][x + 2] === "wall") neighbors.push({ nx: x + 2, ny: y });
return neighbors;
}
function carvePath(x, y, nx, ny) {
// Mark the path between the current cell and the chosen cell
if (x === nx) {
// Vertical movement
maze[(y + ny) / 2][x] = "path";
} else if (y === ny) {
// Horizontal movement
maze[y][(x + nx) / 2] = "path";
}
maze[ny][nx] = "path";
}
function renderMaze() {
mazeContainer.innerHTML = '';
maze.forEach((row, y) => {
row.forEach((cell, x) => {
const cellDiv = document.createElement("div");
cellDiv.classList.add("cell");
cellDiv.dataset.x = x;
cellDiv.dataset.y = y;
if (cell === "wall") {
cellDiv.classList.add("wall");
} else if (cell === "end") {
cellDiv.classList.add("end");
cellDiv.textContent = "END";
} else if (cell === "node") {
cellDiv.classList.add("node");
cellDiv.textContent = "NODE";
} else {
cellDiv.classList.add("path");
}
mazeContainer.appendChild(cellDiv);
});
});
// Mark the player's starting position
mazeContainer.querySelector(`.cell[data-x="0"][data-y="0"]`).classList.add("current");
}
generateMaze();
renderMaze();
// Movement handling for up, down, left, right
function move(direction) {
const { x, y } = playerPosition;
let newX = x, newY = y;
if (direction === "up" && y > 0) newY--;
if (direction === "down" && y < mazeSize - 1) newY++;
if (direction === "left" && x > 0) newX--;
if (direction === "right" && x < mazeSize - 1) newX++;
if (maze[newY][newX] === "wall") {
promptText.textContent = "You hit a wall! Reflect and try another direction.";
return;
}
const currentCellDiv = mazeContainer.querySelector(`.cell[data-x="${x}"][data-y="${y}"]`);
currentCellDiv.classList.remove("current");
playerPosition = { x: newX, y: newY };
const nextCellDiv = mazeContainer.querySelector(`.cell[data-x="${newX}"][data-y="${newY}"]`);
nextCellDiv.classList.add("current");
if (newX === mazeSize - 1 && newY === mazeSize - 1) {
// Player reached the end
promptText.textContent = "Congratulations! You've reached the end of the Cognitive Labyrinth.";
document.getElementById("winMessage").style.display = "block";
} else if (maze[newY][newX] === "node") {
// Display random reflection prompt
promptText.textContent = getRandomPrompt();
} else {
promptText.textContent = "Keep exploring the maze...";
}
}
// Random prompt generator
function getRandomPrompt() {
const prompts = [
"What aspect of your studies are you focusing on now?",
"Reflect on a recent project challenge you've faced.",
"Think about an idea you encountered in game design.",
"What are you curious about in creative technology?",
"What are your goals in 3D modeling or design?"
];
return prompts[Math.floor(Math.random() * prompts.length)];
}
// Use OpenAI GPT-3.5 Turbo API for flexible response
async function submitResponse() {
const response = document.getElementById("response").value;
const apiKey = document.getElementById("apiKey").value.trim();
if (!apiKey) {
promptText.textContent = "Please enter your API key.";
return;
}
if (!response) {
promptText.textContent = "Please write a reflection before submitting.";
return;
}
try {
const res = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: response }],
max_tokens: 150
})
});
const data = await res.json();
if (data.error) {
promptText.textContent = `Error: ${data.error.message}`;
} else {
promptText.textContent = data.choices[0].message.content.trim();
}
} catch (error) {
promptText.textContent = "Error: Could not generate a response.";
}
document.getElementById("response").value = "";
}
// Add mobile swipe gesture support
let startX, startY, endX, endY;
function handleTouchStart(event) {
if (event.target.closest('#maze')) {
// If touch event starts inside the maze container, prevent default scroll
event.preventDefault();
}
startX = event.touches[0].clientX;
startY = event.touches[0].clientY;
}
function handleTouchMove(event) {
if (event.target.closest('#maze')) {
// If the finger moves inside the maze container, prevent default scrolling
event.preventDefault();
}
endX = event.touches[0].clientX;
endY = event.touches[0].clientY;
}
function handleTouchEnd(event) {
if (!startX || !startY || !endX || !endY) {
return;
}
// If the user started the swipe in the maze container, interpret as game movement
if (event.target.closest('#maze')) {
event.preventDefault();
const deltaX = endX - startX;
const deltaY = endY - startY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
// Horizontal swipe
if (deltaX > 0) {
move('right');
} else {
move('left');
}
} else {
// Vertical swipe
if (deltaY > 0) {
move('down');
} else {
move('up');
}
}
}
// Reset the values
startX = null;
startY = null;
endX = null;
endY = null;
}
document.addEventListener('touchstart', handleTouchStart, { passive: false });
document.addEventListener('touchmove', handleTouchMove, { passive: false });
document.addEventListener('touchend', handleTouchEnd, { passive: false });
// Add arrow key support for movement
document.addEventListener('keydown', handleKeyDown);
function handleKeyDown(event) {
switch (event.key) {
case 'ArrowUp':
move('up');
event.preventDefault();
break;
case 'ArrowDown':
move('down');
event.preventDefault();
break;
case 'ArrowLeft':
move('left');
event.preventDefault();
break;
case 'ArrowRight':
move('right');
event.preventDefault();
break;
default:
// If the key pressed is not an arrow key, do nothing
break;
}
}
</script>
</body>
</html>