Skip to content

Instantly share code, notes, and snippets.

@kshoji
Created July 19, 2019 07:24
Show Gist options
  • Save kshoji/5b2ecb173bf0af8f49a70a808972b863 to your computer and use it in GitHub Desktop.
Save kshoji/5b2ecb173bf0af8f49a70a808972b863 to your computer and use it in GitHub Desktop.
Game of Life for scroll:bit
let cell: number[] = []
let newCell: number[] = []
let dotBrightness: number = 64
function newGeneration() {
scrollbit.clear()
for (let m = 0; m <= scrollbit.rows() - 1; m++) {
for (let o = 0; o <= scrollbit.cols() - 1; o++) {
cell[m * scrollbit.cols() + o] = Math.randomRange(0, 1)
scrollbit.setPixel(o, m, cell[m * scrollbit.cols() + o] * dotBrightness)
}
}
scrollbit.show()
}
function nextGeneration() {
scrollbit.clear()
for (let i = 0; i <= scrollbit.rows() - 1; i++) {
for (let j = 0; j <= scrollbit.cols() - 1; j++) {
let im = i - 1
if (im < 0) {
im = scrollbit.rows() - 1
}
let jm = j - 1
if (jm < 0) {
jm = scrollbit.cols() - 1
}
let ip = i + 1
if (ip >= scrollbit.rows()) {
ip = 0
}
let jp = j + 1
if (jp >= scrollbit.cols()) {
jp = 0
}
let n = cell[im * scrollbit.cols() + jm] + cell[im * scrollbit.cols() + j] + cell[im * scrollbit.cols() + jp]
+ cell[i * scrollbit.cols() + jm] + cell[i * scrollbit.cols() + jp]
+ cell[ip * scrollbit.cols() + jm] + cell[ip * scrollbit.cols() + j] + cell[ip * scrollbit.cols() + jp]
if (n < 2) {
newCell[i * scrollbit.cols() + j] = 0
} else if (n === 2) {
newCell[i * scrollbit.cols() + j] = cell[i * scrollbit.cols() + j]
if (cell[i * scrollbit.cols() + j] == 1) {
scrollbit.setPixel(j, i, dotBrightness)
}
} else if (n === 3) {
newCell[i * scrollbit.cols() + j] = 1
scrollbit.setPixel(j, i, dotBrightness)
} else {
newCell[i * scrollbit.cols() + j] = 0
}
}
}
for (let k = 0; k <= scrollbit.rows() - 1; k++) {
for (let l = 0; l <= scrollbit.cols() - 1; l++) {
cell[k * scrollbit.cols() + l] = newCell[k * scrollbit.cols() + l]
}
}
scrollbit.show()
}
newGeneration();
basic.forever(function () {
control.waitMicros(100000)
nextGeneration()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment