跪求 mush lua 迷宮地圖 點到點的寫法
本帖最后由 stu 于 2015-10-6 03:27 PM 编辑小弟我沒程式語言基礎,從zmud跳過來mush,只會一些簡單的lua寫法
因nt系列的mud內迷宮大小不一定,所以想請教會的大大們該怎麼去弄
迷宮範本如下:
┌─┬─┬─┬─┬─┬─┬─┬─┐
│││ ││
├┼┼┼─┼┼─┼─┼┤
│ │ │││
├┼─┼─┼─┼┼─┼┼┤
│ ││ │
├┼─┼─┼─┼┼┼─┼┤
│ ││││ │
├┼─┼─┼┼┼┼─┼┤
│ │ ││
├┼┼┼─┼─┼┼─┼┤
││││ │ ││
├┼─┼┼┼┼─┼─┼┤
│ │ │ ││
├┼┼─┼┼┼┼┼─┤
││ ││││ │
└─┴─┴─┴─┴─┴─┴─┴─┘ 对于这类,正规的迷宫,采取一手扶墙策略可以遍历走出。
对于非正规的(存在回环,实时随机变换),就只能随机碰运气了。 扶着墙进去,扶着墙出来么? 随机走,总会出来的 本帖最后由 stu 于 2015-10-8 08:58 AM 编辑
假如地圖範例如下 :
┌─┬─┬─┬─┬─┐
│││ ││
├┼┼┼┼┤
│ │││
├┼─┼─┼┼┤
│││★ ││
├┼┼┼─┼┤
│ │ │
├┼┼─┼┼─┤
││ │ │
└─┴─┴─┴─┴─┘
★:代表當前位置
小弟我將上圖轉成數字陣列
1,0,1,0,1,1,1,0,1
1,0,1,0,1,0,1,0,1
1,1,1,1,1,0,1,0,1
1,0,0,0,0,0,1,0,1
1,0,1,0,2,1,1,0,1
1,0,1,0,1,0,0,0,1
1,1,1,0,1,1,1,1,1
1,0,1,0,0,0,1,0,0
1,0,1,1,1,0,1,1,1
0:無路
1:有路
2:當前未置
弄成座標位置是:
座標位置 = {東,西,南,北}
map = {0,0,0,1}
map = {1,0,0,1}
map = {0,1,0,0}
map = {1,0,0,1}
map = {0,1,0,0}
map = {1,0,1,1}
map = {0,1,1,1}
map = {1,0,0,1}
map = {1,1,1,0}
map = {0,1,0,1}
map = {0,0,1,1}
map = {0,0,1,0}
map = {1,0,1,0}
map = {0,1,0,1}
map = {0,0,1,1}
map = {1,0,1,1}
map = {1,1,0,1}
map = {0,1,0,1}
map = {0,0,1,1}
map = {0,0,1,1}
map = {0,0,1,0}
map = {0,0,1,0}
map = {1,0,1,0}
map = {0,1,1,0}
map = {0,0,1,0}
左下角座標是0,0右上角座標是4,4
如要從當前座標點2,2 走到座標點0,4的話
這部分的程式該怎麼著手去寫呢?
請各位大大幫忙解惑一下,謝謝 ! 有人告知說用遞迴的方式寫function出來跑
但沒寫過function 也不知道遞迴的概念
能請各位大大教一下小弟我該怎麼寫嗎XD 請問各位大大,下方的演算法如果要用lua表示該怎麼寫呢??
Procedure GO(maze[])
VISIT(maze, START_I, START_J, END_I, END_J)
Procedure VISIT(maze[], i, j, end_i, end_j)
IF maze == 0
maze = 1
IF maze == 0
IF !(VISIT(maze, i, j + 1, end_i, end_j) OR
VISIT(maze, i + 1, j, end_i, end_j) OR
VISIT(maze, i, j - 1, end_i, end_j) OR
VISIT(maze, i - 1, j, end_i, end_j))
maze = 0
RETURN maze == 1 本帖最后由 littleknife 于 2015-10-24 09:11 AM 编辑
回复 7# stu,未测试不一定正确:
function GO(maze, START_I, START_J, END_I, END_J)
VISIT(maze, START_I, START_J, END_I, END_J)
end
function VISIT(maze, i, j, end_i, end_j)
if maze == 0 then
maze = 1
if maze == 0 then
if (VISIT(maze, i, j + 1, end_i, end_j)==nil or
VISIT(maze, i + 1, j, end_i, end_j) or
VISIT(maze, i, j - 1, end_i, end_j) or
VISIT(maze, i - 1, j, end_i, end_j)) then
maze = 0
end
end
end
return maze == 1
end
页:
[1]