Sha256: 0808b19c5c32bf20abd5ff897ce223c03f85d96e9bf642a9c72d8b2303695ad0
Contents?: true
Size: 1.35 KB
Versions: 49
Compression:
Stored size: 1.35 KB
Contents
const MINE = '*' const DELTAS = [ [-1, -1], [-1, 0], [-1, 1], [1, 1], [1, 0], [1, -1], [0, 1], [0, -1], ] class Minesweeper { annotate(rows: string[]) { if (noDataPresent(rows)) { return rows } const inputBoard = rows.map((row) => [...row]) const result = inputBoard.map((row, x) => [...row].map((cell, y) => cellToMineOrCount(cell, inputBoard, x, y))) return stringify(result as string[][]) } } function cellToMineOrCount(cell: string, inputBoard: string[][], x: number, y: number) { if (cell === MINE) { return MINE } return countAdjacentMines(inputBoard, x, y) || " " } function countAdjacentMines(board: string[][], x: number, y: number) { return DELTAS .filter((d) => adjacentSquareIsOnBoard(board, x, d)) .filter((d) => adjacentSquareHasMine(board, x, y, d)) .length } function stringify(board: string[][]) { return board.map((row) => row.join('')) } function noDataPresent(rows: string[]) { return rows.length === 0 || rows[0].length === 0 } function adjacentSquareIsOnBoard(board: string[][], x: number, d: number[]) { return board[x + d[0]] } function adjacentSquareHasMine(board: string[][], x: number, y: number, d: number[]) { return board[x + d[0]][y + d[1]] === MINE } export default Minesweeper
Version data entries
49 entries across 49 versions & 1 rubygems