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

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/minesweeper/minesweeper.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/minesweeper/minesweeper.example.ts