Skip to content

Commit

Permalink
Merge pull request #3 from RajashekarRaju/dev
Browse files Browse the repository at this point in the history
Made design changes,  added new features.
  • Loading branch information
RajashekarRaju committed Apr 8, 2022
2 parents 1f086ab + 051499f commit d29d7bf
Show file tree
Hide file tree
Showing 20 changed files with 718 additions and 82 deletions.
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
}

Expand Down Expand Up @@ -73,6 +73,9 @@ dependencies {
// Navigation
implementation "androidx.navigation:navigation-compose:2.4.1"

// For persisting game difficulty, high score values.
implementation 'androidx.preference:preference-ktx:1.2.0'

// Timber for logging
implementation "com.jakewharton.timber:timber:4.7.1"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fun AppNavigation(
) {
OnBoardingScreen(
navigateToGameScreen = actions.navigateToGameScreen,
navigateToHistoryScreen = actions.navigateToHistoryScreen
navigateToHistoryScreen = actions.navigateToHistoryScreen,
application = application,
) {
actions.finishActivity(activity.finish())
}
Expand Down
42 changes: 33 additions & 9 deletions app/src/main/java/com/hangman/hangman/repository/GameData.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
package com.hangman.hangman.repository

import com.hangman.hangman.modal.Alphabets
import com.hangman.hangman.repository.database.entity.WordsEntity
import com.hangman.hangman.repository.database.entity.HistoryEntity

fun words(): List<WordsEntity> {

val guessingWords = listOf("Blue", "Red", "Purple", "Pink")
object GameData {

val wordsList = ArrayList<WordsEntity>()
guessingWords.forEach {
val wordsEntity = WordsEntity(wordName = it)
wordsList.add(wordsEntity)
}
val easyGuessingWords = listOf(
"india", "china", "sudan", "iran", "libya", "chad", "peru", "egypt", "mali", "chile",
"france", "kenya", "yemen", "spain", "iraq", "norway", "japan", "congo", "italy", "oman",
"ghana", "laos", "syria", "nepal", "cuba", "togo", "haiti", "fiji", "qatar", "tonga",
"malta", "nauru"
)

return wordsList
val mediumGuessingWords = listOf(
"russia", "canada", "brazil", "algeria", "mexico", "angola", "bolivia", "nigeria", "turkey",
"myanmar", "somalia", "ukraine", "morocco", "germany", "finland", "poland", "vietnam",
"guinea", "uganda", "romania", "guyana", "belarus", "uruguay", "tunisia", "iceland",
"jordan", "serbia", "austria", "ireland", "georgia", "panama", "ireland", "croatia",
"denmark", "bhutan", "taiwan", "latvia"
)

val hardGuessingWords = listOf(
"antarctica", "australia", "argentina", "indonesia", "mongolia", "pakistan", "thailand",
"cambodia", "bangladesh", "tajikistan", "guatemala", "portugal", "bulgaria", "azerbaijan",
"lithuania", "netherlands", "switzerland", "palestine", "luxembourg", "dominica", "singapore",
"barbados", "maldives", "kiribati", "mauritius", "slovenia", "honduras", "nicaragua",
"suriname", "kyrgyzstan", "zimbabwe", "botswana"
)
}

val alphabetsList = listOf(
Expand Down Expand Up @@ -43,4 +57,14 @@ val alphabetsList = listOf(
Alphabets(24, "X"),
Alphabets(25, "Y"),
Alphabets(26, "Z")
)

val gameHistory = listOf(
HistoryEntity(1, 3, 1, 2, true),
HistoryEntity(2, 0, 1, 1, false),
HistoryEntity(3, 12, 4, 2, true),
HistoryEntity(4, 4, 2, 3, true),
HistoryEntity(5, 3, 1, 2, true),
HistoryEntity(6, 6, 2, 1, true),
HistoryEntity(7, 15, 5, 1, true),
)
31 changes: 18 additions & 13 deletions app/src/main/java/com/hangman/hangman/repository/GameRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@ package com.hangman.hangman.repository
import com.hangman.hangman.repository.database.entity.HistoryEntity
import com.hangman.hangman.repository.database.entity.WordsEntity
import com.hangman.hangman.repository.database.GameDatabase
import com.hangman.hangman.utils.GameDifficulty
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class GameRepository(
private val database: GameDatabase
) {

private suspend fun getAllGuessingWords(
gameDifficulty: GameDifficulty
): List<WordsEntity> {
val guessingWords: List<WordsEntity>
withContext(Dispatchers.IO) {
guessingWords = database.wordsDao.getGuessingWordsByGameDifficulty(gameDifficulty)
}

return guessingWords
}

suspend fun getRandomGuessingWord(
gameDifficulty: GameDifficulty
): String {
return getAllGuessingWords(gameDifficulty).random().wordName
}

suspend fun saveCurrentGameToHistory(
historyEntity: HistoryEntity
) {
Expand Down Expand Up @@ -40,17 +58,4 @@ class GameRepository(
database.historyDao.deleteAllGamesHistory()
}
}

private suspend fun getAllGuessingWords(): List<WordsEntity> {
val guessingWords: List<WordsEntity>
withContext(Dispatchers.IO) {
guessingWords = database.wordsDao.getAllGuessingWords()
}

return guessingWords
}

suspend fun getRandomGuessingWord(): String {
return getAllGuessingWords().random().wordName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hangman.hangman.repository.database.dao

import androidx.room.*
import com.hangman.hangman.repository.database.entity.HistoryEntity
import com.hangman.hangman.repository.gameHistory

@Dao
interface HistoryDao {
Expand All @@ -10,7 +11,7 @@ interface HistoryDao {
suspend fun saveNewGameToHistory(historyEntity: HistoryEntity)

@Query("SELECT * FROM history_table")
suspend fun getCompleteGameHistory(): List<HistoryEntity>
suspend fun getCompleteGameHistory(): List<HistoryEntity> = gameHistory

@Delete
suspend fun deleteSingleGameHistory(historyEntity: HistoryEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package com.hangman.hangman.repository.database.dao
import androidx.room.Dao
import androidx.room.Query
import com.hangman.hangman.repository.database.entity.WordsEntity
import com.hangman.hangman.repository.words
import com.hangman.hangman.utils.GameDifficulty
import com.hangman.hangman.utils.getFilteredWordsByGameDifficulty

@Dao
interface WordsDao {

@Query("SELECT * FROM words_table")
suspend fun getAllGuessingWords(): List<WordsEntity> = words()
suspend fun getGuessingWordsByGameDifficulty(
gameDifficulty: GameDifficulty
): List<WordsEntity> {
return getFilteredWordsByGameDifficulty(gameDifficulty)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import androidx.room.PrimaryKey
data class HistoryEntity(

@Stable
@PrimaryKey
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "column_game_id")
val gameId: Int,

@ColumnInfo(name = "column_game_score")
val gameScore: Int,

@ColumnInfo(name = "column_game_level")
val gameLevel: Int,

@ColumnInfo(name = "column_game_difficulty")
val gameDifficulty: Int,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.hangman.hangman.ui.game

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinx.coroutines.launch


@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ShowExitGameModalSheet(
navigateUp: () -> Unit,
modalSheetState: ModalBottomSheetState
) {
val coroutineScope = rememberCoroutineScope()

Column(
modifier = Modifier.padding(horizontal = 40.dp, vertical = 60.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Exit Game ?",
style = MaterialTheme.typography.h5,
color = MaterialTheme.colors.primary.copy(0.75f),
)

Spacer(modifier = Modifier.height(48.dp))

Button(
onClick = {
coroutineScope.launch {
modalSheetState.hide()
}
navigateUp()
},
shape = MaterialTheme.shapes.medium,
colors = ButtonDefaults.outlinedButtonColors(backgroundColor = Color.Transparent),
border = BorderStroke(
width = 2.dp,
color = MaterialTheme.colors.primary.copy(0.5f)
),
modifier = Modifier.fillMaxWidth()
) {
Text(
text = "Exit",
letterSpacing = 4.sp,
style = MaterialTheme.typography.button,
color = MaterialTheme.colors.primary.copy(0.75f),
modifier = Modifier.padding(vertical = 4.dp)
)
}

Spacer(modifier = Modifier.height(8.dp))

Button(
modifier = Modifier.fillMaxWidth(),
elevation = ButtonDefaults.elevation(defaultElevation = 0.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = Color.Transparent),
onClick = {
coroutineScope.launch {
modalSheetState.hide()
}
},
) {
Text(
text = "Keep Playing",
letterSpacing = 2.sp,
style = MaterialTheme.typography.button,
color = MaterialTheme.colors.primary.copy(0.75f),
modifier = Modifier.padding(vertical = 4.dp)
)
}
}
}
Loading

0 comments on commit d29d7bf

Please sign in to comment.