Fher
Fher

Reputation: 35

Netlogo - Create only one turtle per patch

I'm trying to create turtles in an office space area where designated grey patches represent their cubicles. However, when the turtles are created some overlap on top of each other within a cubicle.

How can I ensure only one turtle is created per cubicle? (find the code under ***)

This is the code I have:

breed [programmers programmer]
globals []

patches-own [
  a-space
  b-space
  c-space
  d-space
  a-cubicle
  b-cubicle
  c-cubicle
  d-cubicle
  cubicle?]

programmers-own [
  speed
  team-name
  motivation
  myCubicle
  teammates
  projects
  counter ]

to setup-environment
  ask patches with [ pycor mod 2 = 0 and pxcor = -16]   [ set pcolor grey ]
  ask patches with [ pycor mod 2 = 0 and pxcor = 16]   [ set pcolor grey ]
  ask patches with [ pxcor mod 2 = 0 and pycor = -16]   [ set pcolor grey ]
  ask patches with [ pxcor mod 2 = 0 and pycor = 16]   [ set pcolor grey ]
  ask patches with [ pycor = 0]   [ set pcolor red ]
  ask patches with [ pxcor = 0]   [ set pcolor red ]
  ask patches [
    set a-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor > 0) and (pycor < 14)]
    set b-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor > 0) and (pycor < 14)]
    set c-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor < 0) and (pycor > -14)]
    set d-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor < 0) and (pycor > -14)]
    set a-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor <= 16) and (pxcor < 0) and (pycor > 0)]
    set b-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor <= 16) and (pxcor > 0) and (pycor > 0)]
    set c-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor >= -16) and (pxcor > 0) and (pycor < 0)]
    set d-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor >= -16) and (pxcor < 0) and (pycor < 0)]   ]
  ask patches with [pcolor = grey] [set cubicle? true]
end

to setup-programmers
  create-programmers Number_Of_Programmers [
    set shape "person"
    ask n-of (Number_Of_Programmers / 4) programmers [set team-name "A"]
    ask n-of (Number_Of_Programmers / 4) programmers [set team-name "B"]
    ask n-of (Number_Of_Programmers / 4) programmers [set team-name "C"]
    ask n-of (Number_Of_Programmers / 4) programmers [set team-name "D"]

    ; *** WHAT I'M TRYING TO DO WOULD GO HERE ***
    ask programmers with [team-name = "A"]  [ set color blue] if other turtles-on patch-here [ move-to one-of a-cubicle with [cubicle? = true] set myCubicle patch-here]
    ask programmers with [team-name = "B"] [ set color green move-to one-of b-cubicle with [cubicle? = true] set myCubicle patch-here]
    ask programmers with [team-name = "C"] [ set color pink move-to one-of c-cubicle with [cubicle? = true] set myCubicle patch-here]
    ask programmers with [team-name = "D"] [ set color yellow move-to one-of d-cubicle with [cubicle? = true] set myCubicle patch-here]
    ask programmers [set teammates programmers with [color = [color] of myself]]
    set projects (list)
    set motivation 100
    set counter 0
    ]
end

to setup
  clear-all
  reset-ticks
  setup-environment
  setup-programmers
end

to go
  lose-or-gain-motivation
  socialize
  complete-program
  tick
end

Upvotes: 0

Views: 548

Answers (1)

JenB
JenB

Reputation: 17678

There are three different ways to create turtle agents in NetLogo. If you want want no more than one turtle in a patch, the easiest way to do so is to select a subset of patches (using n-of XXX patches) and ask them to sprout turtles. In your case, something like this might work:

Replace create-programmers Number_Of_Programmers

with

ask n-of Number_Of_Programmers patches with [cubicle?]
[ sprout-programmers 1

Upvotes: 2

Related Questions