Skip to content

Instantly share code, notes, and snippets.

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

Beau Nouvelle BeauNouvelle

👨‍💻
Working
View GitHub Profile
public func autoRun() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
self.world.updateCells()
self.setNeedsDisplay()
self.autoRun()
}
}
public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
world.updateCells()
setNeedsDisplay()
}
switch livingNeighbors.count {
case 2...3 where cell.state == .alive:
updatedCells.append(cell)
case 3 where cell.state == .dead:
let liveCell = Cell(x: cell.x, y: cell.y, state: .alive)
updatedCells.append(liveCell)
default:
let deadCell = Cell(x: cell.x, y: cell.y, state: .dead)
updatedCells.append(deadCell)
}
default:
let deadCell = Cell(x: cell.x, y: cell.y, state: .dead)
updatedCells.append(deadCell)
case 3 where cell.state == .dead:
let liveCell = Cell(x: cell.x, y: cell.y, state: .alive)
updatedCells.append(liveCell)
let livingNeighbors = liveCells.filter { $0.isNeighbor(to: cell) }
switch livingNeighbors.count {
case 2...3 where cell.state == .alive:
updatedCells.append(cell)
}
var updatedCells = [Cell]()
let liveCells = cells.filter { $0.state == .alive }
for cell in cells {
}
cells = updatedCells
let view = WorldView(worldSize: 10, cellSize: 10)
PlaygroundPage.current.liveView = view
for cell in world.cells {
let rect = CGRect(x: cell.x * cellSize, y: cell.y * cellSize, width: cellSize, height: cellSize)
let color = cell.state == .alive ? UIColor.green.cgColor : UIColor.white.cgColor
context?.addRect(rect)
context?.setFillColor(color)
context?.fill(rect)
}