1

I'm trying to find a way to loop specific code blocks without losing any of the recorded attributes that were done inside the block.

i.e. my code flows like this:

setup environment (button)

setup drivers (button)

go: (button which incorporates the below)

1. trip assign

2. move-drivers

3. check-distance

tick

end

The code starts out with a number of drivers put in by the user and once I hit go after setup they get assigned a specific trip with variables like a color relating to a specific purpose, drive distance limits etc and then start to move in a systematic way. Every forward movement is recorded as +1 drive distance and once they hit the limit assigned they stop.

The issue is that once each driver stops (reaches distance lim) I want to record this as 1 trip for them and to loop (# of times determined by a variable I made ) them back to the trip assignment, motion and check distance portion (with new fresh assignments) without losing my total distance figures or any of the variables they've recorded. i.e. I want to simulate multiple trips for each driver recorded within the one run.

Nii Mantse
  • 2,554
  • 2
  • 13
  • 10
RMurray24
  • 21
  • 2

1 Answers1

0

I'm not sure about your code's detail, but you can try adding another property to drivers, taking out the trip-assign from go, and not using stop for drivers. Here is the example :

; assume you specify your turtle breed as drivers
drivers-own
[ total-distance ]

to setup-environment
  ; write your original code
end

to setup-drivers
  ; write your original code
  trip-assign
end

to go
  move-drivers
  check-distance
  tick
end

to check-distance
  ask drivers
  [ if distance = distance-limit
    [ set total-distance (total-distance + distance)
      set distance 0 ;reset the distance ​]
 ​   trip-assign ;assign again for next trip
  ]
end
bee
  • 41
  • 4