func updateBoard(_ board: [[Character]], _ click: [Int]) -> [[Character]] {
var board = board
if board[click[0]][click[1]] == "M" {
board[click[0]][click[1]] = "X"
return board
}
helper(&board, click[0], click[1])
return board
}
private func helper(_ board: inout [[Character]], _ i: Int, _ j: Int) {
if i < 0 || i >= board.count || j < 0 || j >= board[i].count || board[i][j] != "E" {
return
}
var count = 0
if i-1 >= 0 && j-1 >= 0 && board[i-1][j-1] == "M" {
count += 1
}
if i-1 >= 0 && board[i-1][j] == "M" {
count += 1
}
if i-1 >= 0 && j+1 < board[i].count && board[i-1][j+1] == "M" {
count += 1
}
if j-1 >= 0 && board[i][j-1] == "M" {
count += 1
}
if j+1 < board[i].count && board[i][j+1] == "M" {
count += 1
}
if i+1 < board.count && j-1 >= 0 && board[i+1][j-1] == "M" {
count += 1
}
if i+1 < board.count && board[i+1][j] == "M" {
count += 1
}
if i+1 < board.count && j+1 < board[i].count && board[i+1][j+1] == "M" {
count += 1
}
if count == 0 {
board[i][j] = "B"
helper(&board, i-1, j-1)
helper(&board, i-1, j)
helper(&board, i-1, j+1)
helper(&board, i, j-1)
helper(&board, i, j+1)
helper(&board, i+1, j-1)
helper(&board, i+1, j)
helper(&board, i+1, j+1)
} else {
board[i][j] = Character("\(count)")
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme