/** @file The mower: Logo-turtle moves on a yard of cells. */ /** what a model is told about a mower: its type, with the moves explained */ export const aboutMower = { ts: `interface Mower { /** put the mower down, facing north, and cut the cell under it */ start(): Promise; /** move forward one cell (10 yards), cutting the cell it enters */ push(): Promise; /** turn right 90 degrees, in place */ turn(): Promise; /** park the mower */ stop(): Promise; }`, }; const headings = [ [0, -1], // north [1, 0], // east [0, 1], // south [-1, 0], // west ] as const; /** a mower on a `width` × `height` yard, starting at `at`; pushing past an * edge wraps to the other side (6d) */ export const makeMower = ({ width, height, at }: { width: number; height: number; at: { x: number; y: number } }) => { let { x, y } = at; let heading = 0; const cut = new Set(); const cutHere = () => cut.add(`${x},${y}`); return { start() { heading = 0; cutHere(); }, push() { const [dx, dy] = headings[heading]; x = (x + dx + width) % width; y = (y + dy + height) % height; cutHere(); }, turn() { heading = (heading + 1) % headings.length; }, stop() {}, cutCells: () => [...cut], }; }; export type Mower = ReturnType;