Last active
July 20, 2017 14:00
-
-
Save ntoonio/6b69cfc855024da8561203623c34d54a to your computer and use it in GitHub Desktop.
Revisions
-
ntoonio revised this gist
Nov 29, 2016 . 2 changed files with 177 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ <html> <head> </head> <body> <pre id="output"> </pre> <script src="GameOfLife.js"></script> </body> </html> 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,165 @@ var grid = [{ "x": 4, "y": 9 }, { "x": 5, "y": 9 }, { "x": 6, "y": 9}, { "x": 6, "y": 8}, { "x": 6, "y": 7}, { "x": 5, "y": 7}, { "x": 4, "y": 7}, { "x": 4, "y": 8 }] function sleep (time) { return new Promise((resolve) => setTimeout(resolve, time)); } function filterArray(src, filt) { // Det här är fan genialt, tack so var temp = {}, i, result = []; for (i = 0; i < filt.length; i++) { temp[filt[i].x + " " + filt[i].y] = true; } for (i = 0; i < src.length; i++) { if (!(src[i].x + " " + src[i].y in temp)) { result.push(src[i]); } } return(result); } function getBounds() { var maxX = 0 var maxY = 0 var minX = grid[0].x var minY = grid[0].y for (var i = 0; i < grid.length; i++) { if (grid[i].x > maxX) { maxX = grid[i].x } else if (grid[i].x < minX) { minX = grid[i].x } if (grid[i].y > maxY) { maxY = grid[i].y } else if (grid[i].y < minY) { minY = grid[i].y } } return { "first": { "x": minX - 1, "y": maxY + 1 }, "second": { "x": maxX + 1, "y": minY - 1 } } } function forCellInBounds(bounds, cell) { var x = bounds.first.x while (x <= bounds.second.x) { var y = bounds.second.y while (y <= bounds.first.y) { cell({"x": x, "y": y}) y++ } x++ } } function isAlive(pos) { for (var i = 0; i < grid.length; i++) { if (grid[i].x == pos.x && grid[i].y == pos.y) { return true } } return false } function getNeigbors(cell) { var neighbors = 0 const bounds = { "first": { "x": cell.x - 1, "y": cell.y + 1 }, "second": { "x": cell.x + 1, "y": cell.y - 1 } } forCellInBounds(bounds, function(pos) { const isThisCell = pos.x == cell.x && pos.y == cell.y if (isAlive(pos) && !isThisCell) { neighbors++ } }) return neighbors } function step() { const bounds = getBounds() var deadCells = [] var liveCells = [] forCellInBounds(bounds, function(pos) { const neighbors = getNeigbors(pos) if (isAlive(pos)) { if (neighbors < 2) { // Die deadCells.push(pos) } else if (neighbors == 2 || neighbors == 3) { // Survive // Do nothing } else if (neighbors > 3) { // Die deadCells.push(pos) } } else { if (neighbors == 3) { // Birth liveCells.push(pos) } } }) // Remove new dead cells grid = filterArray(grid, deadCells) // Add new living cells grid = grid.concat(liveCells) // Temporary printing method display() sleep(2000).then(() => { step() }); } function display() { var column = 0 document.getElementById("output").innerHTML = "" forCellInBounds(getBounds(), function(pos) { if (column != pos.x) { column = pos.x document.getElementById("output").innerHTML += "<br>" } if (isAlive(pos)) { document.getElementById("output").innerHTML += "X" } else { document.getElementById("output").innerHTML += " " } }) } step() -
ntoonio revised this gist
Nov 21, 2016 . 1 changed file with 20 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -116,17 +116,16 @@ func getNeighbors(cell: Position) -> Int { forCellInBounds(bounds: bounds) { (pos: Position) in if isAlive(pos: pos), pos != cell { neighbors += 1 } } return neighbors } while true { let bounds = getBounds() var deadCells: [Position] = [] var liveCells: [Position] = [] @@ -159,8 +158,25 @@ while false { } // Add cells grid.append(contentsOf: liveCells) var row = 0 forCellInBounds(bounds: bounds) { (pos: Position) in if row != pos.x { row = pos.x print("") } //X // if isAlive(pos: pos) { print("X", terminator: "") } else { print(" ", terminator: "") } } print("") sleep(1) } -
ntoonio created this gist
Nov 21, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,166 @@ import Foundation struct Position { var x = 0 var y = 0 init(x: Int, y: Int) { self.x = x self.y = y } static func ==(lhs: Position, rhs: Any) -> Bool { if rhs is (Int, Int) { let rhs = rhs as! (Int, Int) if lhs.x == rhs.0, lhs.y == rhs.1 { return true } else { return false } } else if rhs is Position { let rhs = rhs as! Position if lhs.x == rhs.x, lhs.y == rhs.y { return true } else { return false } } return false } static func !=(lhs: Position, rhs: Any) -> Bool { if rhs is (Int, Int) { let rhs = rhs as! (Int, Int) if lhs.x != rhs.0 || lhs.y != rhs.1 { return true } else { return false } } else if rhs is Position { let rhs = rhs as! Position if lhs.x != rhs.x || lhs.y != rhs.y { return true } else { return false } } return false } } var grid = [Position(x: 4, y: 9), Position(x: 5, y: 9), Position(x: 6, y: 9), Position(x: 6, y: 8), Position(x: 6, y: 7), Position(x: 5, y: 7), Position(x: 4, y: 7), Position(x: 4, y: 8)] func getBounds() -> ((Position), (Position)) { var maxX = 0 var maxY = 0 var minX = grid[0].x // So it will return the max value, var minY = grid[0].y // in case that didn't make sense for cell in grid { if cell.x > maxX { maxX = cell.x } else if cell.x < minX { minX = cell.x } if cell.y > maxY { maxY = cell.y } else if cell.y < minY { minY = cell.y } } return (Position(x: minX - 1, y: maxY + 1), Position(x: maxX + 1, y: minY - 1)) } func forCellInBounds(bounds: (Position, Position), cellBlock: (_ pos: Position) -> ()) { var x = bounds.0.x while x <= bounds.1.x { var y = bounds.1.y while y <= bounds.0.y { cellBlock(Position(x: x, y: y)) y += 1 } x += 1 } } func isAlive(pos: Position) -> Bool { for cell in grid { if Position(x: cell.x, y: cell.y) == pos { return true } } return false } func getNeighbors(cell: Position) -> Int { var neighbors = 0 let bounds = (Position(x: cell.x - 1, y: cell.y + 1), Position(x: cell.x + 1, y: cell.y - 1)) forCellInBounds(bounds: bounds) { (pos: Position) in if isAlive(pos: pos), pos != cell { print(pos) neighbors += 1 } } return neighbors } while false { let bounds = getBounds() print(bounds) var deadCells: [Position] = [] var liveCells: [Position] = [] forCellInBounds(bounds: bounds) { (pos: Position) in let neighbors = getNeighbors(cell: pos) if isAlive(pos: pos) { if neighbors < 2 { // Die deadCells.append(pos) } else if neighbors == 2 || neighbors == 3 { // Survive // Do nothing } else if neighbors > 3 { // Die deadCells.append(pos) } } else { if neighbors == 3 { // Live liveCells.append(pos) } } } // Kill cells for cell in deadCells { let index = grid.index{$0 == cell} grid.remove(at: index!) } // Add cells print("liveCells \(liveCells.count)") grid.append(contentsOf: liveCells) sleep(1) }