2015-12-14 LabN06

posted Dec 14, 2015, 4:45 AM by Samuel Konstantinovich   [ updated Dec 14, 2015, 6:58 AM ]
Goal: Patches can vote

Do Now:

Submit labN04, labN05 on homework server NOW!!!!!

Patches have two useful functions:


Check them out in the dictionary.

Local Variables:

This code is VERY hard to read:

if count turtles with [ abs pxcor + abs pycor > 20 ] < 10
   [ function1]
if count turtles with [ abs pxcor + abs pycor > 20 ] >= 10 and count turtles with [ abs pxcor + abs pycor > 20 ] < 100
  [ function2]

There is a repeating complicated calculation being made, that makes the rest of the code more difficult to read. We can replace this with a local variable to make it much more clear:
let badTurtles count turtles with [ abs pxcor + abs pycor > 20 ]
if badTurtles < 10
   [ function1]
if badTurtles >= 10 and badTurtles < 100
  [ function2]

You can also store random values, to use them in multiple places:

;a b and c have equal chance of being run:
let n random 3
if n = 0 [ a ]
if n = 1 [ b ]
if n = 2 [ c ]

-LabN06 Voting-

Phase I - 2 color voting

Assign each patch a pcolor of red or blue randomly.

Before you write more:
Patches must own some variables for this lab. They need to keep track of how many of their neighbors are a specific pcolor.


1. Make every patch to look at how many red/blue neighbors they have. They need to remember this number for later.

2. Make every patch to change their color if there is a *majority. They should stay the same if there is a tie. ( simple majority, 5 or more of one color.)

You must make sure step 1 completes before you start step 2. Separate these actions, because you don't want to co or you will count the UPDATED colors of neighbors, which is not the desired effect.

Phase II - 3 color voting

Change the setup: 
Make the patches red, blue, green randomly.

Change the Go: 
If there is a majority of any one color ( more than 4 red, more than 4 blue, or more than 4 green ) then change to that color.