minecraft吧 关注:2,547,303贴子:31,848,304
  • 6回复贴,共1

【新人渣作】computercraft走迷宫程序

取消只看楼主收藏回复

1L防吞


来自手机贴吧1楼2015-01-08 22:02回复
    嗯…期末考挂科补考不开心,晚上就写了这个发泄。
    电脑模组大家都不陌生,我就不多说了。
    可以走每条歧路都是死路的竖直放置迷宫。


    来自手机贴吧3楼2015-01-08 22:04
    回复
      废话不多说,上代码。
      local x,y,tx,ty,dirx
      dirx = 1
      x = 0
      y = 0
      function onTarget()
      print('x',x,'y',y)
      print('tx',tx,'ty',ty)
      if x ~= tx then return false end
      if y ~= ty then return false end
      return true
      end
      function change()
      turtle.turnLeft()
      turtle.turnLeft()
      dirx = -dirx
      end
      function move(d)
      if d == 0 then
      while not onTarget() do
      print('forward...')
      if not turtle.detectDown() then
      change()
      while not turtle.down() do turtle.attack() end
      y = y - 1
      move(3)
      elseif turtle.detect() then move(1)
      else
      while not turtle.forward() do turtle.attack() end
      x = x + dirx
      end
      end
      elseif d == 1 then
      while not onTarget() do
      print('up...')
      if not turtle.detect() then
      while not turtle.forward() do turtle.attack() end
      x = x + dirx
      move(0)
      elseif turtle.detectUp() then
      change()
      move(2)
      else
      while not turtle.up() do turtle.attack() end
      y = y + 1
      end
      end
      elseif d == 2 then
      while not onTarget() do
      print('forward head...')
      if not turtle.detectUp() then
      change()
      while not turtle.up() do turtle.attack() end
      y = y + 1
      move(1)
      elseif turtle.detect() then move(3)
      else
      while not turtle.forward() do turtle.attack() end
      x = x + dirx
      end
      end
      elseif d == 3 then
      while not onTarget() do
      print('down')
      if not turtle.detect() then
      while not turtle.forward() do turtle.attack() end
      x = x + dirx
      move(2)
      elseif turtle.detectDown() then
      change()
      move(0)
      else
      while not turtle.down() do turtle.attack() end
      y = y - 1
      end
      end
      end
      end
      tx = 5
      ty = 12
      move(0)


      来自手机贴吧4楼2015-01-08 22:05
      回复
        嗯…看起来不支持行首空格…缩进还请多多包涵啊……通过设置tx和ty的值来确定终点,起点坐标0,0。


        来自手机贴吧5楼2015-01-08 22:08
        回复
          从代码可以很容易理解我的逻辑,就是摸墙走迷宫…因为树形结构的迷宫墙必然是连为一体的,所以只要一直沿着墙走就能走到终点。


          来自手机贴吧6楼2015-01-08 22:08
          回复
            然后就没啥好说的了,自己搭建迷宫玩吧。。。刚刚开始玩cc模组,技术还不好,大触们轻喷。。。


            来自手机贴吧7楼2015-01-08 22:10
            回复
              虽然只是贴墙走,没有把每一个道路分叉口当作树的一个节点处理,但是运动轨迹上来看可以当作是一个DFS(深度优先搜索)。


              来自手机贴吧11楼2015-01-08 22:47
              收起回复