Skip to content

Commit

Permalink
removeLast bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Feb 7, 2025
1 parent c454e21 commit 726e704
Showing 1 changed file with 41 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ class GameState(

isGameStarted.value = true

if (firstPlayerPolicy.value == FirstPlayerPolicy.DiceRolling)
if (firstPlayerPolicy.value == FirstPlayerPolicy.DiceRolling) {
dummyDiceRolling()
}

if (isAiTurnToPlay())
if (isAiTurnToPlay()) {
scope.launch { playCellByAi() }
}
}
}

Expand Down Expand Up @@ -135,14 +137,17 @@ class GameState(
changeCellOwner(cell)
checkIfGameIsFinished()

if (isAiTurnToPlay())
if (isAiTurnToPlay()) {
scope.launch { asyncPlayCellByAi() }
}
}

private fun playCellByAi() {
checkIfGameIsFinished()
val cell = gameLogic?.ai?.play()
if (cell != null) changeCellOwner(cell)
if (cell != null) {
changeCellOwner(cell)
}
checkIfGameIsFinished()
}

Expand Down Expand Up @@ -212,9 +217,11 @@ class GameState(
secondPlayerName
)

if (firstPlayerPolicy.value == FirstPlayerPolicy.DiceRolling)
if (firstPlayerPolicy.value == FirstPlayerPolicy.DiceRolling) {
setFirstPlayerToDiceWinner()
else currentPlayer.value = players.value.first()
} else {
currentPlayer.value = players.value.first()
}
}

private fun setFirstPlayerToDiceWinner() {
Expand Down Expand Up @@ -251,10 +258,12 @@ class GameState(
}

private suspend fun dummyDiceRolling() {
if (isSoundOn)
if (isSoundOn) {
MediaPlayer.create(context, R.raw.dice).start()
if (isVibrationOn)
}
if (isVibrationOn) {
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
}
isRollingDices.value = true

val firstPlayerDice = players.value.first().diceIndex
Expand All @@ -279,23 +288,29 @@ class GameState(

fun getOwnerShape(
owner: Player?
): Shape {
return if (owner == players.value.first()) owner.shape?.toShape() ?: XShape
else owner?.shape?.toShape() ?: RingShape
) = if (owner == players.value.first()) {
owner.shape?.toShape() ?: XShape
} else {
owner?.shape?.toShape() ?: RingShape
}

private fun changePlayer() {
if (currentPlayer.value == players.value.first()) currentPlayer.value = players.value.last()
else currentPlayer.value = players.value.first()
if (currentPlayer.value == players.value.first()) {
currentPlayer.value = players.value.last()
} else {
currentPlayer.value = players.value.first()
}
}

private fun changeCellOwner(
cell: DoozCell
) {
if (isVibrationOn)
if (isVibrationOn) {
hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress)
if (isSoundOn)
}
if (isSoundOn) {
MediaPlayer.create(context, R.raw.pencil).start()
}

if (cell.owner == null && isGameStarted.value) {
lastPlayedCells.value = buildList {
Expand All @@ -309,10 +324,12 @@ class GameState(

private fun checkIfGameIsFinished() {
winner.value = findWinner()
if (winner.value != null)
if (winner.value != null) {
finishGame()
if (gameLogic?.isGameDrew() == true)
}
if (gameLogic?.isGameDrew() == true) {
handleDrewGame()
}
}

private fun handleDrewGame() {
Expand Down Expand Up @@ -342,7 +359,7 @@ class GameState(

lastPlayedCells.value = buildList {
val new = lastPlayedCells.value.toMutableList()
new.removeLast()
new.remove(new.lastOrNull())
addAll(new)
}
prepareGameLogic()
Expand All @@ -351,19 +368,22 @@ class GameState(
isGameDrew.value = false
winnerCells.value = emptyList()

if (isAiTurnToPlay())
if (isAiTurnToPlay()) {
playCellByAi()
}

if (gamePlayersType.value == GamePlayersType.PvP)
if (gamePlayersType.value == GamePlayersType.PvP) {
changePlayer()
}

if (lastPlayedCells.value.isEmpty()) {
val first = players.value.first()
val second = players.value.last()

currentPlayer.value = if (first.diceIndex > second.diceIndex) first else second
if (isAiTurnToPlay())
if (isAiTurnToPlay()) {
playCellByAi()
}
}
}
}
Expand Down

0 comments on commit 726e704

Please sign in to comment.