-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guess Hints.kt
527 lines (509 loc) · 23.5 KB
/
Guess Hints.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package com.example.project1geography
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.unit.sp
import androidx.navigation.compose.rememberNavController
import com.example.project1geography.ui.theme.TimerViewModel
import androidx.compose.ui.unit.sp as sp1
// class to add the timer from the homepage
class GuessHints : ComponentActivity() {
private val timerViewModel: TimerViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val navController = rememberNavController() // Obtain NavHostController
GuessHints(navController, timerViewModel)
}
}
}
}
// function that connected the game with the homepage and the back button to go back to the homepage
@Composable
fun GuessHints(navController: NavHostController, timerViewModel: TimerViewModel) {
// Start the timer
LaunchedEffect(true) {
if(timerViewModel.isRunning) {
timerViewModel.startTimer()
}
}
Column(modifier = Modifier
.fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Guess Hints", style = TextStyle(
fontSize = 40.sp1,
fontWeight = FontWeight.Bold,
)
)
Text(text = "Timer: ${timerViewModel.liveElapsedTime.value} seconds")
Hints(timerViewModel = timerViewModel)
}
Column (modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.Start
){
Button(onClick = {
navController.navigate(route = "HomePage") })
{
Text(text = "Back")
}
}
}
@Composable
fun Hints(timerViewModel: TimerViewModel = remember { TimerViewModel() }) {
// list of flags and their names
val flagAndCountries = listOf(
FlagAndCountries(R.drawable.af, "Afghanistan"),
FlagAndCountries(R.drawable.ax, "Åland Islands"),
FlagAndCountries(R.drawable.al, "Albania"),
FlagAndCountries(R.drawable.dz, "Algeria"),
FlagAndCountries(R.drawable.`as`, "American Samoa"),
FlagAndCountries(R.drawable.ad, "Andorra"),
FlagAndCountries(R.drawable.ao, "Angola"),
FlagAndCountries(R.drawable.ai, "Anguilla"),
FlagAndCountries(R.drawable.aq, "Antarctica"),
FlagAndCountries(R.drawable.ag, "Antigua and Barbuda"),
FlagAndCountries(R.drawable.ar, "Argentina"),
FlagAndCountries(R.drawable.am, "Armenia"),
FlagAndCountries(R.drawable.aw, "Aruba"),
FlagAndCountries(R.drawable.au, "Australia"),
FlagAndCountries(R.drawable.at, "Austria"),
FlagAndCountries(R.drawable.az, "Azerbaijan"),
FlagAndCountries(R.drawable.bs, "Bahamas"),
FlagAndCountries(R.drawable.bh, "Bahrain"),
FlagAndCountries(R.drawable.bd, "Bangladesh"),
FlagAndCountries(R.drawable.bb, "Barbados"),
FlagAndCountries(R.drawable.by, "Belarus"),
FlagAndCountries(R.drawable.be, "Belgium"),
FlagAndCountries(R.drawable.bz, "Belize"),
FlagAndCountries(R.drawable.bj, "Benin"),
FlagAndCountries(R.drawable.bm, "Bermuda"),
FlagAndCountries(R.drawable.bt, "Bhutan"),
FlagAndCountries(R.drawable.bo, "Bolivia"),
FlagAndCountries(R.drawable.ba, "Bosnia and Herzegovina"),
FlagAndCountries(R.drawable.bw, "Botswana"),
FlagAndCountries(R.drawable.br, "Brazil"),
FlagAndCountries(R.drawable.io, "British Indian Ocean Territory"),
FlagAndCountries(R.drawable.bn, "Brunei"),
FlagAndCountries(R.drawable.bg, "Bulgaria"),
FlagAndCountries(R.drawable.bf, "Burkina Faso"),
FlagAndCountries(R.drawable.bi, "Burundi"),
FlagAndCountries(R.drawable.kh, "Cambodia"),
FlagAndCountries(R.drawable.cm, "Cameroon"),
FlagAndCountries(R.drawable.ca, "Canada"),
FlagAndCountries(R.drawable.cv, "Cabo Verde"),
FlagAndCountries(R.drawable.ky, "Cayman Islands"),
FlagAndCountries(R.drawable.cf, "Central African Republic"),
FlagAndCountries(R.drawable.td, "Chad"),
FlagAndCountries(R.drawable.cl, "Chile"),
FlagAndCountries(R.drawable.cn, "China"),
FlagAndCountries(R.drawable.cx, "Christmas Island"),
FlagAndCountries(R.drawable.cc, "Cocos (Keeling) Islands"),
FlagAndCountries(R.drawable.co, "Colombia"),
FlagAndCountries(R.drawable.km, "Comoros"),
FlagAndCountries(R.drawable.cg, "Congo (Brazzaville)"),
FlagAndCountries(R.drawable.cd, "Congo (Kinshasa)"),
FlagAndCountries(R.drawable.ck, "Cook Islands"),
FlagAndCountries(R.drawable.cr, "Costa Rica"),
FlagAndCountries(R.drawable.hr, "Croatia"),
FlagAndCountries(R.drawable.cu, "Cuba"),
FlagAndCountries(R.drawable.cy, "Cyprus"),
FlagAndCountries(R.drawable.cz, "Czechia"),
FlagAndCountries(R.drawable.dk, "Denmark"),
FlagAndCountries(R.drawable.dj, "Djibouti"),
FlagAndCountries(R.drawable.dm, "Dominica"),
FlagAndCountries(R.drawable.resource_do, "Dominican Republic"),
FlagAndCountries(R.drawable.ec, "Ecuador"),
FlagAndCountries(R.drawable.eg, "Egypt"),
FlagAndCountries(R.drawable.sv, "El Salvador"),
FlagAndCountries(R.drawable.gq, "Equatorial Guinea"),
FlagAndCountries(R.drawable.er, "Eritrea"),
FlagAndCountries(R.drawable.ee, "Estonia"),
FlagAndCountries(R.drawable.sz, "Eswatini"),
FlagAndCountries(R.drawable.et, "Ethiopia"),
FlagAndCountries(R.drawable.fk, "Falkland Islands"),
FlagAndCountries(R.drawable.fo, "Faroe Islands"),
FlagAndCountries(R.drawable.fj, "Fiji"),
FlagAndCountries(R.drawable.fi, "Finland"),
FlagAndCountries(R.drawable.fr, "France"),
FlagAndCountries(R.drawable.pf, "French Polynesia"),
FlagAndCountries(R.drawable.tf, "French Southern Territories"),
FlagAndCountries(R.drawable.ga, "Gabon"),
FlagAndCountries(R.drawable.gm, "Gambia"),
FlagAndCountries(R.drawable.ge, "Georgia"),
FlagAndCountries(R.drawable.de, "Germany"),
FlagAndCountries(R.drawable.gh, "Ghana"),
FlagAndCountries(R.drawable.gi, "Gibraltar"),
FlagAndCountries(R.drawable.gr, "Greece"),
FlagAndCountries(R.drawable.gl, "Greenland"),
FlagAndCountries(R.drawable.gd, "Grenada"),
FlagAndCountries(R.drawable.gp, "Guadeloupe"),
FlagAndCountries(R.drawable.gu, "Guam"),
FlagAndCountries(R.drawable.gt, "Guatemala"),
FlagAndCountries(R.drawable.gg, "Guernsey"),
FlagAndCountries(R.drawable.gn, "Guinea"),
FlagAndCountries(R.drawable.gw, "Guinea-Bissau"),
FlagAndCountries(R.drawable.gy, "Guyana"),
FlagAndCountries(R.drawable.ht, "Haiti"),
FlagAndCountries(R.drawable.hm, "Heard and McDonald Islands"),
FlagAndCountries(R.drawable.va, "Vatican City"),
FlagAndCountries(R.drawable.hn, "Honduras"),
FlagAndCountries(R.drawable.hk, "Hong Kong"),
FlagAndCountries(R.drawable.hu, "Hungary"),
FlagAndCountries(R.drawable.`is`, "Iceland"),
FlagAndCountries(R.drawable.`in`, "India"),
FlagAndCountries(R.drawable.id, "Indonesia"),
FlagAndCountries(R.drawable.ir, "Iran"),
FlagAndCountries(R.drawable.iq, "Iraq"),
FlagAndCountries(R.drawable.ie, "Ireland"),
FlagAndCountries(R.drawable.im, "Isle of Man"),
FlagAndCountries(R.drawable.il, "Israel"),
FlagAndCountries(R.drawable.it, "Italy"),
FlagAndCountries(R.drawable.jm, "Jamaica"),
FlagAndCountries(R.drawable.jp, "Japan"),
FlagAndCountries(R.drawable.je, "Jersey"),
FlagAndCountries(R.drawable.jo, "Jordan"),
FlagAndCountries(R.drawable.kz, "Kazakhstan"),
FlagAndCountries(R.drawable.ke, "Kenya"),
FlagAndCountries(R.drawable.ki, "Kiribati"),
FlagAndCountries(R.drawable.kw, "Kuwait"),
FlagAndCountries(R.drawable.kg, "Kyrgyzstan"),
FlagAndCountries(R.drawable.la, "Laos"),
FlagAndCountries(R.drawable.lv, "Latvia"),
FlagAndCountries(R.drawable.lb, "Lebanon"),
FlagAndCountries(R.drawable.ls, "Lesotho"),
FlagAndCountries(R.drawable.lr, "Liberia"),
FlagAndCountries(R.drawable.ly, "Libya"),
FlagAndCountries(R.drawable.li, "Liechtenstein"),
FlagAndCountries(R.drawable.lt, "Lithuania"),
FlagAndCountries(R.drawable.lu, "Luxembourg"),
FlagAndCountries(R.drawable.mo, "Macao"),
FlagAndCountries(R.drawable.mg, "Madagascar"),
FlagAndCountries(R.drawable.mw, "Malawi"),
FlagAndCountries(R.drawable.my, "Malaysia"),
FlagAndCountries(R.drawable.mv, "Maldives"),
FlagAndCountries(R.drawable.ml, "Mali"),
FlagAndCountries(R.drawable.mt, "Malta"),
FlagAndCountries(R.drawable.mh, "Marshall Islands"),
FlagAndCountries(R.drawable.mr, "Mauritania"),
FlagAndCountries(R.drawable.mu, "Mauritius"),
FlagAndCountries(R.drawable.mx, "Mexico"),
FlagAndCountries(R.drawable.fm, "Micronesia"),
FlagAndCountries(R.drawable.md, "Moldova"),
FlagAndCountries(R.drawable.mc, "Monaco"),
FlagAndCountries(R.drawable.mn, "Mongolia"),
FlagAndCountries(R.drawable.me, "Montenegro"),
FlagAndCountries(R.drawable.ms, "Montserrat"),
FlagAndCountries(R.drawable.ma, "Morocco"),
FlagAndCountries(R.drawable.mz, "Mozambique"),
FlagAndCountries(R.drawable.mm, "Myanmar"),
FlagAndCountries(R.drawable.na, "Namibia"),
FlagAndCountries(R.drawable.nr, "Nauru"),
FlagAndCountries(R.drawable.np, "Nepal"),
FlagAndCountries(R.drawable.nl, "Netherlands"),
FlagAndCountries(R.drawable.nz, "New Zealand"),
FlagAndCountries(R.drawable.ni, "Nicaragua"),
FlagAndCountries(R.drawable.ne, "Niger"),
FlagAndCountries(R.drawable.ng, "Nigeria"),
FlagAndCountries(R.drawable.nu, "Niue"),
FlagAndCountries(R.drawable.kp, "North Korea"),
FlagAndCountries(R.drawable.nf, "Norfolk Island"),
FlagAndCountries(R.drawable.mp, "Northern Mariana Islands"),
FlagAndCountries(R.drawable.no, "Norway"),
FlagAndCountries(R.drawable.om, "Oman"),
FlagAndCountries(R.drawable.pk, "Pakistan"),
FlagAndCountries(R.drawable.pw, "Palau"),
FlagAndCountries(R.drawable.ps, "Palestine"),
FlagAndCountries(R.drawable.pa, "Panama"),
FlagAndCountries(R.drawable.pg, "Papua New Guinea"),
FlagAndCountries(R.drawable.py, "Paraguay"),
FlagAndCountries(R.drawable.pe, "Peru"),
FlagAndCountries(R.drawable.ph, "Philippines"),
FlagAndCountries(R.drawable.pn, "Pitcairn Islands"),
FlagAndCountries(R.drawable.pl, "Poland"),
FlagAndCountries(R.drawable.pt, "Portugal"),
FlagAndCountries(R.drawable.pr, "Puerto Rico"),
FlagAndCountries(R.drawable.qa, "Qatar"),
FlagAndCountries(R.drawable.mk, "Republic of North Macedonia"),
FlagAndCountries(R.drawable.ro, "Romania"),
FlagAndCountries(R.drawable.ru, "Russia"),
FlagAndCountries(R.drawable.rw, "Rwanda"),
FlagAndCountries(R.drawable.sh, "Saint Helena"),
FlagAndCountries(R.drawable.kn, "Saint Kitts and Nevis"),
FlagAndCountries(R.drawable.lc, "Saint Lucia"),
FlagAndCountries(R.drawable.vc, "Saint Vincent and the Grenadines"),
FlagAndCountries(R.drawable.ws, "Samoa"),
FlagAndCountries(R.drawable.sm, "San Marino"),
FlagAndCountries(R.drawable.st, "São Tomé and Príncipe"),
FlagAndCountries(R.drawable.sa, "Saudi Arabia"),
FlagAndCountries(R.drawable.sn, "Senegal"),
FlagAndCountries(R.drawable.rs, "Serbia"),
FlagAndCountries(R.drawable.sc, "Seychelles"),
FlagAndCountries(R.drawable.sl, "Sierra Leone"),
FlagAndCountries(R.drawable.sg, "Singapore"),
FlagAndCountries(R.drawable.sx, "Sint Maarten"),
FlagAndCountries(R.drawable.sk, "Slovakia"),
FlagAndCountries(R.drawable.si, "Slovenia"),
FlagAndCountries(R.drawable.sb, "Solomon Islands"),
FlagAndCountries(R.drawable.so, "Somalia"),
FlagAndCountries(R.drawable.kr, "South Korea"),
FlagAndCountries(R.drawable.za, "South Africa"),
FlagAndCountries(R.drawable.gs, "South Georgia and the South Sandwich Islands"),
FlagAndCountries(R.drawable.ss, "South Sudan"),
FlagAndCountries(R.drawable.es, "Spain"),
FlagAndCountries(R.drawable.lk, "Sri Lanka"),
FlagAndCountries(R.drawable.sd, "Sudan"),
FlagAndCountries(R.drawable.sr, "Suriname"),
FlagAndCountries(R.drawable.sj, "Svalbard and Jan Mayen"),
FlagAndCountries(R.drawable.se, "Sweden"),
FlagAndCountries(R.drawable.ch, "Switzerland"),
FlagAndCountries(R.drawable.sy, "Syria"),
FlagAndCountries(R.drawable.tw, "Taiwan"),
FlagAndCountries(R.drawable.tj, "Tajikistan"),
FlagAndCountries(R.drawable.tz, "Tanzania"),
FlagAndCountries(R.drawable.th, "Thailand"),
FlagAndCountries(R.drawable.tl, "Timor-Leste"),
FlagAndCountries(R.drawable.tg, "Togo"),
FlagAndCountries(R.drawable.tk, "Tokelau"),
FlagAndCountries(R.drawable.to, "Tonga"),
FlagAndCountries(R.drawable.tt, "Trinidad and Tobago"),
FlagAndCountries(R.drawable.tn, "Tunisia"),
FlagAndCountries(R.drawable.tr, "Turkey"),
FlagAndCountries(R.drawable.tm, "Turkmenistan"),
FlagAndCountries(R.drawable.tc, "Turks and Caicos Islands"),
FlagAndCountries(R.drawable.tv, "Tuvalu"),
FlagAndCountries(R.drawable.ug, "Uganda"),
FlagAndCountries(R.drawable.ua, "Ukraine"),
FlagAndCountries(R.drawable.ae, "United Arab Emirates"),
FlagAndCountries(R.drawable.gb, "United Kingdom"),
FlagAndCountries(R.drawable.us, "United States"),
FlagAndCountries(R.drawable.uy, "Uruguay"),
FlagAndCountries(R.drawable.uz, "Uzbekistan"),
FlagAndCountries(R.drawable.vu, "Vanuatu"),
FlagAndCountries(R.drawable.ve, "Venezuela"),
FlagAndCountries(R.drawable.vn, "Vietnam"),
FlagAndCountries(R.drawable.wf, "Wallis and Futuna"),
FlagAndCountries(R.drawable.eh, "Western Sahara"),
FlagAndCountries(R.drawable.ye, "Yemen"),
FlagAndCountries(R.drawable.zm, "Zambia"),
FlagAndCountries(R.drawable.zw, "Zimbabwe")
)
// variables to produce random flags and to know if the guess was correct
// also displays correct answer
// launched effect to display need flag and dotted line
val randomIndex = remember { mutableIntStateOf((flagAndCountries.indices).random()) }
var selectFlagAndCountries = flagAndCountries[randomIndex.intValue]
var countryName by remember { mutableStateOf("") }
var guessedName by remember { mutableStateOf("") }
val correctGuesses by remember { mutableStateOf(mutableListOf<Char>()) }
var incorrectGuessCount by remember { mutableIntStateOf(0) }
var isCorrect by remember { mutableStateOf(true) }
var nextClicked by remember { mutableStateOf(false) }
var displayAnswer by remember { mutableStateOf(false) }
LaunchedEffect(selectFlagAndCountries) {
countryName = selectFlagAndCountries.countryName
}
// random image generator
Column(
modifier = Modifier
.fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(id = selectFlagAndCountries.drawableId),
contentDescription = null,
modifier = Modifier
.height(100.dp)
.padding(8.dp)
.border(1.dp, Color.Black),
contentScale = ContentScale.Crop
)
// adds the dotted line and makes it so no matter the case for the letter guessed will display
Row(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.padding(horizontal = 8.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.Top
) {
countryName.forEach { char ->
Text(
text = if (correctGuesses.contains(char.uppercaseChar()) || guessedName.contains(
char,
ignoreCase = true
)
) char.toString() else "-",
color = Color.Black,
fontSize = 70.sp
)
}
}
// Editable text box
BasicTextField( modifier = Modifier
.fillMaxWidth()
.weight(0.5f)
.padding(horizontal = 8.dp)
.border(4.dp, color = Color.Black),
value = guessedName,
onValueChange = {
if (it.length == 1) {
guessedName = it
}
},
textStyle = TextStyle(
color = Color.Black,
fontSize = 30.sp1,
fontWeight = FontWeight.Bold
),
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
// Check if the guessed character is correct and replace the dash
if (guessedName.isNotEmpty()) {
val guessedChar = guessedName[0].lowercaseChar() // or toUpperCase()
if (countryName.lowercase().contains(guessedChar)) {
// Update the correct guesses
correctGuesses.add(guessedChar)
} else {
// Increment the incorrect guess count
incorrectGuessCount++
}
}
// Clear the text box for the next guess
guessedName = ""
// Check if the incorrect guess count reaches the threshold
if (incorrectGuessCount >= 3) {
// Display "Wrong" in red text
isCorrect = false
}
}
)
)
Button(
onClick = {
if (displayAnswer) {
// Reset the game
nextClicked = false
isCorrect = true
displayAnswer = false
randomIndex.intValue = (flagAndCountries.indices).random()
selectFlagAndCountries = flagAndCountries[randomIndex.intValue]
guessedName = ""
incorrectGuessCount = 0
correctGuesses.clear()
}
if (timerViewModel.liveElapsedTime.value == 0 ){
timerViewModel.resetTimer()
timerViewModel.startTimer()
}else {
// Check if the guessed character is correct and replace the dash
if (guessedName.isNotEmpty()) {
val guessedChar = guessedName[0].lowercase()
// Update the correct guesses
if (countryName.lowercase().contains(guessedChar)) {
correctGuesses.add(guessedName[0].uppercaseChar())
} else if (countryName.uppercase().contains(guessedName.uppercase())) {
incorrectGuessCount++
} else {
incorrectGuessCount++
}
// Increment the incorrect guess count
// Clear the text box for the next guess
guessedName = ""
// Check if the incorrect guess count reaches the threshold
if (incorrectGuessCount >= 3) {
isCorrect = false
}
// Check if the game is over (incorrect guesses or correct answer)
if (!isCorrect || correctGuesses.size == countryName.lowercase().toSet().size || incorrectGuessCount >= 3) {
// Set the flag to display the answer
displayAnswer = true
}
}
}
},
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
colors = ButtonDefaults.buttonColors
(containerColor = Color.Cyan),
shape = CutCornerShape(8.dp)
)
{
Text(text = if (displayAnswer|| timerViewModel.liveElapsedTime.value == 0) "Next" else "Submit", fontSize = 20.sp)
}
if (!isCorrect) {
Text(
text = "Wrong",
color = Color.Red,
fontSize = 35.sp,
fontWeight = FontWeight.Bold
)
}
// Display "Correct" message in green text
if (correctGuesses.size == countryName.lowercase().toSet().size) {
Text(
text = "Correct!",
color = Color.Green,
fontSize = 35.sp,
fontWeight = FontWeight.Bold
)
}
if (incorrectGuessCount >= 3) {
Text(
text = "Correct Answer: ${selectFlagAndCountries.countryName}",
color = Color.Blue,
fontWeight = FontWeight.Bold,
fontSize = 30.sp,
modifier = Modifier.padding(8.dp),
)
}
}
}