Created
February 17, 2016 06:29
-
-
Save topher6345/186484361f619d36fc4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The game of life in Swift | |
//I have a huge crush on this language, I wish I could do more with it | |
// I'm looking this stuff up in the iBook, which has hilariously | |
// consequences when trying to copy/paste code snippets | |
import Cocoa | |
import XCPlayground | |
//Ugh, coming from my last project in VanillaJS, having a sweet array constructor rules!! | |
let yayArray = Array(count: 10, repeatedValue: 0) | |
// OooOO I can even add type annotations (drool) | |
let multiarray = [[Int]](count: 10, repeatedValue: [Int](count: 10, repeatedValue: 0)) | |
arc4random_uniform(2) | |
// Indexing is straightforward | |
enum State { | |
case Live, Dead | |
} | |
var state = State.Live | |
class Board { | |
var data: [[Int]] | |
let sizeData: Int | |
init(size:Int) { | |
sizeData = size | |
data = (0...size).map({ (_) -> [Int] in | |
(0...size).map({(i) -> Int in | |
return Int(arc4random_uniform(2)) | |
}) | |
}) | |
} | |
func neighborValues(x x:Int, y:Int)-> [Int] { | |
let locations = neighborLocations(x: x, y: y) | |
var result: Array<Int> = [] | |
for (rowIndex, _) in locations.enumerate(){ | |
for (colIndex, _) in locations[rowIndex].enumerate() { | |
if(rowIndex == 1 && colIndex == 1) { | |
} else { | |
result.append(data[rowIndex][colIndex]) | |
} | |
} | |
} | |
return result | |
} | |
func get(point: (Int,Int))-> Int { | |
let (x,y) = point | |
return data[abs(x % sizeData)][abs(y % sizeData)] | |
} | |
func set(x x:Int, y: Int, value: Int) { | |
data[abs(x % sizeData)][abs(y % sizeData)] = value | |
} | |
func neighborLocations(x x:Int, y:Int)-> [[(Int, Int)]] { | |
return [ | |
[(x-1,y-1),(x,y-1),(x+1,y-1)], | |
[(x-1,y),(x,y),(x+1,y)], | |
[(x-1,y+1),(x,y+1),(x+1,y+1)] | |
] | |
} | |
func liveNeighbors(x x:Int, y:Int)-> Int { | |
let neighbors = neighborLocations(x: x, y: y).flatMap { $0 } | |
var count = 0 | |
for neighbor in neighbors { | |
if(get(neighbor) == 1){ | |
count += 1 | |
} else { | |
} | |
} | |
return count | |
} | |
func update() { | |
var newBoard = (0...sizeData).map({ (_) -> [Int] in | |
(0...sizeData).map({(i) -> Int in | |
return 0 | |
}) | |
}) | |
for (rowIndex, _) in data.enumerate() { | |
for (colIndex, colValue) in data[rowIndex].enumerate() { | |
let neighbors = liveNeighbors(x: rowIndex, y: colIndex) | |
if(colValue == 1) { | |
if(neighbors < 2) { | |
newBoard[rowIndex][colIndex] = 0 | |
} else if(neighbors == 2 || neighbors == 3) { | |
newBoard[rowIndex][colIndex] = 1 | |
} else if(neighbors > 3){ | |
newBoard[rowIndex][colIndex] = 0 | |
} | |
} else { | |
if(neighbors == 3) { | |
newBoard[rowIndex][colIndex] = 1 | |
} else { | |
newBoard[rowIndex][colIndex] = 0 | |
} | |
} | |
} | |
} | |
data = newBoard | |
} | |
func setLocations(locations:[(Int, Int)], value: Int){ | |
for location in locations { | |
let (x,y) = location | |
data[x % sizeData][y % sizeData] = value | |
} | |
} | |
} | |
var board = Board(size: 16) | |
board.data | |
board.update() | |
board.data | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment