2016-02-05 78 views
0

我的代码正在出现这个奇怪的问题。同时运行“相似”代码的两只海龟

在我的模特中,我有女性(设置女性为true)和男性(设为男性为true)。

在特定的触发器上,每个'将成为'分散器(设置分散器真)。

分散器和非分散器有非常不同的行为。

我试着解决这个代表两个类的不同品种,但这也没有奏效。

问题是,其中一只乌龟例如男性,将会相应地操作,并在遇到对面的乌龟时将分散器设置为“假”。但其他不会,并将继续与分散器设置'真'

我确定这是因为一旦海龟操作代码,它不再是'分散器',因此不再适用于搜索条件之后的乌龟,但是周围的所有工作尝试都导致了相同的问题或者没有人犯错。

to search-for partner 
    if male = true [ set potential-mates other turtles with [female = true 
    and disperser = true] 
    if female = true [ set potential-mates other turtles with [male = true 
    and disperser = true] 

    let chosen-mate min-one-of potential-mates [distance myself] 

    if any? potential-mates [ 
     set heading towards chosen-mate] 

    if male = true [ if any? other turtles-here with [female = true and 
     disperser = true] [set disperser false] 

    if female = true [ if any? other turtles-here with [male = true and 
     disperser = true] [set disperser false] 
end 

回答

2

我并不完全相信我知道你想做什么,但我想你是要求为两只海龟设置分散器为假。在这种情况下你想要的代码看起来是这样的(未测试):

if male 
[ let my-partner one-of other turtles-here with [female and disperser ] 
    if my-partner != nobody 
    [ set disperser false 
    ask partner [ set disperser false ] 
    ] 
] 

你或许应该还要考虑分开从“找到合作伙伴”代码“寻找合作伙伴”的代码。此外,您最好让他们检查他们是否有合作伙伴,然后通过将标题设置为最接近和前进来搜索合作伙伴。目前他们环顾四周,但不去任何地方。

+0

欢呼声,完美的作品 –