(I'm pretty new to this, so tips on the general form of this post and my code are greatly appreciated!)
I've been playing around with Swift in the IBM Sandbox, and I can't seem to get around the following problem:
func fillPossibilityMatrix() { //it's a 9x9 Matrix
for i in 0...80 {
let row = (i - (i % 9)) / 9 //-> row is Valid for 0 - 8
let column = i % 9
if possibilityMatrix[row, column] == [0] {
possibilityMatrix[row, column] = possibilities(row, column: column)
}
}
This gives me 132 unkown error!
Even though I can call possibilityMatrix and possibilities() with every combination of values used here, as soon as I put "=" in between them, things get weird.
I've seen the 132 error before, when I tried to assign Values to invalid indexes of Arrays/Matrixes, but I don't see that here...
- The following works perfectly fine. (Note the "prints" instead of the "=")
func fillPossibilityMatrix() {
for i in 0...80 {
let row = (i - (i % 9)) / 9
let column = i % 9
if possibilityMatrix[row, column] == [0] {
print(possibilityMatrix[row, column])
print(possibilities(row, column: column))
}
}
}
- It also works, when I put different Ranges for the Loop. But it's neither certain values being used nor the size of the Range, that determines whether it does or not.
Whats wrong here? Am I just being stupid? Is this specific to the IBM site?
The Rest
(I'm trying to let it solve Sudoku)
-
possibilityMatrix comes about like this: (here: field <-> possibilityMatrix)
struct Matrix {
let rows: Int, columns: Int
var grid: [[Int]]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: [0])
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> [Int] {
get {
assert(indexIsValidForRow(row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValidForRow(row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)
for char in input.characters {
inputArray.append(Int(String(char)))
}
func fromInputToField() {
for i in 0..<inputArray.count {
let row = (i - (i % 9))/9
let column = i % 9
field[row, column][0] = (inputArray[i])
}
}
fromInputToField()
var possibilityMatrix = field
-
possibilities() and it's sub functions look like this:
func possibilities(row: Int, column: Int) -> [Int] {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return numbers.filter {
!rowContains(row, number: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}
func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}
func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
if r % 3 != 0 {
return locateBox(r - 1, c: c)
}
if c % 3 != 0 {
return locateBox(r, c: c - 1)
}
return (r, c)
}
for copy-pasting
struct Matrix {
let rows: Int, columns: Int
var grid: [[Int]]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: [0])
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> [Int] {
get {
assert(indexIsValidForRow(row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValidForRow(row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)
for char in input.characters {
inputArray.append(Int(String(char)))
}
func fromInputToField() {
for i in 0..<inputArray.count {
let row = (i - (i % 9))/9
let column = i % 9
field[row, column][0] = (inputArray[i])
}
}
fromInputToField()
var possibilityMatrix = field
func possibilities(row: Int, column: Int) -> [Int] {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return numbers.filter {
!rowContains(row, number: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}
func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}
func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
if r % 3 != 0 {
return locateBox(r - 1, c: c)
}
if c % 3 != 0 {
return locateBox(r, c: c - 1)
}
return (r, c)
}
func fillPossibilityMatrix() { //it's a 9x9 Matrix
for i in 0...80 {
let row = (i - (i % 9)) / 9 //-> row is Valid for 0 - 8
let column = i % 9
if possibilityMatrix[row, column] == [0] {
possibilityMatrix[row, column] = possibilities(row, column: column)
}
}
}
fillPossibilityMatrix()