diff options
-rw-r--r-- | swift/life/Sources/main.swift | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/swift/life/Sources/main.swift b/swift/life/Sources/main.swift index 72b1b49..3bd47cd 100644 --- a/swift/life/Sources/main.swift +++ b/swift/life/Sources/main.swift @@ -16,7 +16,7 @@ func printBoard(_ board: [[Int]]) { // 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)] + let directions: [(Int, Int)] = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)] // Iterate over the directions and count live neighbors return directions.reduce(0) { count, dir in @@ -104,4 +104,13 @@ rpentomino[3][5] = 1 rpentomino[4][3] = 1 rpentomino[4][4] = 1 rpentomino[5][4] = 1 -simulate(board: rpentomino, generations: 22) \ No newline at end of file +simulate(board: rpentomino, generations: 22) + +func randomBoard() -> [[Int]] { + return (0..<rows).map { _ in + return (0..<cols).map { _ in + return Int.random(in: 0...1) + } + } +} +simulate(board: randomBoard(), generations: 10) \ No newline at end of file |