Skip to content

Instantly share code, notes, and snippets.

@rayfix
Created July 28, 2016 02:30
Show Gist options
  • Select an option

  • Save rayfix/bfe8ca5654302d66f31c3a8609e2a9ef to your computer and use it in GitHub Desktop.

Select an option

Save rayfix/bfe8ca5654302d66f31c3a8609e2a9ef to your computer and use it in GitHub Desktop.
struct PokeCorrall {
let rows: Int, columns: Int
var grid: [Double]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: 0.0)
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> Double {
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment