Итак, я делаю приложение Sudoku, и я пытаюсь реализовать проверку уникальности, поэтому я могу быть абсолютно уверен, что в плате Sudoku есть только одно
import Foundation
func generateSudokuBoard(difficulty: Difficulty) -> ([[Int]], [[Int]]) {
var board = Array(repeating: Array(repeating: 0, count: 9), count: 9)
fillDiagonal(&board)
_ = fillRemaining(&board, row: 0, col: 3)
let solution = board.map { $0 } // copy full solution
let prefilled: Int
switch difficulty {
case .beginner:
prefilled = Int.random(in: 36...49)
case .intermediate:
prefilled = Int.random(in: 30...35)
case .expert:
prefilled = Int.random(in: 22...29)
case .daily:
prefilled = 0
}
let blanks = 81 - prefilled
removeDigits(&board, count: blanks)
print("Generated \(difficulty.rawValue.capitalized) puzzle with \(prefilled) pre-filled cells and \(blanks) blanks")
return (board, solution)
}
// MARK: - Board Fill
func fillDiagonal(_ board: inout [[Int]]) {
for i in stride(from: 0, to: 9, by: 3) {
fillBox(&board, row: i, col: i)
}
}
func fillBox(_ board: inout [[Int]], row: Int, col: Int) {
var nums = Array(1...9).shuffled()
for i in 0.. Int {
for row in 0..
Подробнее здесь: https://stackoverflow.com/questions/797 ... ness-check