Skip to content

Commit

Permalink
implementar undo
Browse files Browse the repository at this point in the history
  • Loading branch information
RodAlc24 committed Apr 22, 2024
1 parent 38a3f38 commit 5c546d1
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
android:theme="@style/Theme.Amarracos"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".main.MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Amarracos">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rodalc.amarracos
package com.rodalc.amarracos.main

import android.os.Bundle
import androidx.activity.ComponentActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rodalc.amarracos
package com.rodalc.amarracos.main

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rodalc.amarracos
package com.rodalc.amarracos.main

import androidx.compose.material3.Button
import androidx.compose.material3.Text
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/rodalc/amarracos/pocha/Jugador.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
data class Jugador(
val id: Int,
var nombre: String = "",
var punots: Int = 0,
var puntos: Int = 0,
var apuesta: Int = 0,
var victoria: Int = 0,
) {
Expand Down
80 changes: 56 additions & 24 deletions app/src/main/java/com/rodalc/amarracos/pocha/PantallaPocha.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rodalc.amarracos.pocha

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -36,6 +37,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.rodalc.amarracos.storage.StateSaver
import com.rodalc.amarracos.storage.UndoStack
import kotlin.math.abs

/**
Expand All @@ -52,6 +54,11 @@ fun PantallaPocha() {
var duplica by rememberSaveable { mutableStateOf(false) }
val context = LocalContext.current
var showRecoever by remember { mutableStateOf(StateSaver.fileExist(context)) }
val undoStack by remember { mutableStateOf(UndoStack<List<Jugador>>()) }

BackHandler(enabled = state != Ronda.NOMBRES) {
state = Ronda.NOMBRES
}

if (showRecoever) {
Dialog(onDismissRequest = { }) {
Expand Down Expand Up @@ -154,7 +161,7 @@ fun PantallaPocha() {
valorState = if (state == Ronda.APUESTAS) jugador.apuesta else jugador.victoria
val textoFila = jugador.nombre
val puntos =
if (state == Ronda.APUESTAS) "${jugador.punots}" else "(${jugador.apuesta}) ${jugador.punots}"
if (state == Ronda.APUESTAS) "${jugador.puntos}" else "(${jugador.apuesta}) ${jugador.puntos}"

FilaJugador(
texto = textoFila,
Expand All @@ -181,34 +188,59 @@ fun PantallaPocha() {
Switch(checked = duplica, onCheckedChange = { duplica = it })
}
}
Button(onClick = {
state = when (state) {
Ronda.NOMBRES -> {
for (jugador in jugadores) {
if (jugador.nombre == "") jugador.nombre = "Jugador ${jugador.id}"
}
Ronda.APUESTAS
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
if (state != Ronda.NOMBRES) {
Button(
modifier = Modifier.padding(10.dp),
enabled = (state == Ronda.CONTEO || undoStack.size() > 0),
onClick = {
if (state == Ronda.CONTEO) {
state = Ronda.APUESTAS
} else {
jugadores = undoStack.pop()!!
state = Ronda.CONTEO
}

}) {
Text(text = "Volver")
}
Spacer(modifier = Modifier.weight(1f))
}
Button(
modifier = Modifier.padding(10.dp),
onClick = {
state = when (state) {
Ronda.NOMBRES -> {
for (jugador in jugadores) {
if (jugador.nombre == "") jugador.nombre = "Jugador ${jugador.id}"
}
Ronda.APUESTAS
}

Ronda.APUESTAS -> Ronda.CONTEO
Ronda.CONTEO -> {
for (jugador in jugadores) {
val incremento = if (jugador.apuesta == jugador.victoria) {
10 + 5 * jugador.apuesta
} else {
-5 * abs(jugador.apuesta - jugador.victoria)
Ronda.APUESTAS -> Ronda.CONTEO
Ronda.CONTEO -> {
undoStack.push(jugadores.map { it.copy() })
for (jugador in jugadores) {
val incremento = if (jugador.apuesta == jugador.victoria) {
10 + 5 * jugador.apuesta
} else {
-5 * abs(jugador.apuesta - jugador.victoria)
}
jugador.puntos += if (duplica) 2 * incremento else incremento
jugador.apuesta = 0
jugador.victoria = 0
}
duplica = false
StateSaver.savePocha(context, jugadores)
Ronda.APUESTAS
}
jugador.punots += if (duplica) 2 * incremento else incremento
jugador.apuesta = 0
jugador.victoria = 0
}
duplica = false
StateSaver.savePocha(context, jugadores)
Ronda.APUESTAS
}
}) {
Text("Aceptar")
}
}) {
Text("Aceptar")
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/rodalc/amarracos/storage/UndoStack.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rodalc.amarracos.storage

class UndoStack<T> public constructor(private val maxCapacity: Int = 0) {
private var stack: MutableList<T> = mutableListOf()

fun push(element: T) {
if (maxCapacity == 0 || stack.size < maxCapacity) {
stack.add(element)
} else {
stack.removeFirst()
stack.add(element)
}
}

fun size(): Int {
return stack.size
}

fun pop(): T? {
return if (stack.isEmpty()) null else stack.removeLast()
}
}

0 comments on commit 5c546d1

Please sign in to comment.