Courses/CS 461/Winter 2006/Bill Kunyiha/Homework3

From CSWiki

Jump to: navigation, search

For the assignment I modified the ants program to include a second food source. The ants go to the first food source and eat it the later go to the second food ours. Before modifying the code, the program looks like the fillowing.

Image:first.gif

After Changing the code, the program now has two sources of food with the ants moving to the first source and then to the second.

Image:second.gif

The source code is shown below.

breeds [
 leader
 followers
]
globals [
 nest-x nest-y    ;; location of center of nest
 food-x food-y    ;; location of center of food
 food-x2 food-y2   ;; location of center of food
 leader-heading   ;; heading of the leader ant
]

to setup
 ca
 set-default-shape turtles "bug"
 set nest-x 10 - screen-edge-x                      ;; set up nest and food locations
 set nest-y 0
 set food-x screen-edge-x - 20
 set food-y 0
 set food-x2 screen-edge-x - 7
 set food-y2 45
 ;; draw the nest in brown by stamping a circular
 ;; brown turtle
 
ask patch nest-x nest-y [
   sprout 1 [
     set color brown
     set shape "circle"
     set size 10
     stamp
     die
   ]
 ]
 ;; draw the nest in orange by stamping a circular
 ;; orange turtle
  
ask patch food-x food-y [
   sprout 1 [
     set color orange
     set shape "circle"
     set size 10
     stamp
     die
   ]
 ]
 
ask patch food-x2 food-y2 [
   sprout 1 [
     set color green
     set shape "circle"
     set size 10
     stamp
     die
   ]
 ]
 
 create-custom-leader 1
   [ set color red                                ;; leader ant is red
     set size 2
     wiggle 50 ]                                  ;; ...and starts out with a random heading
 create-custom-followers (num-ants - 1)
   [ set size 2
     set color yellow ]                           ;; middle ants are yellow
 ask turtles
   [ setxy nest-x nest-y                          ;; start the ants out at the nest
     set heading 90 ]
 ask turtle (num-ants - 1)
   [ set color blue                               ;; last ant is blue
     set pen-size 2
     pd ]                                         ;; ...and leaves a trail
 ask leader
   [ set pen-size 2
     pd ]                                         ;; the leader also leaves a trail
 set leader-heading heading-of turtle 0
 end

 to go
 if not any? turtles with [xcor < food-x]
   [ stop ]
   ;if not any? turtles with [xcor < food-x] 
   ;[ wait 5 ]
   ask leader                                       ;; the leader ant wiggles and moves
    [ wiggle leader-wiggle-angle
      keep-in-bounds
      if (xcor > (food-x - 30 ))                    ;; leader heads straight for food, if it is close
         and (distancexy-nowrap food-x food-y > 0)
        [ set heading towardsxy-nowrap food-x food-y ]
      if xcor < food-x                             ;; do nothing if you're at or past the food
        [ fd 0.5 ] ]
 
 ask followers
    [ if distance turtle (who - 1) > 0
        [ set heading towards turtle (who - 1) ]   ;; follower ants follow the ant ahead of them
      if time-to-start? and (xcor < food-x)        ;; followers wait a bit before leaving nest
        [ fd 0.5 ] ]
  set leader-heading heading-of turtle 0
  ;;########################################
  ;;########################################
  if not any? turtles with [xcor < food-x2]
   [ stop ]
  ask leader                                       ;; the leader ant wiggles and moves
    [ wiggle leader-wiggle-angle
      keep-in-bounds
      if (xcor > (food-x2 - 5 ))                    ;; leader heads straight for food, if it is close
         and (distancexy-nowrap food-x2 food-y2 > 0)
        [ set heading towardsxy-nowrap food-x2 food-y2 ]
      if xcor < food-x2                             ;; do nothing if you're at or past the food
        [ fd 0.5 ] ]
   
 ask followers
    [ if distance turtle (who - 1) > 0
        [ set heading towards turtle (who - 1) ]   ;; follower ants follow the ant ahead of them
      if time-to-start? and (xcor < food-x2)        ;; followers wait a bit before leaving nest
        [ fd 0.5 ] ]
  set leader-heading heading-of turtle 0
  ;;########################################
  ;;########################################
end
;; turtle procedure; wiggle a random amount, averaging zero turn
 
 to wiggle [angle]
 rt random-float angle
 lt random-float angle
end
;; turtle procedure; keep the ants from wrapping the screen
 
 to keep-in-bounds
 ifelse heading > 180
   [ rt 180 ]
   [ if ycor > (screen-edge-x - 5)
       [ rt 100 ]
    if ycor < (5 - screen-edge-x)
       [ lt 100 ] ]
 end
 ;; turtle reporter; if true, then the ant is authorized to move out of the nest
 
 to-report time-to-start?
 report (xcor-of (turtle (who - 1))) > (nest-x + start-delay + random start-delay )
end
Personal tools