Skip to content

Instantly share code, notes, and snippets.

@TylerLeite
Created December 3, 2019 23:06
Show Gist options
  • Save TylerLeite/f896425d1ceeb48908b2eba5ae8f6540 to your computer and use it in GitHub Desktop.
Save TylerLeite/f896425d1ceeb48908b2eba5ae8f6540 to your computer and use it in GitHub Desktop.
cells_1
tiles = '*-+=o8|:x '.split('')
// tiles = ' #.'.split('')
function newWorld () {
world = []
for (let i = 0; i < 23; i++) {
world.push([])
for (let j = 0; j < 80; j++) {
// world[i].push(' ')
world[i].push(tiles[Math.floor(Math.random()*tiles.length)])
}
}
return world
}
world = newWorld()
function print () {
console.clear()
console.log(world.map(e => e.join('')).join('\n'))
}
function arrct (arr, e) {
let ct = 0
for (let i = 0; i < arr.length; i++) {
if (arr[i] == e) {
ct += 1
}
}
return ct
}
function update (n) {
tmp = newWorld()
for (let i = 0; i < world.length; i++) {
for (let j = 0; j < world[i].length; j++) {
cts = []
for (let x = -1; x < 2; x++) {
for (let y = -1; y < 2; y++) {
nx = x + j
ny = y + i
if (nx < 0 || ny < 0 || nx >= world[i].length || ny >= world.length) {
cts.push('?')
} else {
if (world[ny][nx] == world[i][j]) {
cts.push('?')
} else {
cts.push(world[ny][nx])
}
}
}
}
c = cts[Math.floor(Math.random()*cts.length)]
if (c === '?') {
c = tiles[Math.floor(Math.random()*tiles.length)]
}
d = tmp[i][j]
if (arrct(cts, d) < 3 || arrct(cts, d) > 6) {
tmp[i][j] = c
}
}
}
world = tmp
print()
if (n != 0) {
setTimeout(update.bind(this, n-1), 100);
}
}
update(1000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment