Sha256: 8e7ac19dbbe1dc1964b62a98f56c6bc1afb45f8575d9366e0ef4c1fb15c2c204

Contents?: true

Size: 1.91 KB

Versions: 188

Compression:

Stored size: 1.91 KB

Contents

private const val MINE_CHAR = '*'
private const val SPACE_CHAR = ' '

data class MinesweeperBoard(val mineLocations: List<String>) {

    private val numberOfRows by lazy {
        mineLocations.size
    }

    private val numberOfColumns by lazy {
        if (mineLocations.isEmpty()) 0 else mineLocations[0].length
    }

    fun withNumbers() = (0 until numberOfRows).map { getRowWithNumbers(it) }

    private fun getRowWithNumbers(rowNumber: Int) =
            (0 until numberOfColumns)
                    .map { columnNumber -> getCellNumber(rowNumber, columnNumber) }
                    .joinToString("")

    private fun getCellNumber(rowNumber: Int, columnNumber: Int): Char {
        // If (rowNumber, columnNumber) is a mine, we're done.
        if (mineLocations[rowNumber][columnNumber] == MINE_CHAR) {
            return MINE_CHAR
        }

        val mineCount = computeMineCountAround(rowNumber, columnNumber)

        // If computed count is positive, add it to the annotated row. Otherwise, add a blank space.
        return if (mineCount > 0) Character.forDigit(mineCount, 10) else SPACE_CHAR
    }

    private fun computeMineCountAround(rowNumber: Int, columnNumber: Int): Int {
        var result = 0

        // Compute row and column ranges to inspect (respecting board edges).
        val minRowToInspect = Math.max(rowNumber - 1, 0)
        val maxRowToInspect = Math.min(rowNumber + 1, numberOfRows - 1)
        val minColToInspect = Math.max(columnNumber - 1, 0)
        val maxColToInspect = Math.min(columnNumber + 1, numberOfColumns - 1)

        // Count mines in the cells surrounding (row, col).
        for (rowToInspect in minRowToInspect..maxRowToInspect) {
            for (colToInspect in minColToInspect..maxColToInspect) {
                if (mineLocations[rowToInspect][colToInspect] == MINE_CHAR) {
                    result += 1
                }
            }
        }

        return result
    }

}

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.159 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.158 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.157 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.156 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.155 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.154 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.153 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.152 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.151 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.150 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.149 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.148 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.147 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.146 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.145 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.144 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.143 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.142 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.141 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt
trackler-2.2.1.140 tracks/kotlin/exercises/minesweeper/.meta/src/reference/kotlin/MinesweeperBoard.kt