Skip to content

Instantly share code, notes, and snippets.

@stone3311
Created October 10, 2015 20:49
Show Gist options
  • Select an option

  • Save stone3311/907176d4b29b15aa9fdc to your computer and use it in GitHub Desktop.

Select an option

Save stone3311/907176d4b29b15aa9fdc to your computer and use it in GitHub Desktop.
This is a simple Swift class representing a board
import Foundation
class Board {
// Board is quadratic
var boardSize: Int = 0
// The raw array
private var data: [[Int]] = []
// Initialize data array
init (boardsize: Int) {
for _ in 0...boardsize-1 {
var row: [Int] = []
for _ in 0...boardsize-1 {
row.append(0)
}
data.append(row)
}
self.boardSize = boardsize
}
// Check if x and y are valid
private func checkXY(x: Int, y: Int) -> Bool {
if x <= boardSize && y <= boardSize {
return true
} else {
return false
}
}
// Return field at x,y
func getField(x: Int, y: Int) -> Int {
let row = self.data[x]
return row[y]
}
// Set field data at x,y
func setField(data: Int, x: Int, y: Int) {
self.data [x] [y] = data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment