about summary refs log tree commit diff stats
path: root/swift/life/Sources
diff options
context:
space:
mode:
Diffstat (limited to 'swift/life/Sources')
-rw-r--r--swift/life/Sources/main.swift50
1 files changed, 39 insertions, 11 deletions
diff --git a/swift/life/Sources/main.swift b/swift/life/Sources/main.swift
index 4b3e7a6..e010b94 100644
--- a/swift/life/Sources/main.swift
+++ b/swift/life/Sources/main.swift
@@ -2,7 +2,6 @@ import Foundation
 
 let rows = 10
 let cols = 10
-var board = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 
 func printBoard(_ board: [[Int]]) {
     board.forEach { row in
@@ -40,14 +39,43 @@ func nextGeneration(_ currentBoard: [[Int]]) -> [[Int]] {
     }
 }
 
-board[1][2] = 1
-board[2][3] = 1
-board[3][1] = 1
-board[3][2] = 1
-board[3][3] = 1
-
-for _ in 0..<10 {
-    printBoard(board)
-    board = nextGeneration(board)
-    sleep(1) // FIXME: figure out how to sleep for periods shorter than a full second
+func simulate(board: [[Int]], generations: Int) {
+    var currentBoard = board
+    for _ in 0..<generations {
+        printBoard(currentBoard)
+        currentBoard = nextGeneration(currentBoard)
+        sleep(1) // FIXME: figure out how to sleep for periods shorter than a full second
+    }
 }
+
+var glider = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+glider[1][2] = 1
+glider[2][3] = 1
+glider[3][1] = 1
+glider[3][2] = 1
+glider[3][3] = 1
+simulate(board: glider, generations: 10)
+
+var flasher = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+flasher[4][5] = 1
+flasher[5][5] = 1
+flasher[6][5] = 1
+simulate(board: flasher, generations: 4)
+
+var beacon = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+beacon[2][2] = 1
+beacon[2][3] = 1
+beacon[3][2] = 1
+beacon[4][5] = 1
+beacon[5][4] = 1
+beacon[5][5] = 1
+simulate(board: beacon, generations: 4)
+
+var toad = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+toad[3][4] = 1
+toad[3][5] = 1
+toad[3][6] = 1
+toad[4][3] = 1
+toad[4][4] = 1
+toad[4][5] = 1
+simulate(board: toad, generations: 5)