summaryrefslogtreecommitdiff
path: root/mines.kt
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2018-12-15 09:21:53 -0500
committerJakob L. Kreuze <jakob@memeware.net>2018-12-15 09:21:53 -0500
commit15b4a3158bbb501aef542cba3bf1e429d2d594b1 (patch)
treea9b7535eac86ff855e847666f81a087272ea0181 /mines.kt
parent76fd5eaa9707ed423d442b594ec31997901ce2e6 (diff)
Turn into proper Android project.
Diffstat (limited to 'mines.kt')
-rw-r--r--mines.kt124
1 files changed, 0 insertions, 124 deletions
diff --git a/mines.kt b/mines.kt
deleted file mode 100644
index ae8415d..0000000
--- a/mines.kt
+++ /dev/null
@@ -1,124 +0,0 @@
-data class Tile(val adjacentMines: Int = 0, val mine: Boolean = false, val masked: Boolean = true)
-
-/**
- * The Minesweeper "grid", containing instances of [Tile].
- */
-class Grid(val width: Int, val height: Int, val tiles: Array<Tile>) {
- constructor(width: Int = 8, height: Int = 8, mines: Int = 10)
- : this(width, height, Array<Tile>(width * height) { Tile() }) {
- for (i in 0 until mines) {
- val x = (0 until width).random()
- val y = (0 until height).random()
- placeMine(x, y)
- }
- }
-
- private fun index(x: Int, y: Int) = y * width + x
- private fun valid(x: Int, y: Int) = y in 0 until height && x in 0 until width
- private fun place(x: Int, y: Int, tile: Tile) {
- tiles[index(x, y)] = tile
- }
-
- /**
- * Returns the tile at the given coordinates.
- *
- * @throws IllegalArgumentException if the X coordinate is outside
- * the range of [0, width), or if the Y coordinate is outside the
- * range of [0, height).
- */
- operator fun get(x: Int, y: Int) = if (valid(x, y)) {
- tiles[index(x, y)]
- } else {
- throw IllegalArgumentException("Invalid coordinates (${x}, ${y})")
- }
-
- /**
- * Places a mine at the given coordinates.
- *
- * @throws IllegalArgumentException if the X coordinate is outside
- * the range of [0, width), or if the Y coordinate is outside the
- * range of [0, height).
- */
- fun placeMine(x: Int, y: Int) {
- if (!valid(x, y)) {
- throw IllegalArgumentException("Invalid coordinates (${x}, ${y})")
- }
-
- if (this[x, y].mine) {
- return;
- }
-
- place(x, y, Tile(mine = true))
-
- val xMin = (x - 1).coerceAtLeast(0)
- val xMax = (x + 1).coerceAtMost(width - 1)
- val yMin = (y - 1).coerceAtLeast(0)
- val yMax = (y + 1).coerceAtMost(height - 1)
-
- for (y in yMin..yMax) {
- for (x in xMin..xMax) {
- with (tiles[index(x, y)]) {
- if (!mine) {
- place(x, y, copy(adjacentMines + 1))
- }
- }
- }
- }
- }
-
- /**
- * Reveals a tile at the given coordinates according to the game rules.
- *
- * @throws IllegalArgumentException if the X coordinate is outside
- * the range of [0, width), or if the Y coordinate is outside the
- * range of [0, height).
- */
- fun reveal(x: Int, y: Int) {
- if (!valid(x, y) || !this[x, y].masked) {
- return;
- }
-
- with (this[x, y]) {
- place(x, y, copy(masked = false))
-
- if (!mine && adjacentMines == 0) {
- val xMin = (x - 1).coerceAtLeast(0)
- val xMax = (x + 1).coerceAtMost(width - 1)
- val yMin = (y - 1).coerceAtLeast(0)
- val yMax = (y + 1).coerceAtMost(height - 1)
-
- for (y in yMin..yMax) {
- for (x in xMin..xMax) {
- reveal(x, y)
- }
- }
- }
- }
- }
-
- override fun toString(): String = buildString {
- for (y in 0 until height) {
- for (x in 0 until width) {
- val tile = this@Grid[x, y]
- append(when {
- tile.masked -> "."
- tile.mine -> "M"
- tile.adjacentMines == 0 -> " "
- else -> tile.adjacentMines.toString()
- } + " ")
- }
- append("\n")
- }
- }
-}
-
-fun main(args: Array<String>) {
- val grid = Grid()
-
- for (i in 0 until 9) {
- val x = (0 until 8).random()
- val y = (0 until 8).random()
- grid.reveal(x, y)
- println(grid)
- }
-}