asweigart

circular maze, generated by claude ai

Aug 21st, 2025
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.88 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Circular Maze Game</title>
  7. <style>
  8. body {
  9. margin: 0;
  10. padding: 20px;
  11. background: linear-gradient(135deg, #1e3c72, #2a5298);
  12. font-family: Arial, sans-serif;
  13. display: flex;
  14. flex-direction: column;
  15. align-items: center;
  16. min-height: 100vh;
  17. color: white;
  18. }
  19.  
  20. .game-container {
  21. background: rgba(255, 255, 255, 0.1);
  22. border-radius: 20px;
  23. padding: 20px;
  24. backdrop-filter: blur(10px);
  25. box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  26. }
  27.  
  28. #gameCanvas {
  29. border: 3px solid #fff;
  30. border-radius: 15px;
  31. background: #000;
  32. display: block;
  33. }
  34.  
  35. .controls {
  36. margin-top: 20px;
  37. text-align: center;
  38. font-size: 18px;
  39. }
  40.  
  41. .instructions {
  42. margin-top: 15px;
  43. text-align: center;
  44. opacity: 0.8;
  45. }
  46.  
  47. .status {
  48. margin-top: 10px;
  49. text-align: center;
  50. font-size: 20px;
  51. font-weight: bold;
  52. color: #4CAF50;
  53. }
  54.  
  55. button {
  56. margin-top: 15px;
  57. padding: 10px 20px;
  58. font-size: 16px;
  59. background: linear-gradient(45deg, #FF6B6B, #4ECDC4);
  60. border: none;
  61. border-radius: 25px;
  62. color: white;
  63. cursor: pointer;
  64. transition: transform 0.2s;
  65. }
  66.  
  67. button:hover {
  68. transform: scale(1.05);
  69. }
  70. </style>
  71. </head>
  72. <body>
  73. <div class="game-container">
  74. <canvas id="gameCanvas" width="600" height="600"></canvas>
  75. <div class="controls">Use WASD or Arrow Keys to move</div>
  76. <div class="instructions">Navigate from the blue entrance to the red goal on the outer ring!</div>
  77. <div class="status" id="status"></div>
  78. <button onclick="generateNewMaze()">Generate New Maze</button>
  79. </div>
  80.  
  81. <script>
  82. const canvas = document.getElementById('gameCanvas');
  83. const ctx = canvas.getContext('2d');
  84. const statusDiv = document.getElementById('status');
  85.  
  86. const CANVAS_SIZE = 600;
  87. const CENTER = CANVAS_SIZE / 2;
  88. const OUTER_RADIUS = 280;
  89. const INNER_RADIUS = 40;
  90. const RINGS = 12;
  91. const SECTORS = 32;
  92.  
  93. let maze = [];
  94. let player = { ring: 0, sector: 0 };
  95. let entrance = { ring: 0, sector: 0 };
  96. let goal = { ring: 0, sector: 0 };
  97. let gameWon = false;
  98.  
  99. class MazeCell {
  100. constructor(ring, sector) {
  101. this.ring = ring;
  102. this.sector = sector;
  103. this.walls = {
  104. inner: true, // wall towards center
  105. outer: true, // wall towards outside
  106. clockwise: true, // wall in clockwise direction
  107. counterclockwise: true // wall in counter-clockwise direction
  108. };
  109. this.visited = false;
  110. }
  111. }
  112.  
  113. function initializeMaze() {
  114. maze = [];
  115. for (let ring = 0; ring < RINGS; ring++) {
  116. maze[ring] = [];
  117. for (let sector = 0; sector < SECTORS; sector++) {
  118. maze[ring][sector] = new MazeCell(ring, sector);
  119. }
  120. }
  121. }
  122.  
  123. function generateMaze() {
  124. initializeMaze();
  125.  
  126. // Create multiple random paths to ensure multiple solutions
  127. const numPaths = 8;
  128.  
  129. for (let pathNum = 0; pathNum < numPaths; pathNum++) {
  130. // Start from random position
  131. let currentRing = Math.floor(Math.random() * RINGS);
  132. let currentSector = Math.floor(Math.random() * SECTORS);
  133.  
  134. const pathLength = 15 + Math.floor(Math.random() * 20);
  135.  
  136. for (let step = 0; step < pathLength; step++) {
  137. maze[currentRing][currentSector].visited = true;
  138.  
  139. // Randomly choose direction to carve
  140. const directions = [];
  141.  
  142. // Can move inward
  143. if (currentRing > 0) directions.push('inner');
  144. // Can move outward
  145. if (currentRing < RINGS - 1) directions.push('outer');
  146. // Can move clockwise
  147. directions.push('clockwise');
  148. // Can move counter-clockwise
  149. directions.push('counterclockwise');
  150.  
  151. if (directions.length === 0) break;
  152.  
  153. const direction = directions[Math.floor(Math.random() * directions.length)];
  154. let nextRing = currentRing;
  155. let nextSector = currentSector;
  156.  
  157. switch (direction) {
  158. case 'inner':
  159. maze[currentRing][currentSector].walls.inner = false;
  160. nextRing = currentRing - 1;
  161. maze[nextRing][nextSector].walls.outer = false;
  162. break;
  163. case 'outer':
  164. maze[currentRing][currentSector].walls.outer = false;
  165. nextRing = currentRing + 1;
  166. maze[nextRing][nextSector].walls.inner = false;
  167. break;
  168. case 'clockwise':
  169. maze[currentRing][currentSector].walls.clockwise = false;
  170. nextSector = (currentSector + 1) % SECTORS;
  171. maze[nextRing][nextSector].walls.counterclockwise = false;
  172. break;
  173. case 'counterclockwise':
  174. maze[currentRing][currentSector].walls.counterclockwise = false;
  175. nextSector = (currentSector - 1 + SECTORS) % SECTORS;
  176. maze[nextRing][nextSector].walls.clockwise = false;
  177. break;
  178. }
  179.  
  180. currentRing = nextRing;
  181. currentSector = nextSector;
  182. }
  183. }
  184.  
  185. // Add some random openings to increase solutions
  186. for (let i = 0; i < 50; i++) {
  187. const ring = Math.floor(Math.random() * RINGS);
  188. const sector = Math.floor(Math.random() * SECTORS);
  189. const wallType = ['inner', 'outer', 'clockwise', 'counterclockwise'][Math.floor(Math.random() * 4)];
  190.  
  191. if (wallType === 'inner' && ring > 0) {
  192. maze[ring][sector].walls.inner = false;
  193. maze[ring - 1][sector].walls.outer = false;
  194. } else if (wallType === 'outer' && ring < RINGS - 1) {
  195. maze[ring][sector].walls.outer = false;
  196. maze[ring + 1][sector].walls.inner = false;
  197. } else if (wallType === 'clockwise') {
  198. maze[ring][sector].walls.clockwise = false;
  199. maze[ring][(sector + 1) % SECTORS].walls.counterclockwise = false;
  200. } else if (wallType === 'counterclockwise') {
  201. maze[ring][sector].walls.counterclockwise = false;
  202. maze[ring][(sector - 1 + SECTORS) % SECTORS].walls.clockwise = false;
  203. }
  204. }
  205. }
  206.  
  207. function setupGame() {
  208. // Place entrance and goal on outer ring
  209. entrance.ring = RINGS - 1;
  210. entrance.sector = Math.floor(Math.random() * SECTORS);
  211.  
  212. do {
  213. goal.ring = RINGS - 1;
  214. goal.sector = Math.floor(Math.random() * SECTORS);
  215. } while (goal.sector === entrance.sector);
  216.  
  217. // Start player at entrance
  218. player.ring = entrance.ring;
  219. player.sector = entrance.sector;
  220. gameWon = false;
  221. statusDiv.textContent = '';
  222. }
  223.  
  224. function drawMaze() {
  225. ctx.clearRect(0, 0, CANVAS_SIZE, CANVAS_SIZE);
  226.  
  227. for (let ring = 0; ring < RINGS; ring++) {
  228. const innerR = INNER_RADIUS + (ring * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  229. const outerR = INNER_RADIUS + ((ring + 1) * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  230.  
  231. for (let sector = 0; sector < SECTORS; sector++) {
  232. const startAngle = (sector * 2 * Math.PI) / SECTORS;
  233. const endAngle = ((sector + 1) * 2 * Math.PI) / SECTORS;
  234. const cell = maze[ring][sector];
  235.  
  236. ctx.strokeStyle = '#ffffff';
  237. ctx.lineWidth = 2;
  238.  
  239. // Draw radial walls (between sectors)
  240. if (cell.walls.clockwise) {
  241. ctx.beginPath();
  242. ctx.moveTo(CENTER + innerR * Math.cos(endAngle), CENTER + innerR * Math.sin(endAngle));
  243. ctx.lineTo(CENTER + outerR * Math.cos(endAngle), CENTER + outerR * Math.sin(endAngle));
  244. ctx.stroke();
  245. }
  246.  
  247. // Draw circular walls (between rings)
  248. if (cell.walls.outer && ring < RINGS - 1) {
  249. ctx.beginPath();
  250. ctx.arc(CENTER, CENTER, outerR, startAngle, endAngle);
  251. ctx.stroke();
  252. }
  253.  
  254. // Draw innermost circle
  255. if (ring === 0) {
  256. ctx.beginPath();
  257. ctx.arc(CENTER, CENTER, innerR, 0, 2 * Math.PI);
  258. ctx.stroke();
  259. }
  260. }
  261. }
  262.  
  263. // Draw outer boundary
  264. ctx.beginPath();
  265. ctx.arc(CENTER, CENTER, OUTER_RADIUS, 0, 2 * Math.PI);
  266. ctx.stroke();
  267. }
  268.  
  269. function drawPlayer() {
  270. const ring = player.ring;
  271. const sector = player.sector;
  272. const innerR = INNER_RADIUS + (ring * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  273. const outerR = INNER_RADIUS + ((ring + 1) * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  274. const midR = (innerR + outerR) / 2;
  275. const midAngle = ((sector + 0.5) * 2 * Math.PI) / SECTORS;
  276.  
  277. const x = CENTER + midR * Math.cos(midAngle);
  278. const y = CENTER + midR * Math.sin(midAngle);
  279.  
  280. // Player
  281. ctx.fillStyle = '#00FF00';
  282. ctx.beginPath();
  283. ctx.arc(x, y, 8, 0, 2 * Math.PI);
  284. ctx.fill();
  285. }
  286.  
  287. function drawEntranceAndGoal() {
  288. // Entrance (blue)
  289. const entranceInnerR = INNER_RADIUS + (entrance.ring * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  290. const entranceOuterR = INNER_RADIUS + ((entrance.ring + 1) * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  291. const entranceMidR = (entranceInnerR + entranceOuterR) / 2;
  292. const entranceAngle = ((entrance.sector + 0.5) * 2 * Math.PI) / SECTORS;
  293.  
  294. const entranceX = CENTER + entranceMidR * Math.cos(entranceAngle);
  295. const entranceY = CENTER + entranceMidR * Math.sin(entranceAngle);
  296.  
  297. ctx.fillStyle = '#0066FF';
  298. ctx.beginPath();
  299. ctx.arc(entranceX, entranceY, 12, 0, 2 * Math.PI);
  300. ctx.fill();
  301.  
  302. // Goal (red)
  303. const goalInnerR = INNER_RADIUS + (goal.ring * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  304. const goalOuterR = INNER_RADIUS + ((goal.ring + 1) * (OUTER_RADIUS - INNER_RADIUS) / RINGS);
  305. const goalMidR = (goalInnerR + goalOuterR) / 2;
  306. const goalAngle = ((goal.sector + 0.5) * 2 * Math.PI) / SECTORS;
  307.  
  308. const goalX = CENTER + goalMidR * Math.cos(goalAngle);
  309. const goalY = CENTER + goalMidR * Math.sin(goalAngle);
  310.  
  311. ctx.fillStyle = '#FF0000';
  312. ctx.beginPath();
  313. ctx.arc(goalX, goalY, 12, 0, 2 * Math.PI);
  314. ctx.fill();
  315. }
  316.  
  317. function canMove(fromRing, fromSector, toRing, toSector) {
  318. if (toRing < 0 || toRing >= RINGS) return false;
  319.  
  320. const normalizedToSector = (toSector + SECTORS) % SECTORS;
  321.  
  322. if (fromRing === toRing) {
  323. // Moving within same ring
  324. if (Math.abs(fromSector - normalizedToSector) === 1 ||
  325. Math.abs(fromSector - normalizedToSector) === SECTORS - 1) {
  326.  
  327. if (normalizedToSector === (fromSector + 1) % SECTORS) {
  328. // Moving clockwise
  329. return !maze[fromRing][fromSector].walls.clockwise;
  330. } else {
  331. // Moving counterclockwise
  332. return !maze[fromRing][fromSector].walls.counterclockwise;
  333. }
  334. }
  335. } else {
  336. // Moving between rings
  337. if (fromSector === normalizedToSector) {
  338. if (toRing === fromRing + 1) {
  339. // Moving outward
  340. return !maze[fromRing][fromSector].walls.outer;
  341. } else if (toRing === fromRing - 1) {
  342. // Moving inward
  343. return !maze[fromRing][fromSector].walls.inner;
  344. }
  345. }
  346. }
  347.  
  348. return false;
  349. }
  350.  
  351. function movePlayer(direction) {
  352. if (gameWon) return;
  353.  
  354. let newRing = player.ring;
  355. let newSector = player.sector;
  356.  
  357. switch (direction) {
  358. case 'up': // Move inward
  359. newRing = player.ring - 1;
  360. break;
  361. case 'down': // Move outward
  362. newRing = player.ring + 1;
  363. break;
  364. case 'left': // Move counter-clockwise
  365. newSector = player.sector - 1;
  366. break;
  367. case 'right': // Move clockwise
  368. newSector = player.sector + 1;
  369. break;
  370. }
  371.  
  372. if (canMove(player.ring, player.sector, newRing, newSector)) {
  373. player.ring = newRing;
  374. player.sector = (newSector + SECTORS) % SECTORS;
  375.  
  376. // Check if reached goal
  377. if (player.ring === goal.ring && player.sector === goal.sector) {
  378. gameWon = true;
  379. statusDiv.textContent = '🎉 Congratulations! You reached the goal! 🎉';
  380. }
  381.  
  382. render();
  383. }
  384. }
  385.  
  386. function render() {
  387. drawMaze();
  388. drawEntranceAndGoal();
  389. drawPlayer();
  390. }
  391.  
  392. function generateNewMaze() {
  393. generateMaze();
  394. setupGame();
  395. render();
  396. }
  397.  
  398. // Keyboard controls
  399. document.addEventListener('keydown', (e) => {
  400. switch (e.key.toLowerCase()) {
  401. case 'w':
  402. case 'arrowup':
  403. e.preventDefault();
  404. movePlayer('up');
  405. break;
  406. case 's':
  407. case 'arrowdown':
  408. e.preventDefault();
  409. movePlayer('down');
  410. break;
  411. case 'a':
  412. case 'arrowleft':
  413. e.preventDefault();
  414. movePlayer('left');
  415. break;
  416. case 'd':
  417. case 'arrowright':
  418. e.preventDefault();
  419. movePlayer('right');
  420. break;
  421. }
  422. });
  423.  
  424. // Make canvas focusable for keyboard events
  425. canvas.setAttribute('tabindex', '0');
  426. canvas.focus();
  427.  
  428. // Initialize the game
  429. generateNewMaze();
  430. </script>
  431. </body>
  432. </html>
Advertisement
Add Comment
Please, Sign In to add comment