Created
October 10, 2015 20:49
-
-
Save stone3311/907176d4b29b15aa9fdc to your computer and use it in GitHub Desktop.
This is a simple Swift class representing a board
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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