How to kill turtles when they touch?

偶尔善良 提交于 2019-12-10 18:39:46

问题


I can't seem to get turtles to die when they touch each other. I can only kill them when they are on the same patch. Is there any function like this?

to killturtles
if contact?
[die]
end

回答1:


There is not but assuming your turtles are round or roundish

 ask other turtles in-radius (size / 2) [die]

is a good approximation.




回答2:


If "touch" means that the turtle icons are overlapping, you might need something like the method that King-Ink suggests (but there could be a complication like the one described below). If "touch" means "on the next patch", then you can use neighbors and neighbors4 along with turtles-here, but the way you use them it depends on exactly what you want to do. Here's an illustration. This command

ask turtles [ask neighbors4 [ask turtles-here [die]]]

will ensure that no turtle has another turtle next to it or above it or below it. If you replace neighbors4 by neighbors, then there won't be any turtles diagonally next to each other, either. neighbors4 and neighbors get patches that are adjacent to the patch a turtle is on, and turtles-here lists the turtles that are on a patch. So the entire line goes to each turtle, one by one, and checks whether there are turtles in adjacent patches. If so, those turtles are killed, but the first turtle remains alive.

However, you may want to kill the first turtle, too. Here's what I'm wondering, though. Suppose, for example that three turtles are on three adjacent patches [T1][T2][T3]. T1 is next to T2, and T2 is next to T3. Do you want to kill all three? If so, that requires some care. Suppose that your code loops through each turtle using ask or a loop. Your code finds that T1 and and T2 are next to each other, and kills them. In the next step, when your code finds T3, it no longer has a turtle next to it, so it won't be killed. Is that what you want? If so, that's not hard.

However, if you want to kill every turtle that is next to another turtle, then I think you may have to give each turtle a variable, say, ready-to-die?. When you find that there are turtles next to each other, set their ready-to-die? variables to true. Then, in a second step, kill the turtles that have ready-to-die? equal to true:

turtles-own [ready-to-die?]

to setup
  <make some turtles>
  ask turtles [set ready-to-die? false]
  ; don't forget to initialize ready-to-die? in new turtles created later
end

to go
  <do things with turtles>
  ask turtles [ask neighbors4 [ask turtles-here [set ready-to-die? true]]]
  ask turtles with [ready-to-die?] [die]
  <do other things>
end

Each turtle is told by any neighboring turtles it has, that it is ready to die. It stays alive, though, so that other turtles will know that it's a neighbor. Then after all turtles with neighbors know that they are ready to die, they're asked to die.

There are other ways to write the part with the embedded asks. Some might be better, but this code illustrates tools that you can use.



来源:https://stackoverflow.com/questions/34926964/how-to-kill-turtles-when-they-touch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!