;; ;; Project variables are: dropcnt, totalTurtles ;; Turtle variables are: nextTurtle, prevTurtle, dropcnt ;; Run initit to create the above variables the first time. ;; ;; num and skip are sliders used to set the number of turtles and the ;; skip value. ;; ;; Main program starts here. ;;to begin hatch num "a ;; create num turtles numbereed from a0 to a'num - 1' settotalturtles num everyone [turtlesetup] ;; all turtles set themselves up setdropped 1 ;; used to keep track of the order that turtles are removed in a0, hotpotato skip ;; have the first turtle start us off end;; ;; Used to create :num number of turtles prefixed by :prefix ;;to hatch :num :prefix let [ci 5] dotimes [i :num] [newturtle word :prefix :i seth (360 / :num) * :i setc :ci + (:i * 10) if (remainder :i 14) = 0 [make "ci :ci + 1]] end;; ;; Used to get rid of all the turtlesto removeall everyone [remove who] clean end;; ;; Called by each turtle at start. ;;to turtleSetup setc "blue ;; set my color to blue fd 150 ;; move out 150 steps st ;; show mw;; ;; Create the links to the turtles just before and just after me ;;setnextTurtle 1 + bf who setprevTurtle (bf who) - 1;; ;; Used to make list circular. ;;if not (nextTurtle < totalTurtles) [setnextTurtle 0] if prevTurtle < 0 [setprevTurtle totalTurtles - 1] end;; ;; Each turtle in order runs the following. If the hot potato is 0 we ;; leave the ring else we pass the potato on to the nect turtle first ;; decrementing it by 1 ;;to hotPotato :hot ifelse :hot = 0 [dropMe] [ask word "a nextturtle list "hotpotato (:hot - 1)] end;; ;; We run this the hot potato was 0 when we got it. ;; if we point to ourselves then we are the last turtle standing, ;; program is over. ;; It there are still other turtles in the ring we drop out by asking ;; the turtle behind us to set its next pointer to the turtle in front ;; of us. We also ask the turtle in front to set its prev pointer to ;; the turtle behind us. Finally we change our color, remember what ;; order we were dropped in and have the next turtle start up again.to dropMe if (bf who) = nextturtle [setc "red setdropcnt dropped stop] ask word "a prevTurtle list "setnextturtle nextTurtle ask word "a nextturtle list "setprevturtle prevturtle setc 4 setdropcnt dropped setdropped dropped + 1 ask word "a nextturtle [hotpotato skip] end;; ;; Only run the very first time to create the project variables and ;; turtles own variables ;;to initit dolist [i [nextTurtle prevTurtle dropcnt]] [turtlesown :i] createprojectvar "totalTurtles createprojectvar "dropped end