Skip to content

Commit

Permalink
First release is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
kmorfo committed Jan 15, 2024
1 parent ccc6580 commit 1146f61
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 27 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ This is my version of the classic game minesweeper developed in kotlin
- [ X ] Toast for alerts. Only when the user has no more mines
- [ X ] Animations. Only in Dialogs
- [ X ] Persistence for save records
- [ ] Dialog records
- [ X ] Show records on HomeScreen
- [ X ] Board Test Created

# Screenshots
<img src="./githubimages/home.png"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package es.rlujancreations.minesweeper.ui.home

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
Expand All @@ -28,6 +36,7 @@ import es.rlujancreations.minesweeper.data.Level
import es.rlujancreations.minesweeper.ui.composables.OutlinedCustomButton
import es.rlujancreations.minesweeper.ui.theme.DarkBlue
import es.rlujancreations.minesweeper.ui.theme.LightBlue
import es.rlujancreations.minesweeper.ui.theme.RecordsBoard


/**
Expand All @@ -40,6 +49,8 @@ fun HomeScreen(
navigateToGame: (Level) -> Unit,
navigateToHelp: () -> Unit,
) {
val records by homeViewModel.recordsState.collectAsState()

Column(
modifier = Modifier
.fillMaxSize()
Expand All @@ -58,6 +69,30 @@ fun HomeScreen(
.size(220.dp)
)
Spacer(modifier = Modifier.weight(2f))
OutlinedCard(
colors = CardDefaults.cardColors(containerColor = RecordsBoard),
border = BorderStroke(1.dp, Color.Black),
modifier = Modifier
.fillMaxWidth()
.padding(16.dp),
) {
RecordText(
text = stringResource(id = R.string.best_times),
color = Color.Black,
modifier = Modifier.padding(8.dp)
)
Row(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
horizontalArrangement = Arrangement.SpaceAround
) {
RecordText(text = records.easy, color = Color(0xFF3F51B5))
RecordText(text = records.medium, color = Color(0xFF0F3452))
RecordText(text = records.hard, color = Color(0xFFF44336))
}
}
Spacer(modifier = Modifier.weight(0.5f))

OutlinedCustomButton(
text = R.string.btn_play_easy,
Expand All @@ -73,7 +108,7 @@ fun HomeScreen(
onClick = { navigateToGame(Level.Hard) })

Spacer(modifier = Modifier.weight(1f))
FloatingActionButton(containerColor = DarkBlue,onClick = { navigateToHelp() }) {
FloatingActionButton(containerColor = DarkBlue, onClick = { navigateToHelp() }) {
Icon(
modifier = Modifier.size(35.dp),
painter = painterResource(id = R.drawable.ic_help),
Expand All @@ -83,4 +118,15 @@ fun HomeScreen(
}
Spacer(modifier = Modifier.weight(0.5f))
}
}
}

@Composable
private fun RecordText(text: String, color: Color, modifier: Modifier = Modifier) {
Text(
modifier = modifier,
text = text,
color = color,
fontSize = 18.sp,
fontWeight = FontWeight.Bold
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class HomeViewModel @Inject constructor(
) :
ViewModel() {

private var _recordsState = MutableStateFlow<Array<String>>(arrayOf("999", "999", "999"))
val records: StateFlow<Array<String>> = _recordsState
private var _recordsState = MutableStateFlow(RecordState())
val recordsState: StateFlow<RecordState> = _recordsState

init {
loadRecords(level = Level.Easy)
Expand All @@ -35,14 +35,18 @@ class HomeViewModel @Inject constructor(
val record = async {
databaseServiceImpl.getRecordByLevel(level = level).first()
}.await()
val index = when (level) {
Level.Easy -> 0
Level.Hard -> 1
Level.Medium -> 2
_recordsState.value = when (level) {
Level.Easy -> _recordsState.value.copy(easy = record)
Level.Medium -> _recordsState.value.copy(medium = record)
Level.Hard -> _recordsState.value.copy(hard = record)
}
_recordsState.value[index] = record
}
}
}

data class RecordState(
val easy: String = "9999",
val medium: String = "9999",
val hard: String = "9999",
)

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ val CounterFontColor =Color(0xFFF50E02)

val HeaderBackground =Color(0xFFB3B1B1)
val BoardBackground =Color(0xFFC6C6C6)
val RecordsBoard = Color(0x66C6C6C6)



Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<string name="btn_play_easy">Easy</string>
<string name="btn_play_medium">Medium</string>
<string name="btn_play_hard">Hard</string>
<string name="best_times">Best times</string>

<string name="play_again">Play new game!</string>
<string name="result_game_image">End of the game image</string>
Expand Down

This file was deleted.

47 changes: 47 additions & 0 deletions app/src/test/java/es/rlujancreations/minesweeper/data/BoardTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package es.rlujancreations.minesweeper.data

import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test


/**
* Created by Raúl L.C. on 15/1/24.
*/
class BoardTest{

private lateinit var board: Board

@Before
fun setUp(){
board = Board()
}

@Test
fun `check if the number of mines matches with level`(){
val level = Level.Hard
board.initialize(level)

assertEquals(level.mines,board.getMines())
}

@Test
fun `check cell with valid coordinates returns cell value`(){
val level = Level.Easy
board.initialize(level)

val cellValue = board.getCell(0,0)

assertEquals(true,cellValue >=-1 && cellValue <=4 )
}

@Test
fun `check cell with invalid coordinates returns default value`(){
val level = Level.Medium
board.initialize(level)

val cellValue = board.getCell(-1,0)

assertEquals(0,cellValue)
}
}
Binary file modified githubimages/home.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1146f61

Please sign in to comment.