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
| let firstCell = Cell(x: 10, y: 10, state: .dead) | |
| let secondCell = Cell(x: 11, y: 10, state: .dead) | |
| print(firstCell.isNeighbor(to: secondCell)) | |
| // true - Same row, adjacent column. | |
| let firstCell = Cell(x: 10, y: 10, state: .dead) | |
| let secondCell = Cell(x: 20, y: 10, state: .dead) | |
| print(firstCell.isNeighbor(to: secondCell)) | |
| // false - Same row, vastly different column. |
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
| public class World { | |
| public var cells = [Cell]() | |
| public let size: Int | |
| } |
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
| public init(size: Int) { | |
| self.size = size | |
| } |
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
| for x in 0..<size { | |
| for y in 0..<size { | |
| let randomState = arc4random_uniform(3) | |
| let cell = Cell(x: x, y: y, state: randomState == 0 ? .alive : .dead) | |
| cells.append(cell) | |
| } | |
| } |
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
| let world = World(size: 2) | |
| dump(world.cells) |
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
| public class WorldView: UIView { | |
| var world: World = World(size: 100) | |
| var cellSize: Int = 10 | |
| } |
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
| public convenience init(worldSize: Int, cellSize: Int) { | |
| let frame = CGRect(x: 0, y: 0, width: worldSize * cellSize, height: worldSize * cellSize) | |
| self.init(frame: frame) | |
| self.world = World(size: worldSize) | |
| self.cellSize = cellSize | |
| } |
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
| public convenience init() { | |
| let frame = CGRect(x: 0, y: 0, width: 1000, height: 1000) | |
| self.init(frame: frame) | |
| } |
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
| public required init?(coder aDecoder: NSCoder) { | |
| fatalError("Not implemented") | |
| } |
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
| public override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| } |