-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtles.lg
110 lines (89 loc) · 1.8 KB
/
turtles.lg
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
make ".turtles array 1000
buryname ".turtles
make ".curturtle 1
buryname ".curturtle
;; turtle bookkeeping
to saveturtlestate
.setitem turtle :.turtles turtlestate
end
bury "saveturtlestate
to markturtle
.setitem turtle :.turtles "current
end
bury "markturtle
to checkturtle :ttl
if emptyp item :ttl :.turtles [(throw "error se [no such turtle] :ttl)]
end
bury "checkturtle
;; turtle queries
to turtle
op :.curturtle
end
bury "turtle
to turtlepos :ttl
checkturtle :ttl
if :ttl = turtle [op pos]
op first item :ttl :.turtles
end
bury "turtlepos
to turtlestate
op (list pos heading pen)
end
bury "turtlestate
;; changing turtles
to setturtle :ttl
if :ttl = turtle [stop]
checkturtle :ttl
saveturtlestate
make ".curturtle :ttl
setturtlestate item :ttl :.turtles
markturtle
end
bury "setturtle
to setturtlestate :ttl
pu
setpos first :ttl
setheading first bf :ttl
setpen last :ttl
end
bury "setturtlestate
;; creating and deleting turtles
to copyturtle
ignore cloneturtle
end
bury "copyturtle
to cloneturtle
localmake "oldturtle turtle
saveturtlestate
repeat 1000 [
make ".curturtle :.curturtle + 1
if :.curturtle > 1000 [make ".curturtle 1]
if emptyp item turtle :.turtles [markturtle op :oldturtle]
]
(throw "error [too many turtles])
end
bury "cloneturtle
to deleteturtle :ttl
if :ttl = turtle [(throw "error [cannot delete current turtle])]
.setitem :ttl :.turtles []
end
bury "deleteturtle
to resetturtles
make ".turtles array 1000
make ".curturtle 1
markturtle
end
bury "resetturtles
;; nice shorthands
to spread :count
repeat :count [copyturtle rt 360 / :count]
end
bury "spread
to allturtles
op filter [not emptyp item ? :.turtles] iseq 1 1000
end
bury "allturtles
to alldo :.prog
foreach allturtles [setturtle ? run :.prog]
end
bury "alldo