Maze generator & solver · nex-n2-mini
Generate a random maze with the recursive-backtracker algorithm on a canvas, then animate an A-star or BFS solver finding the path from start to finish.
transcript · 6 messages
Generate a random maze with the recursive-backtracker algorithm on a canvas, then animate an A-star or BFS solver finding the path from start to finish.
thinking
We need respond by creating index.html in current dir /tmp/ws. Need build single-page web app self-contained no external resources. Need use tools. We need inspect? likely just write file. Need ensure canvas, maze generation recursive backtracker, animate solver BFS/A*. Single page. Need include CSS/JS inline. Need end with index.html present. We can create robust app. Need perhaps run ls? Not necessary but maybe. Need write index.html. Need think details.
We need produce final concise with path and mention. But first create file. Need maybe use write. Let's craft HTML.
Requirements:
- Generate random maze with recursive-backtracker algorithm on a canvas.
- Animate A-star or BFS solver finding path from start to finish.
- Single file index.html in current directory.
- No external resources.
- Canvas.
Implement:
HTML: canvas, controls buttons (Generate, Solve), sliders? maybe grid size.
JS:
const canvas, ctx, DPR handling.
Grid: cols, rows, cell size.
Maze generation:
Use stack-based recursive backtracker:
create grid cells {x,y,w,h, walls:{N:E:S:W}, visited:false}
choose start 0,0 mark visited.
while stack not empty:
current top
unvisited neighbors (up/down/left/right within bounds and not visited)
if any choose random, remove wall between, mark visited, push
else pop.
This is iterative recursive backtracker.
Draw:
clear black background? white walls. maybe cell bg #0e1116, walls #f8fafc, start green, end red.
Solving:
Use BFS or A*. BFS easier. Need animate path from start to finish.
Use queue, parent map, visited set, frontiers. BFS explores. To animate, maybe set interval / requestAnimationFrame. Need visualize:
- visited cells light blue fill
- frontier yellow maybe.
- path final white/gold line.
We can animate step-by-step with requestAnimationFrame; each frame one or multiple expansions depending speed.
Need ensure maze generated first. Start/finish maybe (0,0) and (cols-1, rows-1). BFS parent.
Algorithm:
function solve() {
const start = {x:0,y:0}, goal={x:cols-1,y:rows-1}
queue=[start], visited=new Set(), parent=new Map()
visited.add(key)
let solved=false
function step() {
if solved return
while framesPerStep and queue length:
cur=queue.shift()
if equal goal: solved=true; path=trace; draw path; return
for neighbors valid and not visited:
visited.add(key); parent.set(key,key(cur)); queue.push(n)
drawVisited(); if not solved drawFrontier? Need frontier can be queue slice. But queue includes many; maybe draw current and frontier.
if queue.length > 0 requestAnimationFrame(step) else solved=false draw no path.
}
Need drawVisited each frame maybe not too heavy.
Need drawFrontier: queue cells in yellow. But queue can be huge; okay.
Need path draw: after solved, maybe animate path? Requirement animate solver finding path. Could also animate final path by drawing segments. But BFS animation enough.
Maybe include "Solve" button and "Generate" button.
Canvas scaling:
CSS width: min(96vw, 900px); height auto? Canvas has fixed internal size based on cols*cell. Use resize? Since no external. Use canvas.width=cols*cell*DPR; style width=...; ctx.setTransform(DPR,0,0,DPR,0,0). Simpler: canvas.width = cols*cell; canvas.height=rows*cell; CSS width: min(96vw, 1000px); but if cell changes, style maybe. We can set canvas.style.width = `${cols*cell}px`; height. On resize? Use CSS max-width, but actual canvas pixel size can be lower/higher. Better use fixed grid 45x31, cell 16 => 720x496. Fine. But responsive? Could use CSS width:100%; height:auto, but drawing coordinates in CSS pixels? Canvas width attr 720, CSS width 100%; if CSS display scaled, draw scaled by browser okay. For crisp maybe not. Good.
Need maybe random maze with recursive backtracker algorithm. Use `generateMaze()`.
Need maybe "Start" and "Finish" labels.
Potential issue no external resources. OK.
Let's write file.
/tmp/ws/index.html (12134 bytes)Successfully wrote 12134 bytes to /tmp/ws/index.html
thinking
Need maybe verify file present. Use ls maybe.
{"command":"ls -l /tmp/ws/index.html && wc -c /tmp/ws/index.html","timeout":5}-rw-r--r--. 1 node node 12134 Aug 1 20:20 /tmp/ws/index.html 12134 /tmp/ws/index.html
thinking
Created `/tmp/ws/index.html`. It includes a self-contained single-page canvas app with: - Recursive backtracker maze generation - Animated BFS solver - Start/finish markers - Visited/frontier/path visualization - Inline CSS and JS only, no external resources
TAKE IT TO CODE
A single self-contained HTML file — copy it and open in any browser, or reuse the prompt with your own pi-agent run.
RUN
EVALUATION
Canvas never renders a maze; only a blank dark area with title and legend show, JS error crashes generation.
Vision grade · anthropic/claude-sonnet-5
MEASUREMENTS
Raw readings from the capture, not a verdict — the grade above is the only judgement. Percentages are how much of the screen changed; the measure under-reports sparse motion on a dark background.
- Cannot set properties of undefined (setting 'N')