这是我的游戏获胜逻辑的代码,我做了它,使它可以水平,垂直和对角.代码可能是错的,但我就是这么做的.我正在试着做一个对话,当你赢了,你赢了,当然,我不知道怎么做,聊天GPT提供的代码总是错误的,或者让我的应用程序崩溃:

 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
        }
    }
}

推荐答案

您所需要做的就是添加一个包含在if中的AlertDialog语句,该语句判断您的组合中的winMessageState.isVisible.value.有关AlertDialog的更多信息,请查看其documentation.简单的例子:

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

Android相关问答推荐

无法从API访问项目详细信息

NativeScript在`ns run android`上重复Kotlin类

无法在Android中创建通知频道

使用Kotlin/Compose与Java/XML指南的比较

安卓喷气背包组成倒计时动画

使用RecycleView和Diffutils时从列表中删除项目时出现问题

为什么 Android Compose 将片段作为参数传递给 Composables 函数?

React-Native Manifest 合并失败并出现多个错误

如何在C++中使用JNI_GetCreatedJavaVMs调用Java代码

如何将DrawableId参数传递给XML布局?

是否可以按照干净的体系 struct 将活动实例传递给视图模型

组成不重叠的元素

您如何衡量条形图的 4 个类别?

为 AlertDialog 的消息文本设置自定义字体

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

如何将新的 ComponentActivity 与 ViewBinding 和其他旧的 AppCompatActivity 组件一起使用

Сan 无法从 Firestore 获取数据,没有错误

Android Studio (Kotlin):无法启动活动

Android REST 客户端,示例?

了解 Canvas 和 Surface 概念