我真的是 NetLogo 编程的新手,我需要帮助。这只是我的第二个任务,我完成了大部分任务。我不得不让机器人在迷宫中行走。机器人只能在黑色斑块上行走(紫色斑块代表障碍物)。机器人可以前进、后退、左转和右转,它必须到达目标。当涉及到目标时,它必须停止。在作业的第一部分,我必须制作程序“迷宫”,它将挑选 15 个随机补丁并将它们涂成紫色(紫色代表障碍物),包括一个代表目标的绿色补丁。每次我调用该程序时,它都会给我带来不同的迷宫。我需要两件事的帮助:
我必须做一个新的程序,它总是会给我同样的迷宫(总是相同的 15 个紫色随机补丁)
我必须创建一个新程序“搜索”,这将使机器人走到目标,因为我只调用一次该程序。机器人必须环顾四周,并始终指向有更多空间的方向。如果在他周围的每个方向上都有相同数量的空闲补丁,机器人必须随机选择它要去目标的方向。当涉及到目标时,它必须停止。
这是我的代码:
breed [robots robot]
robots-own [
target
zforward
zright
zleft]
to paint-walls
ask patches with
[(pxcor = max-pxcor) or (pxcor = min-pxcor) or (pycor = max-pycor) or (pycor =
min-pycor)]
[ set pcolor violet]
end
to labyrinth
ask n-of 15 patches with[ pcolor != violet ] [set pcolor violet]
ask n-of 1 patches with [pcolor != violet ] [ set pcolor green]
end
to create-agent
set-default-shape robots "robot"
ask patch 5 5 [ sprout-robots 1 ]
ask robots [
set heading 0
set color grey
set target false]
ask robots [ask patch-here [set pcolor black ] ]
end
to setup
clear-all
paint-walls
labyrinth
create-agent
end
to forward
ask robot 0 [if [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 = green
[fd 1]]
check
end
to backward
ask robot 0 [if [pcolor] of patch-ahead -1 = black or [pcolor] of patch-ahead 1 =
green[back 1 ]]
check
end
to rot-right
ask robot 0 [right 90 ]
end
to rot-left
ask robot 0 [left 90 ]
end
to right
ask robot 0[rot-right
if [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 = green[
forward]]
check
end
to left
ask robot 0 [rot-left
if [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 = green[
forward]]
check
end
to check-target
ask robot 0[ifelse [pcolor = green ] of patch-here
[set target true]
[set target false]]
end
to check-forward
ask robot 0 [ifelse [pcolor] of patch-ahead 1 = black or [pcolor] of patch-ahead 1 =
green
[ifelse [pcolor] of patch-ahead 2 = black or [pcolor] of patch-ahead 2 = green
[set zforward 2]
[set zforward 1]]
[set zforward 0]]
end
to check-right
ask robot 0[ifelse [pcolor = black] of patch-right-and-ahead 90 1[
ifelse [pcolor = black] of patch-right-and-ahead 90 2
[set zright 2]
[set zright 1]]
[set zright 0]]
end
to check-left
ask robot 0[ifelse [pcolor = black] of patch-left-and-ahead 90 1[
ifelse [pcolor = black] of patch-left-and-ahead 90 2
[set zleft 2]
[set zleft 1]]
[set zleft 0]]
end
to check
check-forward
check-right
check-left
check-target
end