Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

8 Queens solver "Error :attempt to compare with Nil value on line 26"? (this is not roblox)

Asked by 5 years ago
Edited 5 years ago
n = 8 -- board size

-- check wether position (a, n, c) is free from attacks
function isplaceok (a, n, c)
  for i = 1, n - 1 do
    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 printsolution (a)
  for i = 1, N do
    for j = 1, N do
      io.write(a[i] == j and "X" or "-", " ")
      end
    io.write("\n")
    end
  io.write("\n")
  end

function addqueen (a, n)
  if n > N then
    printsolution(a)
    else
    for c = 1, N do
      if isplaceok(a, n, c) then
        a[n] = c
        addqueen(a, n + 1)
        end
      end
    end
  end


addqueen({}, 1)
0
Where is capital N defined? At the top of the script, you only define lowercase n. mattscy 3725 — 5y
0
How do I define it? local? EliteMarata 34 — 5y
0
local N = 8 (keep in mind that capitalisation matters) mattscy 3725 — 5y

Answer this question