Visually distinguish start, end, and obstacle nodes using different background colors or styles.
Include a "Find Path" button. When clicked, this should trigger a basic pathfinding algorithm (like Breadth-First Search - BFS) to find a path from the start to the end node, avoiding obstacles.
During or after the algorithm runs, visually highlight the cells explored by the algorithm or the final found path on the grid. If no path is found, indicate this visually (e.g., message, color change).
Include a "Reset Grid" button that clears all start, end, obstacle, and path highlights.
Optionally include the Tailwind CSS CDN link for styling the grid container, cells, and buttons, but the core grid display, interaction, and pathfinding visualization logic must work with basic or embedded custom CSS. External JavaScript libraries via CDN are allowed if they simplify grid rendering or interaction setup, but the pathfinding algorithm itself should be implemented directly in the <script> tag.
All HTML structure for the grid and controls, necessary CSS for layout and cell states (custom or via optional Tailwind), and the comprehensive JavaScript logic for grid state management, user interaction handling, implementing the pathfinding algorithm, and updating the grid visualization must be entirely self-contained within this single index.html file. This is harder as it involves complex data structure manipulation (the grid), implementing an algorithm, and synchronizing algorithm state with dynamic UI updates.
Assistant
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Simple Path‑Finding Visualizer</title>
<!-- ↓ Optional Tailwind CDN for prettier buttons & layout ↓ -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Core grid & cell styles (work even without Tailwind) */
#grid {
display: grid;
grid-template-columns: repeat(25, 24px); /* 25 × 25 grid */
grid-template-rows: repeat(25, 24px);
gap: 1px;
margin: 1rem auto;
width: max-content;
user-select: none;
}
.cell {
width: 24px;
height: 24px;
border: 1px solid #e5e7eb; /* light gray */
background: white;
transition: background 0.2s;
}
.start { background: #22c55e; } /* green */
.end { background: #ef4444; } /* red */
.wall { background: #6b7280; } /* gray */
.visited { background: #fde047; } /* yellow */
.path { background: #3b82f6; } /* blue */
</style>
</head>
<body class="bg-gray-100 flex flex-col items-center min-h-screen py-6">
<h1 class="text-2xl font-bold mb-4">Grid Path‑Finding Visualizer (BFS)</h1>
<!-- Buttons -->
<div class="space-x-4 mb-4">
<button id="findBtn"
class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 disabled:opacity-50">
Find Path
</button>
<button id="resetBtn"
class="px-4 py-2 bg-gray-600 text-white rounded hover:bg-gray-700">
Reset Grid
</button>
</div>
<!-- Grid container -->
<div id="grid"></div>
<!-- Status / message -->
<p id="msg" class="mt-4 text-lg font-medium"></p>
<script>
/* =============== GRID SETUP =============== */
const SIZE = 25; // 25×25 grid
const gridEl = document.getElementById("grid");
const msgEl = document.getElementById("msg");
const findBtn = document.getElementById("findBtn");
const resetBtn = document.getElementById("resetBtn");
// State matrices
const cells = []; // DOM elements
const walls = makeMatrix(false);
const visited = makeMatrix(false);
const parent = makeMatrix(null);
let start = null; // {r,c}
let end = null; // {r,c}
// Build the grid DOM & attach click handlers
for (let r = 0; r < SIZE; r++) {
cells[r] = [];
for (let c = 0; c < SIZE; c++) {
const div = document.createElement("div");
div.classList.add("cell");
div.dataset.r = r;
div.dataset.c = c;
div.addEventListener("click", handleCellClick);
gridEl.appendChild(div);
cells[r][c] = div;
}
}
/* =============== EVENT HANDLERS =============== */
function handleCellClick(e) {
const r = +e.target.dataset.r;
const c = +e.target.dataset.c;
if (!start) {
start = {r, c};
e.target.classList.add("start");
} else if (!end && !(r === start.r && c === start.c)) {
end = {r, c};
e.target.classList.add("end");
} else if (!(r === start?.r && c === start?.c) &&
!(r === end?.r && c === end?.c)) {
// Toggle wall
walls[r][c] = !walls[r][c];
e.target.classList.toggle("wall");
}
}
/* =============== BUTTONS =============== */
findBtn.addEventListener("click", () => {
if (!start || !end) {
alert("Please select a start and end node first!");
return;
}
findBtn.disabled = true;
resetBtn.disabled = true;
bfs().then(() => {
findBtn.disabled = false;
resetBtn.disabled = false;
});
});
resetBtn.addEventListener("click", resetGrid);
/* =============== BFS PATH‑FINDING =============== */
async function bfs() {
clearVisualization();
msgEl.textContent = "Searching…";
const q = [start];
visited[start.r][start.c] = true;
const dirs = [ [1,0],[0,1],[-1,0],[0,-1] ]; // 4-neigh
while (q.length) {
const cur = q.shift();
const {r, c} = cur;
// Reached end!
if (r === end.r && c === end.c) {
await drawPath();
msgEl.textContent = "Path found ✅";
return;
}
for (const [dr, dc] of dirs) {
const nr = r + dr, nc = c + dc;
if (inside(nr, nc) && !walls[nr][nc] && !visited[nr][nc]) {
visited[nr][nc] = true;
parent[nr][nc] = {r, c};
q.push({r: nr, c: nc});
markVisited(nr, nc);
await sleep(15); // animation delay
}
}
}
msgEl.textContent = "No path found ❌";
}
async function drawPath() {
let cur = end;
const path = [];
while (cur && !(cur.r === start.r && cur.c === start.c)) {
path.push(cur);
cur = parent[cur.r][cur.c];
}
for (let i = path.length - 1; i >= 0; i--) {
const {r, c} = path[i];
cells[r][c].classList.remove("visited");
cells[r][c].classList.add("path");
await sleep(30);
}
}
/* =============== HELPERS =============== */
function markVisited(r, c) {
if (!(r === start.r && c === start.c) &&
!(r === end.r && c === end.c))
cells[r][c].classList.add("visited");
}
function makeMatrix(fill) {
return Array.from({length: SIZE}, () => Array(SIZE).fill(fill));
}
function inside(r, c) {
return r >= 0 && c >= 0 && r < SIZE && c < SIZE;
}
function sleep(ms) {
return new Promise(res => setTimeout(res, ms));
}
function clearVisualization() {
for (let r = 0; r < SIZE; r++)
for (let c = 0; c < SIZE; c++) {
visited[r][c] = false;
parent[r][c] = null;
cells[r][c].classList.remove("visited","path");
}
}
function resetGrid() {
// reset matrices
start = end = null;
msgEl.textContent = "";
for (let r = 0; r < SIZE; r++)
for (let c = 0; c < SIZE; c++) {
walls[r][c] = visited[r][c] = false;
parent[r][c] = null;
cells[r][c].className = "cell";
}
}
</script>
</body>
</html>