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 autoRun() { | |
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { | |
self.world.updateCells() | |
self.setNeedsDisplay() | |
self.autoRun() | |
} | |
} |
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 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
world.updateCells() | |
setNeedsDisplay() | |
} |
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
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) | |
} |
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
default: | |
let deadCell = Cell(x: cell.x, y: cell.y, state: .dead) | |
updatedCells.append(deadCell) |
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
case 3 where cell.state == .dead: | |
let liveCell = Cell(x: cell.x, y: cell.y, state: .alive) | |
updatedCells.append(liveCell) |
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 livingNeighbors = liveCells.filter { $0.isNeighbor(to: 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
switch livingNeighbors.count { | |
case 2...3 where cell.state == .alive: | |
updatedCells.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
var updatedCells = [Cell]() | |
let liveCells = cells.filter { $0.state == .alive } | |
for cell in cells { | |
} | |
cells = updatedCells |
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 view = WorldView(worldSize: 10, cellSize: 10) | |
PlaygroundPage.current.liveView = view |
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 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) | |
} |