mwg API
    Preparing search index...

    Class BoardGrid<P>

    A generic rectangular grid of pieces - the shared shape underneath a game-specific board (chess, checkers, or a custom game entirely) that only needs cells, bounds checking, and moving a piece from one cell to another.

    import { BoardGrid } from '@datamoc/mw_games/board';

    const board = new BoardGrid<{ id: string; owner: string; kind: string }>(3, 3);
    board.set(1, 1, { id: 'king', owner: 'white', kind: 'king' });

    console.log(board.move({ x: 1, y: 1 }, { x: 2, y: 2 })); // { id: 'king', owner: 'white', kind: 'king' }
    console.log(board.get(2, 2)); // the same piece, now at its new cell
    console.log(board.get(1, 1)); // null - the old cell is empty

    Type Parameters

    • P
    Index
    • Type Parameters

      • P

      Parameters

      • width: number
      • height: number
      • Optionalcells: readonly (P | null)[]

      Returns BoardGrid<P>

    cells: (P | null)[]
    height: number
    width: number
    • Parameters

      • x: number
      • y: number

      Returns boolean

    • Parameters

      • from: { x: number; y: number }
      • to: { x: number; y: number }

      Returns P

    • Parameters

      • x: number
      • y: number
      • piece: P | null

      Returns void