使用netlogo获取全局变量的颜色

时间:2019-02-18 22:34:42

标签: netlogo

我正在尝试获取全局变量的真实颜色。

这是我的代码:

breed [players player]

globals [
INITIAL-POSSESSION ;
]

to setup
  clear-all
  reset-ticks
  set-initial-possession
end

to go 
  ticks
  ask players [
  decision
  ]
end


to-report initial-possession
  report random 2
end

to set-initial-possession
  ifelse initial-possession = 1
  [ 
    set INITIAL-POSSESSION black]
  [ 
    set INITIAL-POSSESSION white]
end


to decision
 if ([color] of INITIAL-POSSESSION) = black
 [] ;;do something
if ([color] of INITIAL-POSSESSION) = white
[];;do something
end

但我收到此错误:

  

OF预期输入为乌龟代理集或链接代理集或乌龟   或链接,但得到的是数字0。

所以我用(并且可以)更改它:

to decision
 if INITIAL-POSSESSION = 0
  []
 if INITIAL-POSSESSION = 9.9
  []
end

但是还有其他方法可以做到吗? (我正在使用netlogo 6.0)

1 个答案:

答案 0 :(得分:2)

我认为可能缺少一些代码,所以无法确认,但是看来您可能没有将BALL-OWNER设置为乌龟或补丁,而是直接向该变量分配了一个值。 of从代理查询变量(或从代理集获取变量列表),因此,如果将BALL-OWNER设置为值,则NetLogo会感到困惑。但是,如果确实将代理分配给BALL-OWNER,则您的方法应该可以正常工作。例如,尝试运行以下代码:

to setup
  ca
  crt 10 [ 
    setxy random-xcor random-ycor
    set color one-of [ red blue ] 
  ]
  reset-ticks
end

to go
  let ball-owner one-of turtles

  ifelse [color] of ball-owner = red [
    print "red team has possession"
  ] [
    print "blue team has possession"
  ]
end

编辑:您可以像在第二个代码块中一样使用global来选择颜色-我只想指出of是专门为绑定到agents。如果您想将颜色存储在global变量中进行比较,那是可能的,只是您的比较比使用of更简单:

globals [ initial-possession ]

to setup
  ca
  crt 3
  set-initial-possession
  reset-ticks
end

to go 
  ask turtles [
    decision
  ]
end

to set-initial-possession
  set initial-possession ifelse-value ( random 2 = 1 ) [black] [white]
end


to decision
  ifelse initial-possession = black [
    print "I see that black has possession"
  ] [
    print "I see that white has possession"
  ]
end

我不确定是否有帮助,这可能取决于您首先将颜色存储在global中的目的!