Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,33 @@ class GameViewModel @Inject constructor(
private fun checkForMatch() {
if (selectedCards[0].imageRes == selectedCards[1].imageRes) {
_score.value += 10
_cards.update { currentCards ->
currentCards.map { card ->
if (selectedCards.any { it.id == card.id }) {
card.copy(isMatched = true)
} else card
}
}
selectedCards.clear()
soundManager.playPairSound()
checkIfGameWon()
} else {
//soundManager.playFlipSound()
viewModelScope.launch {
delay(800)
_cards.update { currentCards ->
currentCards.map { card ->
if (selectedCards.any { it.id == card.id }) {
card.copy(isMatched = true)
} else card
if (selectedCards.any { it.id == card.id }) {
card.copy(isFlipped = false)
}
else card
}
}
selectedCards.clear()
soundManager.playPairSound()
} else {
//soundManager.playFlipSound()
viewModelScope.launch {
delay(800)
_cards.update { currentCards ->
currentCards.map { card ->
if (selectedCards.any { it.id == card.id }) {
card.copy(isFlipped = false)
}
else card
}
}
selectedCards.clear()
}
checkIfGameWon()
}
checkIfGameWon()
}
}

private fun checkIfGameWon() {
if (_cards.value.all { it.isMatched }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ class GameViewModelTest {
vm.flipCard(2)
vm.flipCard(3)
vm.flipCard(4)
// Game should not be won immediately after a mismatch
assertFalse(vm.isGameWon.value)
advanceTimeBy(800)
advanceUntilIdle()
// And it should remain not won after cards reset
assertFalse(vm.isGameWon.value)
}

Expand Down
Loading