Skip to content

Instantly share code, notes, and snippets.

View BeauNouvelle's full-sized avatar
👨‍💻
Working

Beau Nouvelle BeauNouvelle

👨‍💻
Working
View GitHub Profile
public convenience init() {
let frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)
self.init(frame: frame)
}
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
}
public class WorldView: UIView {
var world: World = World(size: 100)
var cellSize: Int = 10
}
let world = World(size: 2)
dump(world.cells)
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)
}
}
public init(size: Int) {
self.size = size
}
public class World {
public var cells = [Cell]()
public let size: Int
}
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.
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.
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