Advertisement
jesusthekiller

Maze

Jun 14th, 2013
1,565
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: Maze
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 1.2
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.       1.0:
  17.         Initial Release
  18.       1.1:
  19.         Typos D:
  20.       1.2:
  21.         New logo
  22.         Time fixed
  23. ]]--
  24.  
  25. --[[
  26.     LICENSE:
  27.    
  28.     Maze
  29.     Copyright (c) 2013 Jesusthekiller
  30.  
  31.     This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  32.  
  33.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  34.  
  35.     See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  36. ]]--
  37.  
  38. -- The maze
  39.  
  40. -- The cprint
  41. local function cwrite(msg)
  42.     msg = tostring(msg)
  43.     local x, y = term.getCursorPos()
  44.     term.setCursorPos((51-#msg)/2, y)
  45.     write(msg)
  46. end
  47.  
  48. local function cprint(msg)
  49.     cwrite(msg.."\n")
  50. end
  51.  
  52. -- The logo
  53. local f = fs.open("maze_logo", "w") or error("Error while opening file maze_logo", 0)
  54. f.write("0000000000 0  0  0  0  0  0\n0    0   0 00 0 0 0 00 0 0 0\n0000 000 0 0 00 0 0 0 00  0\n0      0 0\n0 0000 0 0 0   0  0  00  000\n0   0    0 00 00 0 0  0  00\n0000000000 0 0 0 0 0  00 000")
  55. f.close()
  56.  
  57. f = paintutils.loadImage("maze_logo")
  58.  
  59. fs.delete("maze_logo")
  60.  
  61. -- The splash
  62. term.setBackgroundColor(colors.black)
  63. term.setTextColor(colors.white)
  64. term.clear()
  65.  
  66. term.setCursorPos(27, 8)
  67. print("Nano maze!")
  68.  
  69. paintutils.drawImage(f, 13, 5)
  70.  
  71. parallel.waitForAny(
  72.     function() coroutine.yield(); os.pullEvent("key"); coroutine.yield() end,
  73.     function() term.setBackgroundColor(colors.black); term.setTextColor(colors.white) while true do term.setCursorPos(18, 14); term.write("Press any key.."); sleep(0.5); term.clearLine(); sleep(0.5) end end
  74. )
  75.  
  76. -- The size
  77. local size
  78.  
  79. repeat
  80.     term.setCursorPos(1, 14)
  81.     term.clearLine()
  82.  
  83.     cwrite("Enter maze size (5-99):")
  84.     size = read()
  85.    
  86.     size = tonumber(size)
  87.     if not size then
  88.         size = 0
  89.     end
  90. until size > 4 and size < 100
  91.  
  92. -- The generate
  93. local function mazeGen(mx, my)
  94.    
  95.     --[[
  96.         Format:
  97.        
  98.         maze.x.y.(1/2/3/4) = true/false
  99.        
  100.         1 - top
  101.         2 - bottom
  102.         3 - right
  103.         4 - left
  104.     ]]--
  105.    
  106.     local maze = {}
  107.     for i = 1, mx do
  108.         maze[i] = {}
  109.         for j = 1, my do
  110.             maze[i][j] = {}
  111.             for k = 1, 4 do
  112.                 maze[i][j][k] = true
  113.             end
  114.         end
  115.     end
  116.  
  117.     local vis = 1
  118.     local tot = mx * my
  119.     local curr = {}
  120.     curr.x = math.random(1, mx)
  121.     curr.y = math.random(1, my)
  122.     local stack = {}
  123.  
  124.     while vis < tot do
  125.         local intact = {}
  126.         local x = curr.x
  127.         local y = curr.y
  128.        
  129.         if x - 1 >= 1 and maze[x-1][y][1] and maze[x-1][y][2] and maze[x-1][y][3] and maze[x-1][y][4] then -- Check for full cells
  130.             intact[#intact+1] = {x-1, y, 1}
  131.         end
  132.        
  133.         if x + 1 <= mx and maze[x+1][y][1] and maze[x+1][y][2] and maze[x+1][y][3] and maze[x+1][y][4] then
  134.             intact[#intact+1] = {x+1, y, 2}
  135.         end
  136.        
  137.         if y + 1 <= my and maze[x][y+1][1] and maze[x][y+1][2] and maze[x][y+1][3] and maze[x][y+1][4] then
  138.             intact[#intact+1] = {x, y+1, 3}
  139.         end
  140.        
  141.         if y - 1 >= 1 and maze[x][y-1][1] and maze[x][y-1][2] and maze[x][y-1][3] and maze[x][y-1][4] then
  142.             intact[#intact+1] = {x, y-1, 4}
  143.         end
  144.        
  145.         if #intact > 0 then
  146.             local i = math.random(1, #intact) -- Choose random
  147.            
  148.             if intact[i][3] == 1 then -- Set intact's attached wall to false
  149.                 maze[intact[i][1]][intact[i][2]][2] = false
  150.             elseif intact[i][3] == 2 then
  151.                 maze[intact[i][1]][intact[i][2]][1] = false
  152.             elseif intact[i][3] == 3 then
  153.                 maze[intact[i][1]][intact[i][2]][4] = false
  154.             elseif intact[i][3] == 4 then
  155.                 maze[intact[i][1]][intact[i][2]][3] = false
  156.             end
  157.            
  158.             maze[x][y][intact[i][3]] = false -- Set attached wall to false
  159.            
  160.             vis = vis + 1 -- Increase vis
  161.            
  162.             stack[#stack+1] = intact[i] -- Add to stack
  163.         else
  164.             local tmp = table.remove(stack) -- Get last cell
  165.             curr.x = tmp[1]
  166.             curr.y = tmp[2]
  167.         end
  168.     end
  169.    
  170.     return maze
  171. end
  172.  
  173. local m = mazeGen(size, size)
  174.  
  175. -- The game init
  176. local posx = 2
  177. local posy = 2
  178.  
  179. local offsetx = 51/2-2
  180. local offsety = 19/2-2
  181.  
  182. local stime = os.clock()
  183.  
  184. -- The maze-to-table
  185. local tab = {}
  186.  
  187. for x = 1, size * 2 + 1 do
  188.     tab[x] = {}
  189.    
  190.     for y = 1, size * 2 + 1 do
  191.         if x % 2 == 0 and y % 2 == 0 then -- Fill cells (empty)
  192.             tab[x][y] = false
  193.         elseif x % 2 == 1 and y % 2 == 1 then -- Fill corners (full)
  194.             tab[x][y] = true
  195.         end
  196.     end
  197. end
  198.  
  199. for x, tV in ipairs(m) do
  200.     for y, v in ipairs(tV) do
  201.         tab[x*2-1][y*2] = v[1] -- Up
  202.         tab[x*2+1][y*2] = v[2] -- Down
  203.         tab[x*2][y*2+1] = v[3] -- Right
  204.         tab[x*2][y*2-1] = v[4] -- Left
  205.     end
  206. end
  207.  
  208. -- The game itself
  209. repeat
  210.     -- Print map
  211.     term.setBackgroundColor(colors.white)
  212.     term.clear()
  213.    
  214.     if posx == 2 and posy == 2 then
  215.         term.setCursorPos(1, 1)
  216.         term.setTextColor(colors.black)
  217.         print("Controls: WASD")
  218.         print("Back to start: R")
  219.         print("Quit: Q")
  220.         print("Goal: Step on # (It's on bottom right corner)")
  221.         print("\nGood Luck!")
  222.     end
  223.    
  224.     --[[
  225.         term.setTextColor(colors.black)
  226.         term.setCursorPos(1, 19)
  227.         write("X: "..posx.."   Y: "..posy)
  228.     ]]
  229.    
  230.     for x, tV in ipairs(tab) do -- Print the map
  231.         for y, v in ipairs(tV) do
  232.             if offsety+y > 20 then
  233.                 break
  234.             end
  235.            
  236.             term.setCursorPos(offsetx+x, offsety+y)
  237.            
  238.             if v then
  239.                 term.setBackgroundColor(colors.black)
  240.             else
  241.                 term.setBackgroundColor(colors.white)
  242.             end
  243.            
  244.             if offsety+y < 20 and offsety+y > 0 and offsetx+x < 52 and offsetx+x > 0 then
  245.                 if x == size*2 and y == size*2 then
  246.                     if term.isColor() then
  247.                         term.setTextColor(colors.cyan)
  248.                     end
  249.                     write("#")
  250.                 else
  251.                     write(" ")
  252.                 end
  253.             end
  254.         end
  255.        
  256.         if offsetx+x > 51 then
  257.             break
  258.         end
  259.     end
  260.    
  261.     term.setCursorPos(51/2, 19/2)
  262.     term.setBackgroundColor(colors.white)
  263.    
  264.     if term.isColor() then
  265.         term.setTextColor(colors.red)
  266.     else
  267.         term.setTextColor(colors.black)
  268.     end
  269.    
  270.     write("X")
  271.    
  272.     -- Wait for key
  273.    
  274.     local e, k = os.pullEvent("char")
  275.    
  276.     if k == "a" and (not tab[posx-1][posy]) then
  277.         posx = posx - 1
  278.         offsetx = offsetx + 1
  279.     end
  280.    
  281.     if k == "d" and (not tab[posx+1][posy]) then
  282.         posx = posx + 1
  283.         offsetx = offsetx - 1
  284.     end
  285.    
  286.     if k == "w" and (not tab[posx][posy-1]) then
  287.         posy = posy - 1
  288.         offsety = offsety + 1
  289.     end
  290.    
  291.     if k == "s" and (not tab[posx][posy+1]) then
  292.         posy = posy + 1
  293.         offsety = offsety - 1
  294.     end
  295.    
  296.     if k == "q" then
  297.         break
  298.     end
  299.    
  300.     if k == "r" then
  301.         posx = 2
  302.         posy = 2
  303.  
  304.         offsetx = 51/2-2
  305.         offsety = 19/2-2
  306.     end
  307. until posx == size*2 and posy == size*2
  308.  
  309. -- The win/loose message
  310. term.setBackgroundColor(colors.white)
  311. term.setTextColor(colors.black)
  312. term.clear()
  313. term.setCursorPos(1, 1)
  314.  
  315. if posx == size*2 and posy == size*2 then
  316.     local ntime = os.clock()
  317.     write("\n")
  318.     cprint("Congratulations!")
  319.     cprint("You made it in")
  320.     cprint(tostring(math.floor((ntime-stime)/60)).." minutes and "..tostring(math.ceil((ntime-stime)%60)).." seconds")
  321.     cprint("Size of maze: "..size)
  322. else
  323.     write("\n")
  324.     cprint("Oh noes D:")
  325. end
  326.  
  327. parallel.waitForAny(
  328.     function() coroutine.yield(); os.pullEvent("key"); coroutine.yield() end,
  329.     function() term.setBackgroundColor(colors.white); term.setTextColor(colors.black) while true do term.setCursorPos(18, 14); term.write("Press any key.."); sleep(0.5); term.clearLine(); sleep(0.5) end end
  330. )
  331.  
  332. term.setBackgroundColor(colors.black)
  333. term.setTextColor(colors.white)
  334. term.clear()
  335. term.setCursorPos(1, 1)
  336. cprint("  Maze by Jesusthekiller. Thanks for playing!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement