Advertisement
TeoremaPi

Laberinto (algoritmo Prim)

Apr 9th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. -- programa para crear un laberinto segun el metodo de Prim
  2. -- puede recargar materiales al final de cada linea
  3.  
  4. -- dimensiones maximas (incluyendo marco)
  5. -- y: hacia delante, x: hacia la derecha
  6. dimXMax = 7
  7. dimYMax = 13
  8.  
  9. -- dimensiones del laberinto (numero de pasillos)
  10. dimX = math.floor((dimXMax - 1)/2)
  11. dimY = math.floor((dimYMax - 1)/2)
  12.  
  13. -- semilla aleatoria
  14.  
  15. math.randomseed(0)
  16.  
  17. -- funcion para representar una matriz
  18. function printMatrix(matrix)
  19.     text = ""
  20.     color = ""
  21.     for i = 1, table.getn(matrix) do
  22.         text = matrix[i][1]
  23.         if matrix[i][1] == 1 then
  24.             color = "0"
  25.         else
  26.             color = "f"
  27.         end
  28.  
  29.             for j = 2, table.getn(matrix[i]) do
  30.                 text = text .. matrix[i][j]
  31.                 if matrix[i][j] == 1 then
  32.                     color = color .. "0"
  33.                 else
  34.                     color = color .. "f"
  35.                 end
  36.             end
  37.        
  38.         term.blit(text,color,color)
  39.         print(text)
  40.     end
  41. end
  42.  
  43.  
  44. -- funcion que devuelve una matriz con los vecinos de un punto
  45. -- cada fila corresponde a un vecino
  46. -- las columnas representan: fila, columna y valor
  47. function neig(ip,jp,matrix)
  48. -- ip = fila del punto estudiado
  49. -- jp = columna del punto estudiado
  50. -- matrix = matriz
  51.  
  52. -- calculamos el tamano de la matriz
  53. iMax = table.getn(matrix)
  54. jMax = table.getn(matrix[1])
  55.  
  56. -- matriz de desplazamientos para los vecinos
  57. neigMat = {{-1,0},{0,-1},{ 1,0},{0, 1}}
  58.  
  59. -- vector de vecinos
  60. neigs = {}
  61. for k = 1, 4 do
  62.     -- coordenadas del vecino
  63.     iNeig = ip + neigMat[k][1]
  64.     jNeig = jp + neigMat[k][2]
  65.  
  66.     -- si las coordenadas estan detro de la matriz
  67.     -- guardamos los datos
  68.     if iNeig>0 and iNeig<=iMax and jNeig>0 and jNeig<=jMax then
  69.         table.insert(neigs, {iNeig, jNeig, matrix[iNeig][jNeig]})
  70.     end
  71. end
  72.  
  73. -- devolvemos la matriz de vecinos
  74. return neigs
  75. end
  76.  
  77.  
  78. -- creamos una cuadricula de dimX de ancho y dimY de largo
  79.  
  80. maze = {}
  81. walls = {}
  82.  
  83. -- 0 = pared
  84. -- 1 = pasillo visitado
  85. -- 2 = pared seleccionable
  86. -- 3 = pasillo no visitado (sala)
  87. for i = 1, 2*dimY - 1 do
  88.     maze[i] = {}
  89.     for j = 1, 2*dimX - 1 do
  90.         if j%2 == 1 and i%2 == 1 then
  91.             maze[i][j] = 3
  92.         else
  93.             maze[i][j] = 0
  94.         end
  95.     end
  96. end
  97.  
  98. -- selecciono una sala, la anado al pasillo y sus
  99. -- paredes las anado a la lista de seleccionables
  100. iSelect = 2*math.random(0,dimY-1) + 1
  101. jSelect = 2*math.random(0,dimX-1) + 1
  102.  
  103. maze[iSelect][jSelect] = 1
  104.  
  105. neigs = neig(iSelect, jSelect, maze)
  106.  
  107. for kNeig = 1, table.getn(neigs) do
  108.     iNeig = neigs[kNeig][1]
  109.     jNeig = neigs[kNeig][2]
  110.  
  111.     maze[iNeig][jNeig] = 2
  112.  
  113.     table.insert(walls,{iNeig,jNeig})
  114. end
  115.  
  116. -- bucle principal
  117. -- mientras queden paredes seleccionables
  118. while table.getn(walls) > 0 do
  119.     -- selecciono una pared
  120.     wallIndex = math.random(table.getn(walls))
  121.     iWall = walls[wallIndex][1]
  122.     jWall = walls[wallIndex][2]
  123.    
  124.     -- contamos el numero de salas adyacentes
  125.     neigs = neig(iWall, jWall, maze)
  126.  
  127.     rooms = {}
  128.  
  129.     for k = 1, table.getn(neigs) do
  130.         if neigs[k][3] == 3 then
  131.             table.insert(rooms,neigs[k])
  132.         end
  133.     end
  134.  
  135.     -- si solo hay una sala
  136.     if table.getn(rooms) == 1 then
  137.         -- quitamos la pared que pasa a ser parte del pasillo
  138.         maze[iWall][jWall] = 1
  139.  
  140.         -- la sala pasa tambien a formar parte del pasillo
  141.         iRoom = rooms[1][1]
  142.         jRoom = rooms[1][2]
  143.  
  144.         maze[iRoom][jRoom] = 1
  145.  
  146.         -- las paredes adyacentes a la sala pasan a la lista de seleccionables
  147.         neigs = neig(iRoom, jRoom, maze)
  148.  
  149.         for k = 1, table.getn(neigs) do
  150.             if neigs[k][3] == 0 then
  151.                 iNeig = neigs[k][1]
  152.                 jNeig = neigs[k][2]
  153.                 table.insert(walls, {iNeig,jNeig})
  154.                 maze[iNeig][jNeig] = 2
  155.             end
  156.         end
  157.     else
  158.         -- si hay un numero distinto de salas adayacentes, lo volvemos un muro normal
  159.         maze[iWall][jWall] = 0
  160.     end
  161.  
  162.    
  163.         -- eliminamos de la lista la pared analizada
  164.         table.remove(walls,wallIndex)
  165.  
  166. end
  167.  
  168. printMatrix(maze)
  169.  
  170.  
  171.  
  172.  
  173. -- construccion del laberinto
  174. turtle.select(1)
  175.  
  176. -- funcion para buscar el siguiente slot con recursos
  177. -- si no quedan recursos devuelve false, en caso contrario true
  178. function placeBlock()
  179.     while turtle.getItemCount() == 0 do
  180.         turtle.select(turtle.getSelectedSlot() + 1)
  181.        
  182.         if turtle.getSelectedSlot() == 16 and turtle.getItemCount() == 0 then
  183.             return false
  184.         end
  185.     end
  186.     turtle.placeDown()
  187.     return true
  188. end
  189.  
  190.  
  191. -- funcion para recoger mas bloques para construir de un inventario al final de una linea
  192. function reBlocks()
  193.     if turtle.suck() then
  194.         turtle.select(1)
  195.         while turtle.suck() do
  196.         end
  197.     end
  198. end
  199.  
  200.  
  201. -- empezamos haciendo un marco
  202. placeBlock()
  203.  
  204. for k = 1, table.getn(maze) + 1 do
  205.     turtle.forward()
  206.     placeBlock()
  207. end
  208. turtle.turnRight()
  209.  
  210. for k = 1, table.getn(maze[1]) + 1 do
  211.     turtle.forward()
  212.     placeBlock()
  213. end
  214. turtle.turnRight()
  215.  
  216. for k = 1, table.getn(maze) + 1 do
  217.     turtle.forward()
  218.     placeBlock()
  219. end
  220. turtle.turnRight()
  221.  
  222. for k = 1, table.getn(maze[1]) do
  223.     turtle.forward()
  224.     placeBlock()
  225. end
  226. turtle.turnRight()
  227.  
  228.  
  229. -- contruimos el laberinto
  230. iMax = table.getn(maze)
  231. jMax = table.getn(maze[1])
  232.  
  233. turtle.forward()
  234. for j = 1, jMax do
  235.  
  236.     -- contruimos una linea
  237.     for ii = 1, iMax do
  238.         if j%2 == 1 then
  239.             i = iMax - ii + 1
  240.         else
  241.             i = ii
  242.         end
  243.  
  244.         if maze[i][j] == 0 then
  245.             placeBlock()
  246.         end
  247.  
  248.         turtle.forward()
  249.     end
  250.  
  251.     -- repostamos material
  252.     reBlocks()
  253.  
  254.     -- damos la vuelta
  255.     for k = 1,2 do
  256.         if j%2 == 1 then
  257.             turtle.turnRight()
  258.         else
  259.             turtle.turnLeft()
  260.         end
  261.         turtle.forward()
  262.     end
  263.  
  264. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement