# Android 我正在创建一个简单的连接四个游戏，我需要一个弹出式窗口当你赢了

 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 (winMessageState.isVisible.value) {
onDismissRequest = { winMessageState.isVisible.value = false },
confirmButton = {
Button(onClick = { winMessageState.isVisible.value = false }) {
Text("Close")
}
},
text = {
Text(text = "You won!")
},
)
}