parallel turtles in mswlogo 
Author Message
 parallel turtles in mswlogo

i cooked up something, and some of you might be interested.
parallel turtles in mswlogo.

please give me feedback!

mswlogo has multiple turtles
but using them is somewhat tedious.
you have to do something like

setturtle :x
setturtle :x + 1

i wanted to tell the turtles what they should do,
and i wanted to be able to ask all the turtles
the same question

so we have the following new procedures and functions
to tell.turtle :who :action
to tell.turtles :who :action
to ask.turtle :who :question
to ask.turtles :who :question

tell.turtle and ask.turtle are for one turtle only
so the 1st agrument (:who) has to be a number
the second argument has to be a legal instruction
(or sequence of instructions)

tell.turtle 0 [fd 10]
will move turtle 0 forward 10 steps
ask.turtle 0 [pos]
will give you the position of turtle 0

to deal with multiple turtles concurrently
tell.turtles and ask.turtles
have lists as 1st argument

tell.turtle [0 1 2 3] [fd 10]
will mov 4 turtles forward
(use iseq 0 100 for talking to 100 turtles at the same time)

ask.turtle iseq 0 99 [pos]
will give a list containing the positions of te first 100


parallel execution

do.par :who :action
takes a list of turtle numbers and a sequence of actions
and performs these action "interwovev"

do.par iseq 0 9 [[fd 10] [rt 10] [fd 10]]

will do fd 10 for all turtles,
then rt 90 for all turtles, and then fd 10 for all turtles
each one of the bracketed subactions will be performed for all
turtles, only then the next subaction will be performed.
if :actions contains only one word which is the name
of a user defined procedure
this procedure will be "unwoven", i.e. each of its steps
will be performed for all the turtle before the
next step is performed for all turtles.

nowit should be clear what
repeat.par :n.of.times :who :actions
it is a parallel repeat,
it is repeated execution of do.par

in all the actions,
turtle will give you the number of the turtle just
performing the action

in repeat.par repcount will give the number of the current repetition

here are some examples

to demo1
tell.turtles iseq 0 10 [setpos list 20 * turtle 0]
tell.turtles iseq 0 10 [fd 5 * turtle]
wait 20
tell.turtles iseq 0 10 [fd 5 * turtle]
wait 20
tell.turtles iseq 0 10 [fd 5 * turtle]
wait 20

to demo2
do.par iseq 0 10 [[fd 10 + 10 * turtle] [rt 90] [fd 10 + 10 * turtle]
[rt 90] ~
        [fd 10 + 10 * turtle] [rt 90] [fd 10 + 10 * turtle] [rt 90]]

to demo3
repeat.par 360 iseq 0 10 [fd 0.2 * (turtle + 1) rt 1]

to demo4
repeat.par 20 iseq 0 10 [[fd repcount * (1 + turtle)] [rt 90]]


here is the "turtles in love" problem.
in this problem, each turtle is in love with the next turtle
(in the sense 0->1->2->3->0)
and always moves towards the love one

repeat.par 300 iseq 0 3 [approach]

to setup
tell.turtles iseq 0 3 [penup]
tell.turtle 0 [setpos [-150 -150]]
tell.turtle 1 [setpos [-150 150]]
tell.turtle 2 [setpos [150 150]]
tell.turtle 3 [setpos [150 -150]]
tell.turtles iseq 0 3 [pendown]

to :n
if :n = 3 [output 0]
output :n + 1

to approach
setheading towards ask turtle [pos]
fd 1

and now, the library procuedure


to ask.turtle :who :question
localmake "oldturtle turtle
setturtle :who
localmake "result invoke :question :who
setturtle :oldturtle
output :result

to ask.turtles :who :question
output map [ask.turtle ? :question] :who

to do.par :who :actions
if emptyp :actions [stop]
ifelse and (wordp :actions) (not (numberp :actions)) ~
        [do.par.helper :who butfirst text :actions] ~
        [do.par.helper :who :actions]

to do.par.helper :who :actions
if emptyp :actions [stop]
if not listp first :actions [tell.turtles :who :actions stop]
do.par.helper.recur :who :actions

to do.par.helper.recur :who :actions
if emptyp :actions [stop]
tell.turtles :who first :actions
do.par.helper.recur :who butfirst :actions

to repeat.par :n.of.times :who :actions
repeat :n.of.times [do.par :who :actions]

to tell.turtle :who :action
localmake "oldturtle turtle
setturtle :who
run :action
setturtle :oldturtle

to tell.turtles :who :action
foreach :who [tell.turtle ? :action]

bury [ask.turtle ask.turtles tell.turtle tell.turtles ~
        do.par do.par.helper do.par.helper.recur repeat.par]

Tue, 24 Sep 2002 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. LOGO-L> parallel turtles in mswlogo

2. Q Turtle 4 mswlogo?

3. 3D turtle commands of mswlogo port to ucblogo?

4. mswlogo with homebuilt turtle

5. turtle shapes for mswlogo

6. More than one turtle in MSWLogo

7. how to use 2 or more turtles in mswlogo

8. how to use 2 or more turtles in mswlogo

9. MSWLogo Mechanic Turtles

10. MSWLogo Mechanic turtles

11. LOGO-L>My DOS turtle needs to become an NT turtle

12. Help: My DOS turtle needs to become an NT turtle


Powered by phpBB® Forum Software