+ 2

Algorithm to create maze-like grid

Read the comments(in javascript tab) in the code attached I am to create a grid of blocks which are randomly generated but follow some principles specified in the comments each time the code is run https://sololearn.com/compiler-playground/WGprXqqcwYIw/?ref=app

18th Jun 2025, 1:38 PM
Vaibhav
Vaibhav - avatar
7 Antworten
19th Jun 2025, 1:57 PM
Bob_Li
Bob_Li - avatar
+ 2
The kind of grid I wanted was this but the maze code made me understand something important, thanks https://sololearn.com/compiler-playground/W9MW3JrsoVeI/?ref=app
20th Jun 2025, 1:07 PM
Vaibhav
Vaibhav - avatar
0
Replace This Part: for (let a = 0; a < grid.length; a++){ grid[a] = Math.floor(Math.random()*2)+1 } Try this, it might help you: // Step 1: Create full outline grid = new Array(64).fill(1); // 8x8 all 1s // Step 2: Pick random empty points on each side let topGap = Math.floor(Math.random() * 6) + 1; // column 1–6 (row 0) let bottomGap = Math.floor(Math.random() * 6) + 1; // column 1–6 (row 7) let leftGap = Math.floor(Math.random() * 6) + 1; // row 1–6 (column 0) let rightGap = Math.floor(Math.random() * 6) + 1; // row 1–6 (column 7) // Step 3: Apply gaps to grid (set to 0) grid[topGap] = 0; // Top row grid[56 + bottomGap] = 0; // Bottom row grid[leftGap * 8] = 0; // Left column grid[rightGap * 8 + 7] = 0; // Right column // Optional: Fill rest with some randomness inside the inner area for (let row = 1; row < 7; row++) { for (let col = 1; col < 7; col++) { let idx = row * 8 + col; grid[idx] = Math.random() < 0.4 ? 1 : 0; // 40% filled } }
18th Jun 2025, 3:38 PM
Lime
Lime - avatar
0
Lime, the code snippet you provided doesn't satisfy all the objectives. It only creates an opening on each side, which is inaccessible by top-down movement
18th Jun 2025, 3:59 PM
Vaibhav
Vaibhav - avatar
0
Can you tell me idea or expected output in detail
18th Jun 2025, 4:19 PM
Lime
Lime - avatar
0
All the openings should be accessible from any one of them in a top-down movement. Replace the grid array with the example from line 110 and run the code to see an expected output
18th Jun 2025, 4:33 PM
Vaibhav
Vaibhav - avatar
0
Vaibhav nice game. you don't really need a maze algorithm. randomized positioning (or 2d array maps if you want consistent level design) is all that is necessary. hard to control the movement on phone screen, though. would be nice if there are up-down-left-right buttons around the bomb button, so all controls are in the same place. ^ < o > v the player position also seems to drift a bit in my phone. perhaps it's detecting random touches. then it's easy to get stuck in corners when running away. that's another reason to limit the controls to buttons instead of making the entire screen touch sensitive.
20th Jun 2025, 3:13 PM
Bob_Li
Bob_Li - avatar