Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #18 from brianjoseff/master
added 3 state cells--however, no different behavior yet--also, might not be integrated well with the gThreeUnused array architecture
  • Loading branch information
brianjoseff committed Jan 19, 2015
2 parents 3320dcd + 3c29c8e commit a5cb0d8
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 16 deletions.
3 changes: 2 additions & 1 deletion index.html
Expand Up @@ -79,6 +79,7 @@
</div>
<span id="debug"></span>
<br/>
<button class="goHash" onclick="setRule('?rule=golRule')" id="x"> choose a rule</button>
<button class="goHash" id="introHash" onclick="selectHash('PeciXwDuTA0', this)">Intro</button>
<br/>
<button class="goHash" onclick="selectHash('GJR0', this)">Glider 1</button>
Expand Down Expand Up @@ -179,7 +180,7 @@
<script type="text/javascript" src="js/gol3DRule.js"></script>
<script type="text/javascript" src="js/bbRule.js"></script>
<script type="text/javascript" src="js/bb3Rule.js"></script>

<script type="text/javascript" src="js/bb3StateRule.js"></script>
<script type="text/javascript" src="js/index.js"></script>

<div style="display:hidden">
Expand Down
9 changes: 7 additions & 2 deletions js/Grid.js
@@ -1,6 +1,6 @@
var DEBUG = true;

function Grid(x, y, z, mod_range, mode) {
function Grid(x, y, z, mod_range, mode, state) {
this.dimx = x;
this.dimy = y;
this.dimz = z;
Expand Down Expand Up @@ -37,11 +37,16 @@ function Grid(x, y, z, mod_range, mode) {
//update new set of cells
this.put = function(x, y, z, cell_state) {
var key = "" + x + "," + y + "," + z;
if (cell_state) {

if (cell_state == 1 || cell_state === -1) {
this.cells[key] = cell_state;
console.log("mission accomplished");
console.log("cell state: ", cell_state);
} else {
//research this
delete this.cells[key];
console.log("nay");
console.log("cell state: ", cell_state);
}
}

Expand Down
2 changes: 2 additions & 0 deletions js/bb3Rule.js
Expand Up @@ -2,6 +2,8 @@
* implements the BusyBoxes reversible CA as outlined here:
* http://arxiv.org/abs/1206.2060
*/


bb3Rule = function(grid, x, y, z, frm) {
var offx, offy, offz, swpx, swpy, swpz;
var coi = grid.get(x, y, z);
Expand Down
125 changes: 125 additions & 0 deletions js/bb3StateRule.js
@@ -0,0 +1,125 @@
/*
* implements the BusyBoxes reversible CA as outlined here:
* http://arxiv.org/abs/1206.2060
*/

STATES=3;

bb3StateRule = function(grid, x, y, z, frm) {
var offx, offy, offz, swpx, swpy, swpz;
var coi = grid.get(x, y, z);
if ((x + y + z & 1) != (frm & 1)) return coi; // only process if field parity is correct

function getSwap(x, y, z) { // return valid swap offset or null if there is contention
var swapx = null, swapy = null;
for (var i=0; i<8; i++) {
var xx = x+offx[i];
var yy = y+offy[i];
var zz = z+offz[i];
if (grid.get(xx, yy, zz)) {
if ((swapx != null) && (swapx != swpx[i] || swapy != swpy[i] || swapz != swpz[i]) ) { // swap confict, forgeddaboudit
return null;
}
swapx = swpx[i];
swapy = swpy[i];
swapz = swpz[i];
}
}
if (swapx == null) return null;
return [swapx, swapy, swapz];
}

function getSwap2(x, y, z) { // return valid swap offset or null if there is contention
var swapx = null, swapy = null;
for (var i=0; i<8; i++) {
var xx = x+offx[i];
var yy = y+offy[i];
var zz = z+offz[i];
if (grid.get(xx, yy, zz)) {
if ((swapx != null) && (swapx != swpx[i] || swapy != swpy[i] || swapz != swpz[i]) ) { // swap confict, forgeddaboudit
return null;
}
swapx = swpx[i];
swapy = swpy[i];
swapz = swpz[i];
}
}
if (swapx == null) return null;
return [swapx, swapy, swapz];
}

function onePlane() { // process one of 3 planes dep on phase
var swap = getSwap(x, y, z); // proposed swap cell as delta from xyz
if (swap != null) { // if valid (no immediate conflicts)
var swapper = grid.get(x+swap[0], y+swap[1], z+swap[2]); // get state at swap cell
if (swapper != coi) { // if state is different from ours, we might swap
var revswap = getSwap(x+swap[0], y+swap[1], z+swap[2]); // proposed swap for swap cell
if (revswap != null) {
if ( (swap[0] + revswap[0] == 0) &&
(swap[1] + revswap[1] == 0) && // if it matches (mutual proposed swaps), do this thing
(swap[2] + revswap[2] == 0)) { // we return his state; he will return ours.
return swapper; // That's what we call a swap, Scooby Doo
}
}
}
}
return coi;
}

var m = trueMod(frame, 3); // set up offsets & swap coords dep on phase
if (m==0) {
offx = bb_offsetx;
offy = bb_offsety;
offz = bb_offsetz;
swpx = bb_swapx;
swpy = bb_swapy;
swpz = bb_swapz;
}
if (m==1) {
offx = bb_offsetz;
offy = bb_offsetx;
offz = bb_offsety;
swpx = bb_swapz;
swpy = bb_swapx;
swpz = bb_swapy;
}
if (m==2) {
offx = bb_offsety;
offy = bb_offsetz;
offz = bb_offsetx;
swpx = bb_swapy;
swpy = bb_swapz;
swpz = bb_swapx;
}
return onePlane();
}

// Knight's move offsets
var bb_offsetx = [+2, +1, -1, -2, +2, +1, -1, -2];
var bb_offsety = [+1, +2, +2, +1, -1, -2, -2, -1];
var bb_offsetz = [0, 0, 0, 0, 0, 0, 0, 0];

// corresponding swap offsets
var bb_swapx = [+1, -1, +1, -1, +1, -1, +1, -1];
var bb_swapy = [-1, +1, +1, -1, +1, -1, -1, +1];
var bb_swapz = [0, 0, 0, 0, 0, 0, 0, 0];

// Javascript ftw
function trueMod(v, base) {
if (v < 0) {
return ((v % base) + base) % base;
}
return v % base;
}

// and TDD also ftw
bb_TEST=0;
if(bb_TEST) {
var grid = new Grid(10, 10, 10);
grid.put(0, 0, 0, 1);
grid.put(1, 2, 0, 1);
// grid.put(-1, 2, 0, 1);
console.log("bbRule returns:", bbRule(grid, 0, 0, 0));
console.log("bbRule returns:", bbRule(grid, -1, 1, 0));
}

3 changes: 3 additions & 0 deletions js/bbRule.js
Expand Up @@ -2,6 +2,9 @@
* implements the BusyBoxes reversible CA as outlined here:
* http://arxiv.org/abs/1206.2060
*/

STATES=3;

bbRule = function(grid, x, y, z, frm) {
var offx, offy, offz, swpx, swpy, swpz;
var coi = grid.get(x, y, z);
Expand Down

0 comments on commit a5cb0d8

Please sign in to comment.