下面的答案強調簡單的新手。所以它只涉及提出的非常具體的問題。
to-report choose-neighbor
let _candidates neighbors4 with [pcolor = black]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = brown]
if any? _candidates [report one-of _candidates]
set _candidates neighbors4 with [pcolor = white]
if any? _candidates [report one-of _candidates]
report nobody
end
你會注意到這段代碼有很多重複。如果將這樣的重複打包成一個子程序可能是一個好主意,例如
to-report choose-nbr [#color]
let _candidates neighbors4 with [pcolor = #color]
report ifelse-value (any? _candidates) [one-of _candidates] [nobody]
end
我能夠按照這裏的邏輯。我現在要試試這個。非常感謝你! – HS3