Lua PANIC错误

时间:2014-08-21 01:37:09

标签: c++ lua

我正在用C ++创建一个Sudoku解算器,同时实现Lua脚本以实现拼图的实际解决。我创建了以下Lua代码,但得到了

  

PANIC:调用Lua API时出现无保护错误(尝试调用nil值)

每当我的C ++代码到达lua_call的第一个实例时出现

错误。 在SciTE中编译代码时,我收到以下错误:

  

lua:SudokuSolver.lua:99:' end'预期(关闭'在第61行)   靠近'

在第61行具有for循环的函数末尾添加三个结尾会清除该错误,但会导致C ++程序出错。有人可以看看我的Lua,看看是否有任何语法错误或其他可能导致此问题的问题?谢谢

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

1 个答案:

答案 0 :(得分:4)

语法错误出现在包含else if的所有行中:

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

在Lua中,请改用elseif。使用else if需要更多关闭end

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