From 9c96e8380f4206c318e5d277cad11909fc7868cc Mon Sep 17 00:00:00 2001 From: elioat Date: Thu, 20 Jun 2024 18:38:11 -0400 Subject: * --- swift/life/.vscode/launch.json | 28 ++++++++++++++++++++++++++++ swift/life/.vscode/settings.json | 4 ++++ swift/life/Sources/main.swift | 28 ++++++++++++++-------------- 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 swift/life/.vscode/launch.json create mode 100644 swift/life/.vscode/settings.json (limited to 'swift/life') 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..