diff options
author | elioat <elioat@tilde.institute> | 2024-06-20 18:38:11 -0400 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-06-20 18:38:11 -0400 |
commit | 9c96e8380f4206c318e5d277cad11909fc7868cc (patch) | |
tree | e8cec5789607ae0491f72e7a62d91474b07ef790 /swift/life | |
parent | 05a1adcbedb43920074c1854338d6ffc635093f8 (diff) | |
download | tour-9c96e8380f4206c318e5d277cad11909fc7868cc.tar.gz |
*
Diffstat (limited to 'swift/life')
-rw-r--r-- | swift/life/.vscode/launch.json | 28 | ||||
-rw-r--r-- | swift/life/.vscode/settings.json | 4 | ||||
-rw-r--r-- | swift/life/Sources/main.swift | 28 |
3 files changed, 46 insertions, 14 deletions
diff --git a/swift/life/.vscode/launch.json b/swift/life/.vscode/launch.json new file mode 100644 index 0000000..9d0e87c --- /dev/null +++ b/swift/life/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + "configurations": [ + { + "type": "lldb", + "request": "launch", + "sourceLanguages": [ + "swift" + ], + "args": [], + "cwd": "${workspaceFolder:life}", + "name": "Debug life", + "program": "${workspaceFolder:life}/.build/debug/life", + "preLaunchTask": "swift: Build Debug life" + }, + { + "type": "lldb", + "request": "launch", + "sourceLanguages": [ + "swift" + ], + "args": [], + "cwd": "${workspaceFolder:life}", + "name": "Release life", + "program": "${workspaceFolder:life}/.build/release/life", + "preLaunchTask": "swift: Build Release life" + } + ] +} \ No newline at end of file diff --git a/swift/life/.vscode/settings.json b/swift/life/.vscode/settings.json new file mode 100644 index 0000000..bf01c5d --- /dev/null +++ b/swift/life/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "lldb.library": "/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Versions/A/LLDB", + "lldb.launch.expressions": "native" +} \ No newline at end of file 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 |