about summary refs log tree commit diff stats
path: root/swift/life/Sources
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-06-20 18:38:11 -0400
committerelioat <elioat@tilde.institute>2024-06-20 18:38:11 -0400
commit9c96e8380f4206c318e5d277cad11909fc7868cc (patch)
treee8cec5789607ae0491f72e7a62d91474b07ef790 /swift/life/Sources
parent05a1adcbedb43920074c1854338d6ffc635093f8 (diff)
downloadtour-9c96e8380f4206c318e5d277cad11909fc7868cc.tar.gz
*
Diffstat (limited to 'swift/life/Sources')
-rw-r--r--swift/life/Sources/main.swift28
1 files changed, 14 insertions, 14 deletions
diff --git a/swift/life/Sources/main.swift b/swift/life/Sources/main.swift
index 3b9f74a..72b1b49 100644
--- a/swift/life/Sources/main.swift
+++ b/swift/life/Sources/main.swift
@@ -1,7 +1,7 @@
 import Foundation
 
-let rows = 10
-let cols = 10
+let rows: Int = 10
+let cols: Int = 10
 
 func printBoard(_ board: [[Int]]) {
     board.forEach { row in
@@ -13,7 +13,7 @@ func printBoard(_ board: [[Int]]) {
     print("\n")
 }
 
-// are these comments enough for a blog post?
+// Are these comments enough for a blog post?
 func countLiveNeighbors(_ board: [[Int]], x: Int, y: Int) -> Int {
     // All of the possible directions to check for live neighbors
     let directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
@@ -21,8 +21,8 @@ func countLiveNeighbors(_ board: [[Int]], x: Int, y: Int) -> Int {
     // Iterate over the directions and count live neighbors
     return directions.reduce(0) { count, dir in
         // Calculate the coordinates of the neighbor cell
-        let newX = x + dir.0
-        let newY = y + dir.1
+        let newX: Int = x + dir.0
+        let newY: Int = y + dir.1
         
         // Check if the neighbor cell is within the bounds of the board
         if newX >= 0 && newX < rows && newY >= 0 && newY < cols {
@@ -37,10 +37,10 @@ func countLiveNeighbors(_ board: [[Int]], x: Int, y: Int) -> Int {
 
 func nextGeneration(_ currentBoard: [[Int]]) -> [[Int]] {
     // Iterate over each cell in the current board
-    return currentBoard.enumerated().map { (x, row) in
-        return row.enumerated().map { (y, cell) in
+    return currentBoard.enumerated().map { (x: Int, row: [Int]) in
+        return row.enumerated().map { (y: Int, cell: Int) in
             // Count the number of live neighbors for the current cell
-            let liveNeighbors = countLiveNeighbors(currentBoard, x: x, y: y)
+            let liveNeighbors: Int = countLiveNeighbors(currentBoard, x: x, y: y)
             
             // Determine the next state of the cell
             if cell == 1 && (liveNeighbors < 2 || liveNeighbors > 3) {
@@ -58,7 +58,7 @@ func nextGeneration(_ currentBoard: [[Int]]) -> [[Int]] {
 }
 
 func simulate(board: [[Int]], generations: Int) {
-    var currentBoard = board
+    var currentBoard: [[Int]] = board
     for _ in 0..<generations {
         printBoard(currentBoard)
         currentBoard = nextGeneration(currentBoard)
@@ -66,7 +66,7 @@ func simulate(board: [[Int]], generations: Int) {
     }
 }
 
-var glider = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+var glider: [[Int]] = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 glider[1][2] = 1
 glider[2][3] = 1
 glider[3][1] = 1
@@ -74,13 +74,13 @@ glider[3][2] = 1
 glider[3][3] = 1
 simulate(board: glider, generations: 10)
 
-var flasher = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+var flasher: [[Int]] = 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)
+var beacon: [[Int]] = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 beacon[2][2] = 1
 beacon[2][3] = 1
 beacon[3][2] = 1
@@ -89,7 +89,7 @@ beacon[5][4] = 1
 beacon[5][5] = 1
 simulate(board: beacon, generations: 4)
 
-var toad = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+var toad: [[Int]] = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 toad[3][4] = 1
 toad[3][5] = 1
 toad[3][6] = 1
@@ -98,7 +98,7 @@ toad[4][4] = 1
 toad[4][5] = 1
 simulate(board: toad, generations: 5)
 
-var rpentomino = Array(repeating: Array(repeating: 0, count: cols), count: rows)
+var rpentomino: [[Int]] = Array(repeating: Array(repeating: 0, count: cols), count: rows)
 rpentomino[3][4] = 1
 rpentomino[3][5] = 1
 rpentomino[4][3] = 1