+ 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
4 odpowiedzi
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