Lua 5.2 Source Code
-- eight-queens
-- Author: Hannes du Plooy
-- Date: 20 Sep 2016
-- Objective: To solve the following
-- Search for a chess board solution with 8 queens and no one
-- endangering any other.
board = {0,0,0,0,0,0,0,0}
function EightQueens(line)
if line>7 then return true end
for column = 0,7 do
endangered = false
for line2 = 0,line-1 do
if board[line2]==column or math.abs(line-line2)==math.abs(column-board[line2]) then
endangered = true
break
end
end
if not endangered then
board[line] = column
if EightQueens(line+1) then
return true
end
end
end
return false
end
if EightQueens(0) then
for i=0,7 do
tmp = ""
for j=0,7 do
if board[i] == j then
tmp = tmp .. "Q"
else
tmp = tmp .. "."
end
end
print(tmp)
end
end