Permalink
Browse files

A smidgen less quick, a hair less dirty.

  • Loading branch information...
cpressey committed Sep 21, 2012
1 parent 1503857 commit efe7bf6b809a7dac2cc6aedc158301256418343b
Showing with 116 additions and 87 deletions.
  1. +16 −0 README.markdown
  2. +8 −1 html/gemooy.html
  3. +31 −84 src/gemooy.js
  4. +61 −2 src/playfield.js
View
@@ -0,0 +1,16 @@
+Gemooy
+======
+
+This is the reference distribution of Gemooy.
+
+However, it only contains one implementation of Gemooy at the moment, and
+it isn't even the reference implementation.
+
+And it doesn't even contain documentation, either.
+
+These shortcomings will soon be addressed. Maybe. For now, see
+[the Gemooy article on the esowiki](http://esolangs.org/wiki/Gemooy)
+for a description of the language.
+
+The implementation of Gemooy contained herein is in Javascript, and uses
+the HTML5 canvas element to display the playfield.
View
@@ -21,10 +21,18 @@
</script>
<style>
#canvas { border: 1px solid blue; }
+#info { float: right; }
</style>
</head>
<body>
+<p id="info">
+Gemooy on:
+<a href="http://catseye.tc/node/Gemooy.html">catseye.tc</a> |
+<a href="https://github.com/catseye/Gemooy">github</a> |
+<a href="http://esolangs.org/wiki/Gemooy">esolangs wiki</a>
+</p>
+
<h1>Gemooy
<button id="load">Load</button>
<button id="start">Start</button>
@@ -35,7 +43,6 @@ <h1>Gemooy
Your browser doesn't support displaying an HTML5 canvas.
</canvas>
-<span>
<textarea id="program" rows="25" cols="40">
% @@ @@
# @ $ @
View
@@ -3,12 +3,8 @@ function GemooyController(canvas) {
var interval_id;
var p = Playfield();
- var ip_x;
- var ip_y;
- var ip_dx;
- var ip_dy;
- var dp_x;
- var dp_y;
+ var ip = Cursor(0, 0, 1, 1);
+ var dp = Cursor(0, 0, 0, 0);
self.draw = function() {
var ctx = canvas.getContext('2d');
@@ -21,66 +17,17 @@ function GemooyController(canvas) {
var width = ctx.measureText("@").width;
ctx.fillStyle = "#ff5080";
- ctx.fillRect(ip_x * width, ip_y * height, width, height);
+ ctx.fillRect(ip.x * width, ip.y * height, width, height);
ctx.fillStyle = "#50ff80";
- ctx.fillRect(dp_x * width, dp_y * height, width, height);
+ ctx.fillRect(dp.x * width, dp.y * height, width, height);
ctx.fillStyle = "black";
p.foreach(function (x, y, value) {
ctx.fillText(value, x * width, y * height);
});
}
- var rotate_clockwise = function() {
- if (ip_dx === 0 && ip_dy === -1) {
- ip_dx = 1; ip_dy = -1;
- } else if (ip_dx === 1 && ip_dy === -1) {
- ip_dx = 1; ip_dy = 0;
- } else if (ip_dx === 1 && ip_dy === 0) {
- ip_dx = 1; ip_dy = 1;
- } else if (ip_dx === 1 && ip_dy === 1) {
- ip_dx = 0; ip_dy = 1;
- } else if (ip_dx === 0 && ip_dy === 1) {
- ip_dx = -1; ip_dy = 1;
- } else if (ip_dx === -1 && ip_dy === 1) {
- ip_dx = -1; ip_dy = 0;
- } else if (ip_dx === -1 && ip_dy === 0) {
- ip_dx = -1; ip_dy = -1;
- } else if (ip_dx === -1 && ip_dy === -1) {
- ip_dx = 0; ip_dy = -1;
- }
- }
-
- var rotate_counterclockwise = function() {
- if (ip_dx === 0 && ip_dy === -1) {
- ip_dx = -1; ip_dy = -1;
- } else if (ip_dx === -1 && ip_dy === -1) {
- ip_dx = -1; ip_dy = 0;
- } else if (ip_dx === -1 && ip_dy === 0) {
- ip_dx = -1; ip_dy = 1;
- } else if (ip_dx === -1 && ip_dy === 1) {
- ip_dx = 0; ip_dy = 1;
- } else if (ip_dx === 0 && ip_dy === 1) {
- ip_dx = 1; ip_dy = 1;
- } else if (ip_dx === 1 && ip_dy === 1) {
- ip_dx = 1; ip_dy = 0;
- } else if (ip_dx === 1 && ip_dy === 0) {
- ip_dx = 1; ip_dy = -1;
- } else if (ip_dx === 1 && ip_dy === -1) {
- ip_dx = 0; ip_dy = -1;
- }
- }
-
- var is_headed = function(dx, dy) {
- return ip_dx === dx && ip_dy === dy;
- }
-
- var advance = function() {
- ip_x += ip_dx;
- ip_y += ip_dy;
- }
-
var increment = function(x, y) {
var data = p.get(x, y);
if (data === undefined) {
@@ -106,36 +53,36 @@ function GemooyController(canvas) {
}
self.step = function() {
- var instr = p.get(ip_x, ip_y);
+ var instr = p.get(ip.x, ip.y);
if (instr === '@') {
- var data = p.get(dp_x, dp_y);
+ var data = p.get(dp.x, dp.y);
if (data === undefined) {
- rotate_clockwise();
+ ip.rotate_clockwise();
} else if (data == '#') {
- rotate_counterclockwise();
+ ip.rotate_counterclockwise();
}
} else if (instr === '#') {
- if (is_headed(0, -1)) {
- dp_y--;
- advance();
- } else if (is_headed(0, 1)) {
- dp_y++;
- advance();
- } else if (is_headed(1, 0)) {
- dp_x++;
- advance();
- } else if (is_headed(-1, 0)) {
- dp_x--;
- advance();
- } else if (is_headed(-1, -1) || is_headed(1, -1)) {
- increment(dp_x, dp_y);
- } else if (is_headed(-1, 1) || is_headed(1, 1)) {
- decrement(dp_x, dp_y);
+ if (ip.is_headed(0, -1)) {
+ dp.y--;
+ ip.advance();
+ } else if (ip.is_headed(0, 1)) {
+ dp.y++;
+ ip.advance();
+ } else if (ip.is_headed(1, 0)) {
+ dp.x++;
+ ip.advance();
+ } else if (ip.is_headed(-1, 0)) {
+ dp.x--;
+ ip.advance();
+ } else if (ip.is_headed(-1, -1) || ip.is_headed(1, -1)) {
+ increment(dp.x, dp.y);
+ } else if (ip.is_headed(-1, 1) || ip.is_headed(1, 1)) {
+ decrement(dp.x, dp.y);
}
}
- advance();
+ ip.advance();
self.draw();
}
@@ -159,17 +106,17 @@ function GemooyController(canvas) {
p.load(0, 0, textarea.val());
p.foreach(function (x, y, value) {
if (value === '$') {
- ip_x = x;
- ip_y = y;
+ ip.x = x;
+ ip.y = y;
return ' ';
} else if (value === '%') {
- dp_x = x;
- dp_y = y;
+ dp.x = x;
+ dp.y = y;
return ' ';
}
});
- ip_dx = 1;
- ip_dy = 1;
+ ip.dx = 1;
+ ip.dy = 1;
self.draw();
}
View
@@ -1,6 +1,65 @@
+function Cursor(x, y, dx, dy) {
+ var self = {};
+ self.x = x;
+ self.y = y;
+ self.dx = dx;
+ self.dy = dy;
+
+ self.is_headed = function(dx, dy) {
+ return self.dx === dx && self.dy === dy;
+ }
+
+ self.advance = function() {
+ self.x += self.dx;
+ self.y += self.dy;
+ }
+
+ self.rotate_clockwise = function() {
+ if (self.dx === 0 && self.dy === -1) {
+ self.dx = 1; self.dy = -1;
+ } else if (self.dx === 1 && self.dy === -1) {
+ self.dx = 1; self.dy = 0;
+ } else if (self.dx === 1 && self.dy === 0) {
+ self.dx = 1; self.dy = 1;
+ } else if (self.dx === 1 && self.dy === 1) {
+ self.dx = 0; self.dy = 1;
+ } else if (self.dx === 0 && self.dy === 1) {
+ self.dx = -1; self.dy = 1;
+ } else if (self.dx === -1 && self.dy === 1) {
+ self.dx = -1; self.dy = 0;
+ } else if (self.dx === -1 && self.dy === 0) {
+ self.dx = -1; self.dy = -1;
+ } else if (self.dx === -1 && self.dy === -1) {
+ self.dx = 0; self.dy = -1;
+ }
+ }
+
+ self.rotate_counterclockwise = function() {
+ if (self.dx === 0 && self.dy === -1) {
+ self.dx = -1; self.dy = -1;
+ } else if (self.dx === -1 && self.dy === -1) {
+ self.dx = -1; self.dy = 0;
+ } else if (self.dx === -1 && self.dy === 0) {
+ self.dx = -1; self.dy = 1;
+ } else if (self.dx === -1 && self.dy === 1) {
+ self.dx = 0; self.dy = 1;
+ } else if (self.dx === 0 && self.dy === 1) {
+ self.dx = 1; self.dy = 1;
+ } else if (self.dx === 1 && self.dy === 1) {
+ self.dx = 1; self.dy = 0;
+ } else if (self.dx === 1 && self.dy === 0) {
+ self.dx = 1; self.dy = -1;
+ } else if (self.dx === 1 && self.dy === -1) {
+ self.dx = 0; self.dy = -1;
+ }
+ }
+
+ return self;
+}
+
function Playfield() {
- var self = {}
- var store = {}
+ var self = {};
+ var store = {};
self.min_x = undefined;
self.min_y = undefined;
self.max_x = undefined;

0 comments on commit efe7bf6

Please sign in to comment.