如何在游戏板模拟中移动海龟?

时间:2018-12-27 15:51:06

标签: netlogo

我正在创建类似于垄断的棋盘游戏模拟,其中棋子在棋盘上移动。我的乌龟充当用户的游戏角色,并在世界边界内移动。边框以交替的颜色设置。我有一个按钮可以生成乌龟移动的步数。这些步骤存储为全局变量dice-num。但是,当乌龟降落在补丁(16 -16)(棋盘的右下角)之前的补丁上并且需要移动的步数超过1时,我无法将乌龟的航向转向一半向上移动董事会。结果,它只能移回木板的开始。

我试图分别处理每种情况:

     ;if the turtle lands on the patch before the corner
     ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1 
       [setxy max-pxcor min-pycor
         set dice-num dice-num - 1 
       show dice-num
         ]
       ] 

; dice-num是指乌龟移动的步数

到目前为止,这里有我的代码:

    to setup
     board
    end

  to go
   dice-roll
  end 

  to board
  ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min- 
  pxcor or pycor = min-pycor
  [set pcolor blue]]

ask patches [if pycor = max-pycor or pycor = min-pycor
 [if pxcor mod 2 = 0
   [set pcolor orange]]]

ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
 [if pycor mod 2 = 0
   [set pcolor orange]]]

 ask patch min-pxcor min-pycor [set pcolor green]
end

to dice-roll
  set dice [1 2 3 4 5 6]
  set dice-num one-of dice
  user-message (word "You rolled: " dice-num)
  ask turtle 0 [
  fd dice-num
  ]

 ;allows the turtle to turn if it lands on a corner
 ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
  if xcor = max-pxcor and ycor = max-pycor [set heading 270] 
  if xcor = min-pxcor and ycor = max-pycor [set heading 180]
 ;add a statement to end game once player rereaches the green patch
 ]

 ;if the turtle lands on the patch before the corner
 ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1 
   [setxy max-pxcor min-pycor
 set dice-num dice-num - 1 
 show dice-num
   ]
 ]
 end

我希望输出结果是乌龟一旦降落在贴片上(15 -16)并获得大于1的骰子数,便开始在板的右侧向上移动。但是,当乌龟降落时在补丁(15 -16)上并且骰子数大于1时,它简单地移回了棋盘的开头。

1 个答案:

答案 0 :(得分:1)

好的,这简直太久了,无法发表评论,所以我不得不做出一些猜测。我希望问题是您在检查位置后要转发全部的模辊。这意味着您必须测试所有可能出现的潜在补丁。相反,在移动下一个补丁之前,先移动一个补丁并测试位置会更容易。这是实现此移动和检查过程的代码的清理版本。

to setup
  clear-all
  board
  ask one-of patches with [pcolor = green]
  [ sprout 1
    [ set color white
      set heading 90
      forward 1
    ]
  ]
end

to go
  let die-num dice-roll
  ask one-of turtles [ move die-num ]
end 

to board
  ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
  [ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
  ask patches with [ pycor = max-pycor or pycor = min-pycor ]
  [ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
  ask patch min-pxcor min-pycor [set pcolor green]
end

to-report dice-roll
  let dice-num one-of [1 2 3 4 5 6]
  user-message (word "You rolled: " dice-num)
  report dice-num
end

to move [#roll]
  while [ #roll > 0 ]
  [ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
    if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
    if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
    if pxcor = min-pxcor and pycor = min-pycor [ stop ]
    fd 1
    set #roll #roll - 1
  ]
end

另一种选择是简单地将模具卷添加到当前位置,然后查看它是否已过去。如果是这样,请计算您想要去的地方。

相关问题