summaryrefslogtreecommitdiff
path: root/app/src/main/java/space/jakob/mines
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2019-01-11 13:34:49 -0500
committerJakob L. Kreuze <jakob@memeware.net>2019-01-11 13:34:49 -0500
commitcc84e4eaec11580fcaf0b70a7782566917ed286e (patch)
tree0a2ba0544260ccec48d5849c268c57a1ec6d6459 /app/src/main/java/space/jakob/mines
parentd321bb13a9a2ea7af10400b3ce3848733039cbd4 (diff)
Initial implementation of flagging.
Diffstat (limited to 'app/src/main/java/space/jakob/mines')
-rw-r--r--app/src/main/java/space/jakob/mines/GameActivity.kt7
-rw-r--r--app/src/main/java/space/jakob/mines/GameView.kt18
-rw-r--r--app/src/main/java/space/jakob/mines/Grid.kt24
3 files changed, 47 insertions, 2 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameActivity.kt b/app/src/main/java/space/jakob/mines/GameActivity.kt
index b6135b3..5a44720 100644
--- a/app/src/main/java/space/jakob/mines/GameActivity.kt
+++ b/app/src/main/java/space/jakob/mines/GameActivity.kt
@@ -42,6 +42,13 @@ class GameActivity : AppCompatActivity() {
}
return false
}
+
+ override fun onLongPress(e: MotionEvent) {
+ with (gameView) {
+ flagAt(e.x.toInt(), e.y.toInt())
+ invalidate()
+ }
+ }
}
// Implements the callbacks for scaleGestureDetector.
diff --git a/app/src/main/java/space/jakob/mines/GameView.kt b/app/src/main/java/space/jakob/mines/GameView.kt
index 118cb45..d0a7ae0 100644
--- a/app/src/main/java/space/jakob/mines/GameView.kt
+++ b/app/src/main/java/space/jakob/mines/GameView.kt
@@ -97,6 +97,19 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
}
}
+ /**
+ * Flag and @return the tile at (@param x, @param y).
+ */
+ fun flagAt(x: Int, y: Int): Tile? = grid?.let {
+ val (x, y) = toGridCoordinates(x.toInt(), y.toInt())
+ if (it.valid(x, y)) {
+ it.flag(x, y)
+ return it[x, y]
+ } else {
+ return null
+ }
+ }
+
private fun atlasRect(entry: AtlasEntry): Rect {
val atlasWidth = 3
@@ -111,7 +124,6 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
val xSpacing = (scaledTileWidth * tileSpacing).toInt()
val ySpacing = (scaledTileWidth * tileSpacing).toInt()
- // TODO: Opportunity for more ASCII art explanations :)
val dst = Rect(
x + xSpacing,
y + ySpacing,
@@ -121,6 +133,10 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
if (tile.masked) {
canvas.drawBitmap(atlas, atlasRect(AtlasEntry.MASKED), dst, null)
+
+ if (tile.flagged) {
+ canvas.drawBitmap(atlas, atlasRect(AtlasEntry.FLAG), dst, null)
+ }
} else {
canvas.drawBitmap(atlas, atlasRect(AtlasEntry.UNMASKED), dst, null)
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<Tile>) {
}
}
+ /**
+ * 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) {