c++ - Lua PANIC 错误

标签 c++ lua

我正在用 C++ 创建一个数独解算器,同时为实际解题实现 Lua 脚本。我创建了以下 Lua 代码,但得到了一个

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

每当我的 C++ 代码到达 lua_call 的第一个实例时出错。 在 SciTE 中编译代码时,出现以下错误:

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

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

代码

-- 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

最佳答案

语法错误出现在所有包含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

关于c++ - Lua PANIC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25417028/

相关文章:

c++ - Windows编程教程中未解析的外部符号__RTC_*

c++ - 是否有可能在未包含在任何其他文件中的文件中使用 extern 变量?

macos - osx - 获取当前桌面号的脚本

c++ - 随机数生成器 - 为什么每次都播种

c++ - 写跳转到内存

c++ - 在 C/C++ 中遍历 Fasta 文件

lua - 常规 Lua 解释器与 LuaJIT 解释器?

lua - 如何从apk中提取Lua文件(corona生成的apk)

build - 如何在一个 .exe 文件中构建和链接 Lua 核心和几个 Lua 模块

algorithm - 最长公共(public)子串错误