lua-prog-book/eight-queen.lua

26 lines
557 B
Lua

N = 8 -- size of the board
-- check whether position (n, c) is free from attacks
function is_place_ok (a, n, c)
for i = 1, n - 1 do -- leaving the inc/dec value empty defaults to +1
if (a[i] == c) or
(a[i] - i == c - n) or
(a[i] + i == c + n) then
return false
end
end
return true
end
function print_solution (a)
for i = 1, N, 1 do
for j = 1, N, 1 do
io.write(a[i] == j and "X" or "-", " ")
end
io.write("\n")
end
io.write("\n")
end