0

我正在使用 Netlogo 为我的大学完成这项任务,但我真的被困住了。我刚开始使用 Netlogo,我正试图与一些朝圣者一起重建 Mekka。

我一直在尝试很多不同的代码,添加新代码,尝试一下,删除一些代码,但这是我到目前为止想出的:

 turtles-own
[direction                ;;  1 follows right-hand wall, -1 follows left-hand wall
 way-is-clear?            ;; reporter - true if no wall ahead
 checked-following-wall?]

globals [halfedge]

breed [agents agent ] 

agents-own [ around visible ]

to setup
  create-agents 500 [
    set color green
    set size 2
    ; distribute agents randomly
    setxy pxcor = halfedge pycor = halfedge 
    set heading random 360
    ; ensure that each is on its own patch
    while [any? other agents-here] [ fd 1 ]

  ]
end

to bounce 
  if [pcolor] of patch-at dx 0 = blue [
    set heading (- heading)
  ]
  if [pcolor] of patch-at 0 dy = blue [
    set heading (180 - heading)
  ]
end

to go 
  ask agents [ count-those-around ]
  ask agents [ move ]
end

; store the number of agents surrounding me within
; local-radius units
; and the agents that I can see within visible-radius

to count-those-around
  set around count agents with [self != myself] in-radius 
  local-radius
  set visible agents with [self != myself] in-radius 
  visible-radius
end

to move 
   ;; turn right if necessary
  if not wall? (90 * direction) and wall? (135 * direction) [ rt 90 * direction ]
  ;; turn left if necessary (sometimes more than once)
  while [wall? 0] [ lt 90 * direction ]
  ;; move forward
  fd 1
end

; face towards the most popular local spot 
to face-towards
  face max-one-of visible [around]
end
; face away from the most popular local spot
to face-away
  set heading towards max-one-of visible [around] - 180
end 

to setup-center
   clear-all
  set halfedge int (edge / 2)
  ask patches[
    if (pxcor = (- halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
      [set pcolor blue]                                ;; ... draws left edge in blue
    if ( pxcor = (0 + halfedge) and pycor >= (- halfedge) and pycor <= (0 + halfedge) )
      [set pcolor blue]                                ;; ... draws right edge in blue
    if ( pycor = (- halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
      [set pcolor blue]                                ;; ... draws bottom edge in blue
    if ( pycor = (0 + halfedge) and pxcor >= (- halfedge) and pxcor <= (0 + halfedge) )
      [set pcolor blue]                                ;; ... draws upper edge in blue
    ]
end
  • 这个想法是,首先,设置一个类似于天房的正方形。
  • 之后,海龟就设置好了。
  • 他们应该都以逆时针方向绕着墙走。
  • 应该有一位“领袖”带领所有朝圣者在天房周围。

现在天房已成功绘制,唯一的问题是海龟不应该在那里产卵或撞到它(因此是凹凸代码)。此外,它们随机四处走动,我不知道如何让它们以 Counter-Clickwise 的形式移动,跟随一个不同颜色的领导者。

你们中的任何人都可以帮助我吗?我将永远感激不尽!

4

1 回答 1

2

您可能试图通过一次编写一个大程序来一次学习太多东西。

从编写一个非常小的程序开始;让它工作;尝试对其进行非常小的改进,并使其发挥作用;等等。如果您在任何时候卡住了,请到这里,显示您的代码最多有一件事情被破坏,并就您目前遇到的一个问题提出一个问题。这是获得帮助的最有效方式。

一些随机编码技巧:

这不是有效的代码:

setxy pxcor = halfedge pycor = halfedge

setxy需要两个数字,但您要传递两个布尔值:pxcor = halfedge是真还是假,pycor = halfedge也是真或假。我想你的意思可能只是setxy halfedge halfedge.

agents with [self != myself]可以简单地替换为other agents.

于 2014-04-30T22:44:13.467 回答