-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of Issue #9
- Loading branch information
Showing
7 changed files
with
69 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/keren/tomer/minesweeper/GameViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.keren.tomer.minesweeper | ||
|
||
import android.arch.lifecycle.MutableLiveData | ||
import android.arch.lifecycle.ViewModel | ||
import android.arch.lifecycle.ViewModelProvider | ||
|
||
class GameViewModel(height: Int, width: Int, amountOfMines: Int) : ViewModel() { | ||
val game: Game = Game(height, width, amountOfMines).apply { endCallback = { winnState.value = it } } | ||
val height = game.height | ||
val width = game.width | ||
val board = game.board.flatten() | ||
val winnState: MutableLiveData<Game.EndState> = MutableLiveData() | ||
val flagsLeft: MutableLiveData<Int> = MutableLiveData() | ||
val tiles: List<MutableLiveData<IndexedTile>> = board.map { MutableLiveData<IndexedTile>().apply { value = it } } | ||
val gameState = MutableLiveData<Game.EndState>() | ||
private fun flagsLeft_() = game.amountOfMines - board.count { it.value.isFlagged } | ||
fun holdTile(i: Int, j: Int) { | ||
game.holdTile(i, j) | ||
updateData() | ||
} | ||
|
||
private fun updateData() { | ||
flagsLeft.value = flagsLeft_() | ||
gameState.value = game.winState | ||
} | ||
|
||
fun clickTile(i: Int, j: Int) { | ||
game.clickTile(i, j) | ||
updateData() | ||
} | ||
|
||
} | ||
|
||
class GameViewModelFactory(private val height: Int, private val width: Int, private val amountOfMines: Int) : ViewModelProvider.Factory { | ||
|
||
override fun <T : ViewModel?> create(modelClass: Class<T>): T { | ||
@Suppress("UNCHECKED_CAST") | ||
return GameViewModel(height, width, amountOfMines) as T | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
app/src/test/java/com/keren/tomer/minesweeper/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters