From cc84e4eaec11580fcaf0b70a7782566917ed286e Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Fri, 11 Jan 2019 13:34:49 -0500 Subject: Initial implementation of flagging. --- app/src/main/java/space/jakob/mines/Grid.kt | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'app/src/main/java/space/jakob/mines/Grid.kt') diff --git a/app/src/main/java/space/jakob/mines/Grid.kt b/app/src/main/java/space/jakob/mines/Grid.kt index 2eec411..963f8cb 100644 --- a/app/src/main/java/space/jakob/mines/Grid.kt +++ b/app/src/main/java/space/jakob/mines/Grid.kt @@ -14,7 +14,12 @@ package space.jakob.mines import java.util.Random -data class Tile(val adjacentMines: Int = 0, val mine: Boolean = false, val masked: Boolean = true) +data class Tile( + val adjacentMines: Int = 0, + val mine: Boolean = false, + val masked: Boolean = true, + val flagged: Boolean = false +) /** * The Minesweeper "grid", containing instances of [Tile]. @@ -118,6 +123,23 @@ class Grid(val width: Int, val height: Int, val tiles: Array) { } } + /** + * Mark the tile at the given coordinates as flagged. + * + * @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 flag(x: Int, y: Int) { + if (!valid(x, y) || !this[x, y].masked) { + return; + } + + with (this[x, y]) { + place(x, y, copy(flagged = true)) + } + } + override fun toString(): String = buildString { for (y in 0 until height) { for (x in 0 until width) { -- cgit v1.3