-- reslove maze
--]]
path={};
search_count = 1;
search_pos={x=start_pos.x, y=start_pos.y, dir=0};
table.insert(path,search_pos);
function read_north()
if( search_pos.y == 1 ) then return end;
if( maze[search_pos.x][search_pos.y-1].s == true ) then
return;
else
if ( maze[search_pos.x][search_pos.y-1].v == true ) then
maze[search_pos.x][search_pos.y-1].v = false;
local node = {x=search_pos.x, y=search_pos.y-1, dir=3, r=search_count};
table.insert(path,node);
end
end
end
function read_south()
if( search_pos.y == size.y ) then return end;
if( maze[search_pos.x][search_pos.y].s == true ) then
return;
else
if ( maze[search_pos.x][search_pos.y+1].v == true ) then
maze[search_pos.x][search_pos.y+1].v = false;
local node = {x=search_pos.x, y=search_pos.y+1, dir=1, r=search_count};
table.insert(path,node);
end
end
end
function read_east()
if( search_pos.x == size.x ) then return end;
if( maze[search_pos.x][search_pos.y].e == true ) then
return;
else
if ( maze[search_pos.x+1][search_pos.y].v == true ) then
maze[search_pos.x+1][search_pos.y].v = false;
local node = {x=search_pos.x+1, y=search_pos.y, dir=4, r=search_count};
table.insert(path,node);
end
end
end
function read_west()
if( search_pos.x == 1 ) then return end;
if( maze[search_pos.x-1][search_pos.y].e == true ) then
return;
else
if ( maze[search_pos.x-1][search_pos.y].v == true ) then
maze[search_pos.x-1][search_pos.y].v = false;
local node = {x=search_pos.x-1, y=search_pos.y, dir=2, r=search_count};
table.insert(path,node);
end
end
end
while ( not (search_pos.x == target_pos.x and search_pos.y == target_pos.y) ) do
read_north();
read_south();
read_east();
read_west();
search_count = search_count + 1;
search_pos.x = path[search_count].x;
search_pos.y = path[search_count].y;
search_pos.dir = path[search_count].dir;
-- print(search_count, search_pos.x, search_pos.y, path[search_count].r );
end
修改為使用訪問過的座標標記的方式。這樣可以解有廻路的迷宮。
沒有留言:
張貼留言