【发布时间】:2022-10-15 01:22:59
【问题描述】:
我正在模拟三个有乘客的公交车站。当一个汽车站的所有乘客都上车时,我希望公共汽车(一只乌龟)开走。这应该同时发生,但目前不是。公共汽车一个接一个地离开。有谁知道我做错了什么?提前致谢!我的代码:
globals [time]
turtles-own [target]
breed [bus busses]
to setup
clear-all
;; above
ask patches with [pxcor = 2 and pycor = 6][set pcolor white]
ask patches with [pxcor = 2 and pycor = 5][set pcolor white]
ask patches with [pxcor = 0 and pycor = 5][set pcolor white]
ask patches with [pxcor = 0 and pycor = 6][set pcolor white]
ask patches with [pxcor = 1 and pycor = 5][set pcolor white]
ask patches with [pxcor = 1 and pycor = 6][set pcolor white]
ask patches with [pxcor = 3 and pycor = 5][set pcolor white]
ask patches with [pxcor = 3 and pycor = 6][set pcolor white]
ask patches with [pxcor = 4 and pycor = 5][set pcolor white]
ask patches with [pxcor = 4 and pycor = 6][set pcolor white]
ask patches with [pxcor = 5 and pycor = 5][set pcolor white]
ask patches with [pxcor = 5 and pycor = 6][set pcolor white]
ask patches with [pycor = 7][
set pcolor gray
]
;;middle
ask patches with [pxcor = 2 and pycor = -6][set pcolor white]
ask patches with [pxcor = 2 and pycor = -5][set pcolor white]
ask patches with [pxcor = 0 and pycor = -5][set pcolor white]
ask patches with [pxcor = 0 and pycor = -6][set pcolor white]
ask patches with [pxcor = 1 and pycor = -5][set pcolor white]
ask patches with [pxcor = 1 and pycor = -6][set pcolor white]
ask patches with [pxcor = 3 and pycor = -5][set pcolor white]
ask patches with [pxcor = 3 and pycor = -6][set pcolor white]
ask patches with [pxcor = 4 and pycor = -5][set pcolor white]
ask patches with [pxcor = 4 and pycor = -6][set pcolor white]
ask patches with [pxcor = 5 and pycor = -5][set pcolor white]
ask patches with [pxcor = 5 and pycor = -6][set pcolor white]
ask patches with [pycor = -4][
set pcolor gray
]
;;below
ask patches with [pxcor = 2 and pycor = -15][set pcolor white]
ask patches with [pxcor = 2 and pycor = -14][set pcolor white]
ask patches with [pxcor = 0 and pycor = -14][set pcolor white]
ask patches with [pxcor = 0 and pycor = -15][set pcolor white]
ask patches with [pxcor = 1 and pycor = -14][set pcolor white]
ask patches with [pxcor = 1 and pycor = -15][set pcolor white]
ask patches with [pxcor = 3 and pycor = -14][set pcolor white]
ask patches with [pxcor = 3 and pycor = -15][set pcolor white]
ask patches with [pxcor = 4 and pycor = -14][set pcolor white]
ask patches with [pxcor = 4 and pycor = -15][set pcolor white]
ask patches with [pxcor = 5 and pycor = -14][set pcolor white]
ask patches with [pxcor = 5 and pycor = -15][set pcolor white]
ask patches with [pycor = -13][
set pcolor gray
]
;; passengers above
ask n-of Passengers_2 patches with [pcolor = white and pycor > 0][
sprout 1[
set color grey
set size 1
set shape "person"
set target patches with [pxcor = 3 and pycor = 8]
]]
;; passengers middle
ask n-of Passengers_1 patches with [pcolor = white and pycor < 0 and pycor > -12][
sprout 1[
set color grey
set size 1
set shape "person"
set target patches with [pxcor = 3 and pycor = -3]
]]
;; passengers below
ask n-of Passengers_3 patches with [pcolor = white and pycor < -12][
sprout 1[
set color grey
set size 1
set shape "person"
set target patches with [pxcor = 3 and pycor = -12]
]]
;; bus above
create-bus Bus_2[
set color red
set size 5
set xcor 3
set ycor 8
set shape "bus"
set heading 90
]
;; bus middle
create-bus Bus_1[
set color red
set size 5
set xcor 3
set ycor -3
set shape "bus"
set heading 90
]
;; bus below
create-bus Bus_3[
set color red
set size 5
set xcor 3
set ycor -12
set shape "bus"
set heading 90
]
reset-ticks
end
to check-in
ask turtles with [pycor < 0 and pycor > -12 ] [ ;; middle
move-to one-of patches with [pxcor = 3 and pycor = -3]
if any? neighbors with [pxcor = 3 and pycor = -4] and shape != "bus" ;; if passenger neighbors this patch, it dies
[
die]
]
ask turtles with [ycor > 1 ] [ ;; above
move-to one-of patches with [pxcor = 3 and pycor = 8]
if any? neighbors with [pxcor = 4 and pycor = 8] and shape != "bus" ;; if passenger neighbors this patch, it dies
[
die]
]
ask turtles with [ycor < -12 ] [ ;; below
move-to one-of patches with [pxcor = 3 and pycor = -12]
if any? neighbors with [pxcor = 3 and pycor = -13] and shape != "bus" ;; if passenger neighbors this patch, it dies
[
die]
]
tick
end
to drive
set time ticks
if time > 0 [
ask turtles [
forward 33
]]
end
to go
check-in
drive
tick
end
【问题讨论】: