Skip to content

Instantly share code, notes, and snippets.

@gboncoffee
Created November 18, 2023 12:38
Show Gist options
  • Save gboncoffee/5e5243c5f701dee6f1516e3b5e575abe to your computer and use it in GitHub Desktop.
Save gboncoffee/5e5243c5f701dee6f1516e3b5e575abe to your computer and use it in GitHub Desktop.
Knight Tour problem in Lua
#!/usr/bin/env lua
--
-- Copyright (c) 2023 Gabriel G. de Brito
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
--
create_line = function(n)
local l = {}
for i = 1, n do
table.insert(l, 0)
end
return l
end
create_grid = function(n)
local g = {}
for i = 1, n do
table.insert(g, create_line(n))
end
return g
end
print_grid = function(grid, n)
for i = 1, n do
for j = 1, n do
if grid[i][j] == 0 then
io.write("| ")
else
io.write("|" .. string.format("%03d", grid[i][j]))
end
end
io.write("|\n")
end
end
clrscr = function()
io.write "\27[2J"
io.write "\27[H"
end
hidecursor = function()
io.write "\27[?25l"
end
showcursor = function()
io.write "\27[?25h"
end
getmov = function(x, y, n)
local mov = {}
local st = {
{ x = -2, y = 1 },
{ x = -2, y = -1 },
{ x = -1, y = 2 },
{ x = -1, y = -2 },
{ x = 1, y = 2 },
{ x = 2, y = 1 },
{ x = 1, y = -2 },
{ x = 2, y = -1 },
}
for i = 1, 8 do
local s = { x = x + st[i].x, y = y + st[i].y }
if s.x > 0 and s.x <= n and s.y > 0 and s.y <= n then
table.insert(mov, s)
end
end
return mov
end
knight_tour = function(grid, n, x, y, h)
grid[x][y] = h
clrscr()
-- print_grid(grid, n)
-- os.execute("sleep 0.0003")
if h == n * n then
return true
end
local pos = getmov(x, y, n)
for i, m in ipairs(pos) do
if grid[m.x][m.y] == 0 then
if knight_tour(grid, n, m.x, m.y, h + 1) then
return true
end
end
end
grid[x][y] = 0
return false
end
clrscr()
hidecursor()
size = 5
grid = create_grid(size)
if knight_tour(grid, size, size, size, 1) then
print_grid(grid, size)
print "solution found!!"
else
print_grid(grid, size)
print "no solution found"
end
showcursor()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment