Cómo funciona
Una figura de libro de texto sobre caminos más cortos en una cuadrícula. Los obstáculos son celdas sólidas; S y G son el inicio y la meta. Tras cada cambio la búsqueda A* vuelve a ejecutarse: las celdas examinadas aparecen en el orden en que las alcanzó y después se dibuja el camino más corto con el color de acento. Con 4 direcciones la longitud es el número de pasos; con 8, un paso diagonal cuenta √2 y no puede cortar la esquina de un obstáculo.
Úsalo para
- Teach graph search: how a shortest path bends around obstacles, how its length changes, and how many cells the search had to examine.
- Compare 4- and 8-direction movement and see why a diagonal step costs √2.
Busca otra opción cuando
- For routes on a real map, use a map with roads: this grid has uniform cells only.
Uso
import React, { useState } from 'react'
import { KeepoutWireRouter } from './KeepoutWireRouter.jsx'
const start = { start: [1, 5], goal: [18, 5], walls: [[9, 3], [9, 4], [9, 5], [9, 6]] }
export function PathLesson() {
const [grid, setGrid] = useState(start)
const [length, setLength] = useState(null)
return (
<KeepoutWireRouter
value={grid}
onChange={(next, details) => {
setGrid(next)
setLength(details.path?.length ?? null)
}}
defaultMoves={4}
/>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
value | { start: [x, y], goal: [x, y], walls: Array<[x, y]> } | — | Controlled grid: the start and goal cells and the obstacle cells, as zero-based column and row. |
defaultValue | { start, goal, walls } | two walls, S at [2, 4], G at [17, 3] | Starting grid when uncontrolled. |
onChange | (value, { reason, path }) => void | — | Called after every edit. reason is 'paint', 'drag', 'key', 'clear' or 'cancel'; path is null when G cannot be reached, otherwise { cells, length, straight, diagonal, examined }. |
moves | 4 | 8 | — | Controlled move set: 4 directions, or 8 with diagonal steps of length √2 that may not cut an obstacle's corner. |
defaultMoves | 4 | 8 | 8 | Starting move set when uncontrolled. |
onMovesChange | (moves) => void | — | Called when the move set is changed. |
columns | number | 20 | Number of columns (4–40). |
rows | number | 9 | Number of rows (3–24). |
labels | object | — | Replaces any built-in text: title, formula4, formula8, legendWall, legendExplored, legendPath, grid, help, keys, moves, moves4, moves8, clear, startMark, goalMark, readout, noPath, straight, diagonal, explored, cell ('{x}', '{y}'), wallOn, wallOff, startAt, goalAt ('{cell}'), result ('{length}', '{explored}') and blocked. |
locale | string | 'en' | Language tag for the numbers. |
className | string | — | Extra class on the card, for example to set its width (30rem by default, never wider than its container). |
Accesibilidad
- The grid is an application region with its instructions as its description. A cell cursor moves with the arrows (Home and End go to the row's ends); Space or Enter toggles an obstacle; S and G put the start or the goal on the cursor. A hint with these keys shows while the grid has keyboard focus.
- A polite status announces each change with the new path length and the number of cells examined, or that there is no path.
- The move set is a radio group: arrows switch between 4 and 8 directions. Clear is a button, marked unavailable when there are no obstacles.
- Painting uses pointer capture and touch-action none on the grid only; a cancelled gesture restores the grid it started from.
Movimiento
After an edit the examined cells appear in search order within 420ms and the path fades in for --ds-duration-base; while painting, everything updates at once. With reduced motion there is no animation.
Añádelo a tu proyecto
Los miembros Pro copian un prompt y su agente de programación instala el código, igual que con los gratuitos.
KeepoutWireRouter.jsxkeepout-wire-router.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.







