forked from creationix/experiments
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathturtle.js
More file actions
197 lines (188 loc) · 3.72 KB
/
turtle.js
File metadata and controls
197 lines (188 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Add some ruby-like methods to some of the builtins
Object.prototype.instance_eval = function (block) {
// Convert the function to a string so that we can rebind it
if (typeof block === 'function') {
block = "(" + block + ").call(this)";
}
// Eval using "this" as the "with" scope
return eval("with(this) { " + block + "}");
};
Object.prototype.keys = function () {
var key, keys = [];
for (key in this) {
if (this.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
};
Number.prototype.times = function (block) {
for (var i = 0; i < this; i += 1) {
block(i);
}
};
Number.prototype.is_even = function () {
return (this % 2) === 0;
};
Number.prototype.upto = function (other, block) {
for (var i = this; i <= other; i+= 1) {
block(i);
}
};
Array.prototype.minmax = function () {
var min, max;
this.forEach(function (item) {
if (min === undefined || item < min) {
min = item;
}
if (max === undefined || item > max) {
max = item;
}
});
return [min, max];
};
function Turtle() {
// directions: 0 = E, 1 = S, 2 = W, 3 = N
// axis: 0 = x, 1 = y
this.board = {};
this.x = 0;
this.y = 0;
this.direction = 0;
this.pen_up();
}
Turtle.prototype = {
pen_up: function () {
this.is_pen_down = false;
},
pen_down: function () {
this.is_pen_down = true;
this.mark_current_location();
},
forward: function (n) {
with(this) {
if (n === undefined) {
n = 1;
}
n.times(function () {
move();
});
}
},
left: function () {
this.direction -= 1;
if (this.direction < 0) {
this.direction = 3;
}
},
right: function () {
this.direction += 1;
if (this.direction > 3) {
this.direction = 0;
}
},
walk: function (block) {
this.instance_eval(block);
},
draw: function () {
var xrange, yrange,
puts = require('sys').puts;
with (this) {
xrange = board.keys().map(function (key) {
return parseInt(key.split(',')[0]);
}).minmax();
yrange = board.keys().map(function (key) {
return parseInt(key.split(',')[1]);
}).minmax();
yrange[0].upto(yrange[1], function (y) {
var row = "";
xrange[0].upto(xrange[1], function (x) {
row += board[[x,y]] || " ";
});
puts(row);
});
}
},
move: function () {
var increment = this.direction > 1 ? -1 : 1;
if (this.direction.is_even()) {
this.x += increment;
} else {
this.y += increment;
}
this.mark_current_location();
},
mark_current_location: function () {
if (this.is_pen_down) {
this.board[[this.x, this.y]] = "#";
}
}
};
// Use the dsl (Domain Specific Language)
var turtle = new Turtle();
turtle.walk(function () {
pen_down();
left();
forward(4);
right();
(4).times(function () {
forward(1);
right();
forward(1);
left();
});
left();
forward(4);
right();
pen_up();
forward(4);
pen_down();
(4).times(function () {
forward(4);
right();
});
pen_up();
forward(8);
pen_down();
forward(3);
right();
forward(1);
left();
forward(1);
right();
forward(2);
right();
forward(1);
left();
forward(1);
right();
forward(3);
right();
forward(4);
right();
pen_up();
// E
forward(12);
right();
right();
pen_down();
forward(4);
left();
forward(4);
left();
forward(4);
pen_up();
left();
forward(2);
left();
forward(1);
pen_down();
forward(3);
});
turtle.draw();
// Here's the output if you don't have node
// Timothy-Caswells-MacBook-Pro:Desktop tim$ node turtle.js
// ##### ##### #####
// # # # # # #
// # # # # # #
// # # # # # #
// ##### ##### #####