<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>BFS Pathfinding Visualization</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
#grid {
display: grid;
grid-template-columns: repeat(10, 40px);
grid-template-rows: repeat(10, 40px);
gap: 2px;
justify-content: center;
margin-top: 20px;
}
.cell {
width: 40px;
height: 40px;
background-color: #eee;
border: 1px solid #ccc;
}
.player {
background-color: blue;
}
.coin {
background-color: gold;
}
.visited {
background-color: lightgreen;
}
.path {
background-color: green;
}
#controls {
margin-top: 20px;
}
button {
margin: 5px;
padding: 10px 15px;
font-size: 16px;
}
</style>
</head>
<body>
<h1>BFS Pathfinding Visualization</h1>
<div id="grid"></div>
<div id="controls">
<button id="respawn">Respawn Player and Coin</button>
<button id="start">Start BFS</button>
</div>
<script>
const gridSize = 10;
const gridElement = document.getElementById('grid');
let grid = [];
let playerPosition = { x: 0, y: 0 };
let coinPosition = { x: gridSize - 1, y: gridSize - 1 };
let cells = [];
function createGrid() {
gridElement.innerHTML = '';
grid = [];
cells = [];
for (let y = 0; y < gridSize; y++) {
const row = [];
for (let x = 0; x < gridSize; x++) {
const cell = document.createElement('div');
cell.classList.add('cell');
gridElement.appendChild(cell);
row.push(cell);
}
cells.push(row);
}
}
function respawn() {
// Random positions for player and coin
playerPosition = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
do {
coinPosition = {
x: Math.floor(Math.random() * gridSize),
y: Math.floor(Math.random() * gridSize)
};
} while (playerPosition.x === coinPosition.x && playerPosition.y === coinPosition.y);
// Reset the grid
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
cells[y][x].className = 'cell';
}
}
// Set player and coin positions
cells[playerPosition.y][playerPosition.x].classList.add('player');
cells[coinPosition.y][coinPosition.x].classList.add('coin');
}
function startBFS() {
const queue = [];
const visited = [];
for (let y = 0; y < gridSize; y++) {
visited[y] = [];
for (let x = 0; x < gridSize; x++) {
visited[y][x] = false;
}
}
const parent = [];
for (let y = 0; y < gridSize; y++) {
parent[y] = [];
for (let x = 0; x < gridSize; x++) {
parent[y][x] = null;
}
}
queue.push(playerPosition);
visited[playerPosition.y][playerPosition.x] = true;
function bfsStep() {
if (queue.length === 0) {
return;
}
const current = queue.shift();
const x = current.x;
const y = current.y;
if (x === coinPosition.x && y === coinPosition.y) {
// Found the coin, trace back the path
let path = [];
let curr = current;
while (curr) {
if (!(curr.x === playerPosition.x && curr.y === playerPosition.y) &&
!(curr.x === coinPosition.x && curr.y === coinPosition.y)) {
cells[curr.y][curr.x].classList.add('path');
}
path.push(curr);
curr = parent[curr.y][curr.x];
}
return; // Path found
}
const directions = [
{ x: 0, y: -1 }, // Up
{ x: 1, y: 0 }, // Right
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 } // Left
];
for (let dir of directions) {
const newX = x + dir.x;
const newY = y + dir.y;
if (newX >= 0 && newX < gridSize && newY >= 0 && newY < gridSize &&
!visited[newY][newX]) {
visited[newY][newX] = true;
parent[newY][newX] = current;
queue.push({ x: newX, y: newY });
if (!(newX === coinPosition.x && newY === coinPosition.y)) {
cells[newY][newX].classList.add('visited');
}
}
}
// Continue BFS after a short delay
setTimeout(bfsStep, 50);
}
bfsStep();
}
document.getElementById('respawn').addEventListener('click', respawn);
document.getElementById('start').addEventListener('click', startBFS);
createGrid();
respawn();
</script>
</body>
</html>
This code creates a 10x10 grid and places the player (blue square) and coin (gold square) in random positions. When you click "Start BFS", it initiates the Breadth-First Search algorithm, visualizing each step as the player searches for the coin. The search process highlights visited cells in light green and the shortest path in green once the coin is found.
Features:
- Respawn Button: Randomizes the positions of the player and the coin.
- Start BFS Button: Begins the BFS algorithm visualization.
- Visual Representation: Shows the grid, player, coin, visited cells, and the path found.
How It Works:
-
Grid Creation: The grid is generated using a nested loop that creates div elements styled to look like cells.
-
Player and Coin Positioning: The player and coin are placed at random positions on the grid, ensuring they do not overlap.
-
BFS Algorithm:
- A queue is used to keep track of cells to visit.
- The
visited array records which cells have been explored.
- The
parent array keeps track of the path.
- The algorithm explores neighboring cells (up, right, down, left) and uses
setTimeout to create a delay between steps for visualization.
-
Visualization:
- As cells are visited, they change color to light green.
- Once the coin is found, the shortest path is traced back using the
parent array and highlighted in green.
Instructions to Use:
- Step 1: Copy the entire code into a text editor and save it as
bfs_visualization.html.
- Step 2: Open the
bfs_visualization.html file in a modern web browser (e.g., Chrome, Firefox).
- Step 3: Click "Respawn Player and Coin" to randomize their positions.
- Step 4: Click "Start BFS" to begin the visualization of the BFS algorithm.
Feel free to customize the grid size, colors, and delays according to your preferences. This visualization should help you understand how BFS works in finding the shortest path in a grid-like structure.