Skip to content

Instantly share code, notes, and snippets.

@joshskeen
Created March 20, 2016 06:33
Show Gist options
  • Save joshskeen/773c499f15a12b098eed to your computer and use it in GitHub Desktop.
Save joshskeen/773c499f15a12b098eed to your computer and use it in GitHub Desktop.
16-segment-GOL
var segment = null;
var masks = (function() {
"use strict";
// Segment Bitmasks for individual segments.
// Binary Or them together to create bitmasks
// a1|a2|b|c|d1|d2|e|f|g1|g2|h|i|j|k|l|m
var a1 = 1 << 0,
a2 = 1 << 1,
b = 1 << 2,
c = 1 << 3,
d1 = 1 << 4,
d2 = 1 << 5,
e = 1 << 6,
f = 1 << 7,
g1 = 1 << 8,
g2 = 1 << 9,
h = 1 << 10,
i = 1 << 11,
j = 1 << 12,
k = 1 << 13,
l = 1 << 14,
m = 1 << 15;
// Character map associates characters with a bit pattern
return {
' ': 0,
'': 0,
'0': a1 | a2 | b | c | d1 | d2 | e | f | j | m,
'1': b | c | j,
'2': a1 | a2 | b | d1 | d2 | e | g1 | g2,
'3': a1 | a2 | b | c | d1 | d2 | g2,
'4': b | c | f | g1 | g2,
'5': a1 | a2 | c | d1 | d2 | f | g1 | g2,
'6': a1 | a2 | c | d1 | d2 | e | f | g1 | g2,
'7': a1 | a2 | b | c,
'8': a1 | a2 | b | c | d1 | d2 | e | f | g1 | g2,
'9': a1 | a2 | b | c | f | g1 | g2,
'A': e | f | a1 | a2 | b | c | g1 | g2,
'B': a1 | a2 | b | c | d1 | d2 | g2 | i | l,
'C': a1 | a2 | f | e | d1 | d2,
'D': a1 | a2 | b | c | d1 | d2 | i | l,
'E': a1 | a2 | f | e | d1 | d2 | g1 | g2,
'F': a1 | a2 | e | f | g1,
'G': a1 | a2 | c | d1 | d2 | e | f | g2,
'H': b | c | e | f | g1 | g2,
'I': a1 | a2 | d1 | d2 | i | l,
'J': b | c | d1 | d2 | e,
'K': e | f | g1 | j | k,
'L': d1 | d2 | e | f,
'M': b | c | e | f | h | j,
'N': b | c | e | f | h | k,
'O': a1 | a2 | b | c | d1 | d2 | e | f,
'P': a1 | a2 | b | e | f | g1 | g2,
'Q': a1 | a2 | b | c | d1 | d2 | e | f | k,
'R': a1 | a2 | b | e | f | g1 | g2 | k,
'S': a1 | a2 | c | d1 | d2 | f | g1 | g2,
'T': a1 | a2 | i | l,
'U': b | c | d1 | d2 | e | f,
'V': e | f | j | m,
'W': b | c | e | f | k | m,
'X': h | j | k | m,
'Y': b | f | g1 | g2 | l,
'Z': a1 | a2 | d1 | d2 | j | m,
'-': g1 | g2,
'?': a1 | a2 | b | g2 | l,
'+': g1 | g2 | i | l,
'*': g1 | g2 | h | i | j | k | l | m
};
}());
var LEDState = function(position){
this.position = position;
this.a1 = false; //1,1
this.a2 = false; //1,2
this.f = false; //2, 1
this.h = false; //2, 2
this.i = false; //2, 3
this.j = false; //2, 4
this.b = false; //2, 5
this.g1 = false; //3, 1
this.g2 = false; //3, 2
this.e = false; //4, 1
this.m = false; //4, 2
this.l = false; //4, 3
this.k = false; //2, 4
this.c = false; //4, 5
this.d1 = false; //5, 1
this.d2 = false; //5, 2
};
LEDState.prototype.determineState = function(element, neighbors){
var liveNeighbors = neighbors.reduce(function(counter, item){
return counter + (item === true ? 1 : 0);
});
if(element){
//living cell
//Any live cell with fewer than two live neighbours dies, as if caused by under-population.
//Any live cell with more than three live neighbours dies, as if by over-population.
if(liveNeighbors < 2 || liveNeighbors > 3){
return (Math.floor((Math.random() * 10) + 1) > 9);
// return false;
} else {
//Any live cell with two or three live neighbours lives on to the next generation.
return (Math.floor((Math.random() * 35) + 1) < 34);
}
} else {
//dead cell
if(liveNeighbors === 3) {
return true;
} else if( liveNeighbors === 1 ){
return (Math.floor((Math.random() * 25) + 1) > 22);
}
return false;
}
};
LEDState.prototype.advance = function(){
Object.assign(this, this.nextState);
};
//TODO: update neighbors from adjacent cells
LEDState.prototype.setNextState = function(cells){
var newstate = new LEDState();
var left = cells[this.position - 1];
var right = cells[this.position + 1];
//*A1* - see 16_segment_diagram.png for details
var a1Neighbors = [this.f, this.h, this.j, this.a2];
newstate.a1 = this.determineState(this.a1, a1Neighbors);
//*A2*
var a2Neighbors = [this.a1, this.j, this.k, this.b]; //todo: update from adjacent cells
newstate.a2 = this.determineState(this.a2, a2Neighbors);
//*F*
var fNeighbors = [this.a1, this.h, this.g1, this.e]; //todo: update from adjacent cells
if(left !== undefined){
fNeighbors = fNeighbors.concat([left.b])
}
newstate.f = this.determineState(this.f, fNeighbors);
//*H*
var hNeighbors = [this.a1, this.i, this.f, this.g1];
newstate.h = this.determineState(this.h, hNeighbors);
//*I*
var iNeighbors = [this.a1, this.a2, this.h, this.j, this.g1, this.g2];
newstate.h = this.determineState(this.i, iNeighbors);
//*J*
var jNeighbors = [this.a1, this.a2, this.b, this.g2];
newstate.j = this.determineState(this.j, jNeighbors);
//*B*
var bNeighbors = [this.a2, this.j, this.g2, this.c];
if(right !== undefined){
bNeighbors = bNeighbors.concat([right.f])
}
newstate.b = this.determineState(this.b, bNeighbors);
var g1Neighbors = [this.f, this.h, this.i, this.e, this.m, this.l, this.g2];
newstate.g1 = this.determineState(this.g1, g1Neighbors);
//*G2*
var g2Neighbors = [this.i, this.j, this.b, this.l, this.k, this.c];
newstate.g2 = this.determineState(this.g2, g2Neighbors);
//*E*
var eNeighbors = [this.g1, this.m, this.d1, this.f];
if(left !== undefined){
eNeighbors = eNeighbors.concat([left.c])
}
newstate.e = this.determineState(this.e, eNeighbors);
//*M*
var mNeighbors = [this.e, this.g1, this.l, this.d1];
newstate.m = this.determineState(this.m, mNeighbors);
//*L*
var lNeighbors = [this.g1, this.g2, this.m, this.k, this.d1, this.d2];
newstate.l = this.determineState(this.l, lNeighbors);
//*K*
var kNeighbors = [this.g2, this.l, this.c, this.d2];
newstate.k = this.determineState(this.k, kNeighbors);
//*C*
var cNeighbors = [this.g2, this.k, this.d2, this.b];
if(right !== undefined){
cNeighbors = cNeighbors.concat([right.e])
}
newstate.c = this.determineState(this.c, cNeighbors);
//*D1*
var d1Neighbors = [this.e, this.m, this.l, this.d2];
newstate.d1 = this.determineState(this.d1, d1Neighbors);
//*D2*
var d2Neighbors = [this.d1, this.l, this.k, this.c];
newstate.d2 = this.determineState(this.d2, d2Neighbors);
this.nextState = newstate;
};
LEDState.prototype.toBitmask = function() {
var a1 = 1 << 0, a2 = 1 << 1, b = 1 << 2, c = 1 << 3,
d1 = 1 << 4, d2 = 1 << 5, e = 1 << 6, f = 1 << 7,
g1 = 1 << 8, g2 = 1 << 9, h = 1 << 10, i = 1 << 11,
j = 1 << 12, k = 1 << 13, l = 1 << 14, m = 1 << 15;
return (this.a1 === true ? a1 : 0)|(this.a2 === true ? a2 : 0)|
(this.b === true ? b : 0)|(this.c === true ? c : 0)|
(this.d1 === true ? d1 : 0)|(this.d2 === true ? d2 : 0)|
(this.e === true ? e : 0)|(this.f === true ? f : 0)|
(this.g1 === true ? g1 : 0)|(this.g2 === true ? g2 : 0)|
(this.h === true ? h : 0)|(this.i === true ? i : 0)|
(this.j === true ? j : 0)|(this.k === true ? k : 0)|
(this.l === true ? l : 0)|(this.m === true ? m : 0)
}
LEDState.prototype.fromBitmask = function(bitmask) {
var a1 = 1 << 0, a2 = 1 << 1, b = 1 << 2, c = 1 << 3,
d1 = 1 << 4, d2 = 1 << 5, e = 1 << 6, f = 1 << 7,
g1 = 1 << 8, g2 = 1 << 9, h = 1 << 10, i = 1 << 11,
j = 1 << 12, k = 1 << 13, l = 1 << 14, m = 1 << 15;
this.a1 = ((bitmask & a1) == a1);
this.a2 = ((bitmask & a2) == a2);
this.f = ((bitmask & f) == f);
this.h = ((bitmask & h) == h);
this.i = ((bitmask & i) == i);
this.j = ((bitmask & j) == j);
this.b = ((bitmask & b) == b);
this.g1 = ((bitmask & g1) == g1);
this.g2 = ((bitmask & g2) == g2);
this.e = ((bitmask & e) == e);
this.m = ((bitmask & m) == m);
this.l = ((bitmask & l) == l);
this.k = ((bitmask & k) == k);
this.c = ((bitmask & c) == c);
this.d1 = ((bitmask & d1) == d1);
this.d2 = ((bitmask & d2) == d2);
return this;
}
cells = [
new LEDState(0).fromBitmask(masks['C']),
new LEDState(1).fromBitmask(masks['O']),
new LEDState(2).fromBitmask(masks['S']),
new LEDState(3).fromBitmask(masks['M']),
new LEDState(4).fromBitmask(masks['O']),
new LEDState(5).fromBitmask(masks['S']),
];
function animate() {
var i = 0;
for(i = 0; i < cells.length; i++){
var cell = cells[i];
console.log("well ehll!!" + cell);
cell.setNextState(cells);
}
i = 0;
for(i = 0; i < cells.length; i++){
var cell = cells[i];
cell.advance();
}
segment.DisplayCells(cells);
}
var settings = {
X : 0,
Y : 0,
SegmentWidth : 0.12,
BevelWidth : 0.49,
SegmentInterval : 0.025,
SideBevelEnabled : false,
Padding : 15,
Spacing : 10,
FillLight : '#9eff0d',
FillDark : '#0c1401',
StrokeLight : '#ff0000',
StrokeDark : '#550000',
StrokeWidth : 0
};
window.onload = function () {
"use strict";
// Set the canvas size to fill its container
var canvas = document.getElementById('c');
var container = document.getElementById('container');
canvas.width = container.clientWidth;
canvas.height = container.clientHeight;
// Create a new sixteen segment display
segment = new SixteenSegment(6, canvas);
// Initialize the segment settings and input controls
settings.Width = canvas.width;
settings.Height = canvas.height;
for (var s in settings){
segment[s] = settings[s];
}
init(segment);
update(segment);
window.setInterval('animate()', 1500);
segment.DisplayCells(cells);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment