Blair Purkiss
Blair Purkiss

Reputation: 13

Lua PANIC error

I am creating a Sudoku solver in C++ while implementing Lua scripting for the actual solving of the puzzle. I have created the following Lua code, but get a

PANIC: unprotected error in call to Lua API (attempt to call a nil value)

error whenever my C++ code reaches the first instance of lua_call. When compiling the code in SciTE, I get the following error:

lua: SudokuSolver.lua:99: 'end' expected (to close 'for' at line 61) near ''

Adding three 'end's to the end of the function that has the for loop at line 61 clears that error, but causes errors in the C++ program. Can someone please look at my Lua and see if there's any syntax errors or other issues which may be causing this? Thank you

CODE

-- Table Declaration
SudokuGrid = {}

function RecieveGrid ( _Pos, _Value )
    -- Recives the cell value at _Pos position from C++
    SudokuGrid[_Pos] = _Value
end

function SolveSudoku ( _Pos )
    -- Recursive function which solves the sudoku puzzle
    local iNewValue = 1

    -- If Position is 82+, all cells are solved
    if( _Pos >= 82 ) then
        return true
    end

    -- If Position already has a value
    if( SudokuGrid[_Pos] ~= 0) then
        return SolveSudoku( _Pos + 1 )
    else
        while(true) do
            SudokuGrid[_Pos] = iNewValue
            iNewValue = iNewValue + 1

            -- If the new value of the cell is higher than 9 its not valid
            if( SudokuGrid[_Pos] > 9 ) then
                --Reset value
                SudokuGrid[_Pos] = 0
                return false
            end

            if( IsValid( _Pos ) and SolveSudoku( _Pos + 1 ) ) then
                return true
            end
        end
    end
end

function IsValid ( _Pos )
    -- Calculate Column and Row in Grid
    x = _Pos % 9
    if( x == 0 ) then
        x = 9
    end
    y = math.ceil(_Pos / 9)

    -- Check Rows
    for i=1, 9 do
        CheckVal = ((y - 1)  * 9) + i
        if( CheckVal == _Pos ) then
            -- Do nothing
        else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal]and SudokuGrid[_Pos] ~= 0 ) then
            return false
        else
            -- Do nothing
        end
    end

    -- Check Columns
    for i=1, 9 do
        CheckVal = ((i - 1) * 9) + x
        if( CheckVal == _Pos ) then
            -- Do nothing
        else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 ) then
            return false
        else
            -- Do nothing
        end
    end

    -- Check 3X3 Grid
    SquareCol = math.ceil(x/3)
    SquareRow = math.ceil(y/3)
    StartVal = (SquareCol - 1) * 27 + (SquareRow * 3) -2
    for j=0, 2 do
        for i=0, 2 do
            CheckVal = StartVal + i
            if( CheckVal == _Pos ) then
                -- Do nothing
            else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 ) then
                return false
            else
                -- Do nothing
            end
        end
        StartVal = StartVal + 9
    end

    return true
end

function SendGrid ( _Pos )
    -- Sends the value at _Pos to C++
    return SudokuGrid[_Pos]
end

Upvotes: 1

Views: 730

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

The syntax error is in all lines containing else if:

else if ( SudokuGrid[_Pos] == SudokuGrid[CheckVal]and SudokuGrid[_Pos] ~= 0 ) then

In Lua, use elseif instead. Using else if would need more closing end.

elseif SudokuGrid[_Pos] == SudokuGrid[CheckVal] and SudokuGrid[_Pos] ~= 0 then

Upvotes: 4

Related Questions