Skip to content

Instantly share code, notes, and snippets.

@ntoonio
Last active July 20, 2017 14:00
Show Gist options
  • Save ntoonio/6b69cfc855024da8561203623c34d54a to your computer and use it in GitHub Desktop.
Save ntoonio/6b69cfc855024da8561203623c34d54a to your computer and use it in GitHub Desktop.
<html>
<head>
</head>
<body>
<pre id="output">
</pre>
<script src="GameOfLife.js"></script>
</body>
</html>
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()
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 {
neighbors += 1
}
}
return neighbors
}
while true {
let bounds = getBounds()
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
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment