Skip to content
Permalink
Browse files

Add big bang of life

  • Loading branch information...
antonmedv committed Feb 9, 2019
1 parent 07f8e6e commit 2d3becc9e41c7326865021f57be2fa61e5fc87b3
Showing with 83 additions and 0 deletions.
  1. +79 −0 bang.js
  2. +3 −0 index.js
  3. +1 −0 package.json
79 bang.js
@@ -0,0 +1,79 @@
'use strict'
// http://bit.ly/fx--life

const
run = f => setInterval(f, 16),
p = (s) => process.stdout.write(s),
esc = (...x) => x.map(i => p('\u001B[' + i)),
[upper, lower, full] = ['\u2580', '\u2584', '\u2588'],
{columns, rows} = process.stdout,
[w, h] = [columns, rows * 2],
a = Math.floor(w / 2) - 6, b = Math.floor(h / 2) - 7

let $ = Array(w * h).fill(false)
if (process.env.GUN) {
$[1 + 5 * w] = $[1 + 6 * w] = $[2 + 5 * w] = $[2 + 6 * w] = $[12 + 5 * w] = $[12 + 6 * w] = $[12 + 7 * w] = $[13 + 4 * w] = $[13 + 8 * w] = $[14 + 3 * w] = $[14 + 9 * w] = $[15 + 4 * w] = $[15 + 8 * w] = $[16 + 5 * w] = $[16 + 6 * w] = $[16 + 7 * w] = $[17 + 5 * w] = $[17 + 6 * w] = $[17 + 7 * w] = $[22 + 3 * w] = $[22 + 4 * w] = $[22 + 5 * w] = $[23 + 2 * w] = $[23 + 3 * w] = $[23 + 5 * w] = $[23 + 6 * w] = $[24 + 2 * w] = $[24 + 3 * w] = $[24 + 5 * w] = $[24 + 6 * w] = $[25 + 2 * w] = $[25 + 3 * w] = $[25 + 4 * w] = $[25 + 5 * w] = $[25 + 6 * w] = $[26 + 1 * w] = $[26 + 2 * w] = $[26 + 6 * w] = $[26 + 7 * w] = $[35 + 3 * w] = $[35 + 4 * w] = $[36 + 3 * w] = $[36 + 4 * w] = true
} else if (process.env.RANDOM) {
for (let i = 0; i < $.length; i++)
if (Math.random() < 0.16) $[i] = true
} else {
$[a + 1 + (2 + b) * w] = $[a + 2 + (1 + b) * w] = $[a + 2 + (3 + b) * w] = $[a + 3 + (2 + b) * w] = $[a + 5 + (15 + b) * w] = $[a + 6 + (13 + b) * w] = $[a + 6 + (15 + b) * w] = $[a + 7 + (12 + b) * w] = $[a + 7 + (13 + b) * w] = $[a + 7 + (15 + b) * w] = $[a + 9 + (11 + b) * w] = $[a + 9 + (12 + b) * w] = $[a + 9 + (13 + b) * w] = true
}

function at(i, j) {
if (i < 0) i = h - 1
if (i >= h) i = 0
if (j < 0) j = w - 1
if (j >= w) j = 0
return $[i * w + j]
}

function neighbors(i, j) {
let c = 0
at(i - 1, j - 1) && c++
at(i - 1, j) && c++
at(i - 1, j + 1) && c++
at(i, j - 1) && c++
at(i, j + 1) && c++
at(i + 1, j - 1) && c++
at(i + 1, j) && c++
at(i + 1, j + 1) && c++
return c
}

run(() => {
esc('H')

let gen = Array(w * h).fill(false)
for (let i = 0; i < h; i++) {
for (let j = 0; j < w; j++) {
const n = neighbors(i, j)
const z = i * w + j
if ($[z]) {
if (n < 2) gen[z] = false
if (n === 2 || n === 3) gen[z] = true
if (n > 3) gen[z] = false
} else {
if (n === 3) gen[z] = true
}
}
}
$ = gen

for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
if ($[i * 2 * w + j] && $[(i * 2 + 1) * w + j]) p(full)
else if ($[i * 2 * w + j] && !$[(i * 2 + 1) * w + j]) p(upper)
else if (!$[i * 2 * w + j] && $[(i * 2 + 1) * w + j]) p(lower)
else p(' ')
}
if (i !== rows - 1) p('\n')
}
})

esc('2J', '?25l')

process.on('SIGINT', () => {
esc('?25h')
process.exit(2)
})
@@ -52,6 +52,9 @@ function main(input) {
stderr.write(require('./package.json').version + '\n')
process.exit(2)
}
if (args.length === 1 && args[0] === '--life') {
return require('./bang')
}

input = fs.readFileSync(args[0])
filename = path.basename(args[0])
@@ -9,6 +9,7 @@
"fx": "index.js"
},
"files": [
"bang.js",
"config.js",
"find.js",
"fx.js",

0 comments on commit 2d3becc

Please sign in to comment.
You can’t perform that action at this time.