androidkotlinandroid-jetpack-composeandroid-jetpackwordpress-jetpack

I'm creating an simple connect four game and i need an "pop up" when you win


Here is my code for the win logic for the game, i have made it so it can look horizontally, vertically and diagonally. The code can be wrong but this is how i made it. I am trying to make a dialog where it comes up that you won when you win of course, i dont know how to make it and the code Chat gpt provides is always wrong or makes my app crash:

 fun checkForWin(board: List<List<Color>>, colorToCheck: Color): Boolean {
    for (boardHeight in 0 until board.size) {
        for (boardWidth in 0 until board[0].size) {
            // Check horizontally
            if (boardWidth + 3 < board[0].size &&
                board[boardHeight][boardWidth] == colorToCheck &&
                board[boardHeight][boardWidth + 1] == colorToCheck &&
                board[boardHeight][boardWidth + 2] == colorToCheck &&
                board[boardHeight][boardWidth + 3] == colorToCheck
            ) {
                return true
            }

            // Check vertically
            if (boardHeight + 3 < board.size &&
                board[boardHeight][boardWidth] == colorToCheck &&
                board[boardHeight + 1][boardWidth] == colorToCheck &&
                board[boardHeight + 2][boardWidth] == colorToCheck &&
                board[boardHeight + 3][boardWidth] == colorToCheck
            ) {
                return true
            }

            // Check diagonally (from top-left to bottom-right)
            if (boardHeight + 3 < board.size && boardWidth + 3 < board[0].size &&
                board[boardHeight][boardWidth] == colorToCheck &&
                board[boardHeight + 1][boardWidth + 1] == colorToCheck &&
                board[boardHeight + 2][boardWidth + 2] == colorToCheck &&
                board[boardHeight + 3][boardWidth + 3] == colorToCheck
            ) {
                return true
            }

            // Check diagonally (from bottom-left to top-right)
            if (boardHeight - 3 >= 0 && boardWidth + 3 < board[0].size &&
                board[boardHeight][boardWidth] == colorToCheck &&
                board[boardHeight - 1][boardWidth + 1] == colorToCheck &&
                board[boardHeight - 2][boardWidth + 2] == colorToCheck &&
                board[boardHeight - 3][boardWidth + 3] == colorToCheck
            ) {
                return true
            }
        }
    }
    return false
}

fun handleCellClick(boardWidth: Int) {
    val emptyRow = findLowestEmpty(_state.value.board, boardWidth)
    if (emptyRow != -1) {
        val updateBoard = _state.value.board.map { it.toMutableList().toList() }.toMutableList()
        updateBoard[emptyRow] =
            updateBoard[emptyRow].toMutableList().apply {
                set(boardWidth, if (_state.value.isRedTurn) Color.Red else Color.Yellow)
            }.toList()
        _state.value = _state.value.copy(board = updateBoard, isRedTurn = !_state.value.isRedTurn)

        val currentColor = if (_state.value.isRedTurn) Color.Red else Color.Yellow
        if (checkForWin(updateBoard, currentColor)) {
            winMessageState.winnerColor.value = currentColor
            winMessageState.isVisible.value = true
        }
    }
}

Solution

  • All you need to do is add an AlertDialog enclosed in if statement which checks winMessageState.isVisible.value in your composable. For more information on AlertDialog check its documentation. Simple example:

        if (winMessageState.isVisible.value) {
            AlertDialog(
                onDismissRequest = { winMessageState.isVisible.value = false },
                confirmButton = {
                    Button(onClick = { winMessageState.isVisible.value = false }) {
                        Text("Close")
                    }
                },
                text = {
                    Text(text = "You won!")
                },
            )
        }