Commit 8ed99536 by Phil Hagelberg

Add random mazes and solver.

1 parent 3a741022
......@@ -17,12 +17,13 @@ love.load = function(args)
love.graphics.setFont(love.graphics.newFont("polywell/inconsolata.ttf", 14))
love.keyboard.setTextInput(true)
love.keyboard.setKeyRepeat(true)
require("maze")
love.math.setRandomSeed(os.time())
local maze = require("maze")
require("polywell.config.edit")
require("polywell.config.lua_mode")
require("polywell.config.emacs_keys")
local dir = args[2] or "main"
polywell.fs = fs_for(dir)
polywell.fs = fs_for(".")
polywell.open("*maze*", nil, "maze")
maze.random_maze()
end
local editor = require("polywell")
local random = require("random")
local solver = require("solver")
local lume = require("lib.lume")
local maze = dofile("mazes/1.lua")
local size = 32
local size, path = 32, nil
local player = {x=maze.start[1], y=maze.start[2]}
w, h = 24, 16
local draw_cell = function(x, y, exits)
love.graphics.setColor(255,255,255)
......@@ -21,6 +24,19 @@ local draw_cell = function(x, y, exits)
end
end
local draw_path = function(path)
love.graphics.push()
love.graphics.translate(size/2, size/2)
love.graphics.scale(size)
love.graphics.setLineWidth(0.3)
for i,xy in ipairs(path) do
if(i > 1) then
love.graphics.line(path[i-1][1], path[i-1][2], unpack(xy))
end
end
love.graphics.pop()
end
local draw = function()
love.graphics.setLineWidth(4)
for y,row in ipairs(maze) do
......@@ -34,12 +50,13 @@ local draw = function()
love.graphics.setColor(25,255,25)
love.graphics.circle("fill", player.x*size+size/2,
player.y*size+size/2, size/2)
if(path) then draw_path(path) end
end
editor.define_mode("maze", "default", {draw=draw})
local allowed = function(dir)
return string.find(maze[player.y][player.x], dir)
return string.find(maze[player.y][player.x], dir)
end
editor.bind("maze", "up", function()
......@@ -51,12 +68,29 @@ editor.bind("maze", "left", function()
editor.bind("maze", "right", function()
if(allowed("r")) then player.x = player.x + 1 end end)
editor.bind("maze", "s", function() require("solver")(maze, player) end)
editor.bind("maze", "escape", love.event.quit)
editor.bind("maze", "ctrl-o", editor.find_file)
editor.bind("maze", "p", function() path = solver(maze, player) end)
require("polywell.config.console")
local close = function() editor.change_buffer(editor.last_buffer()) end
editor.bind("maze", "ctrl-return", lume.fn(editor.change_buffer, "*console*"))
editor.bind("console", "escape", close)
editor.bind("console", "ctrl-return", close)
local random_maze = function()
local new_maze = random.new(w, h)
player.x, player.y = new_maze.start[1], new_maze.start[2]
while(not solver(new_maze, player)) do
new_maze = random.new(w, h)
end
maze, path = new_maze, nil
player.x, player.y = maze.start[1], maze.start[2]
end
editor.bind("maze", "m", random_maze)
editor.bind("maze", "escape", love.event.quit)
return {random_maze = random_maze}
local random_exits = function(exits)
local s = ""
for i=1,math.random(2,3) do
local e = exits[math.random(5)]
s = s .. e
end
return s
end
local fix_one_way = function(m)
for y,row in ipairs(m) do
for x,exits in ipairs(row) do
local r = m[y][x+1]
if(r and string.find(r, "l")) then
m[y][x] = m[y][x] .. "r"
end
local l = m[y][x-1]
if(l and string.find(l, "r")) then
m[y][x] = m[y][x] .. "l"
end
local u = m[y-1] and m[y-1][x]
if(u and string.find(u, "d")) then
m[y][x] = m[y][x] .. "u"
end
local d = m[y+1] and m[y+1][x]
if(d and string.find(d, "u")) then
m[y][x] = m[y][x] .. "d"
end
end
end
end
local random_maze = function(w, h)
local m = {start={1,1}, finish={w,h}}
for y=1,h do
local r = {}
local exits = {"u", "d", "l", "r", ""}
for x=1,w do
if(y == 1) then exits[1] = "" end
if(y == h) then exits[2] = "" end
if(x == 1) then exits[3] = "" end
if(x == w) then exits[4] = "" end
table.insert(r, random_exits(exits))
end
table.insert(m, r)
end
fix_one_way(m)
return m
end
return {new = random_maze}
......@@ -30,6 +30,4 @@ local function solve(m, player, to_visit, path)
end
end
return function(m, player)
print(inspect(solve(m, player)))
end
return solve
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!