game = {
let p = {
add: function(value) {
let direction = this.d6();
let ring = this.d4();
this.assign(value, direction, ring);
},
assign: function(value, direction, ring) {
this.log += ` @(${direction},${ring}):`
let q = this.fromPolar(direction, ring);
if (this.board[q[0]][q[1]]==0) {
this.board[q[0]][q[1]]=value;
this.log += `:add`;
} else {
this.log += `:nope`;
}
},
d4: function() { return Math.floor(Math.random() * 4); },
d6: function() { return Math.floor(Math.random() * 6); },
log: "log:",
move: function(point, direction) {
let odd = (n) => n % 2;
let dx = 0;
let dy = 0;
switch (direction % 6) {
case 0: dx=0;dy=-1; break;
case 1: dx=1;dy = odd(point[0]) ? -1 : 0; break;
case 2: dx=1;dy = odd(point[0]) ? 0 : +1; break;
case 3: dx = 0; dy = 1; break;
case 4: dx = -1; dy = odd(point[0]) ? 0 : +1; break;
case 5: dx = -1; dy = odd(point[0]) ? -1 : 0; break;
}
return [point[0]+dx, point[1]+dy];
},
fromPolar: function(direction, ring) {
let here = [4,4];
for (let r = ring; r >= 0; r--) {
here = this.move(here, direction);
this.log += `<<${here[0]},${here[1]}>>`
}
return here;
}
}
return p;
}