This file contains 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 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 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 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 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 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 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 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 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: 11, state: .dead) | |
print(firstCell.isNeighbor(to: secondCell)) | |
// true - Adjacent row, adjacent column. |
This file contains 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 func isNeighbor(to cell: Cell) -> Bool { | |
let xDelta = abs(self.x - cell.x) | |
let yDelta = abs(self.y - cell.y) | |
switch (xDelta, yDelta) { | |
case (1, 1), (0, 1), (1, 0): | |
return true | |
default: | |
return false |