Attempting to translate small C++ code into Lua

Hey guys, I’m new here. I am also very new to Lua scripting language, I’ve only been using it for a week now and so far I’m really enjoying it. However, I bumped into a problem I’ve been having all day today, and it’s basically figuring out how to send something by reference using Lua. I am planning on using Lua as a way to write AI, below is a C++ code that solve a Sudoku puzzle (I figured it’d be a good exercise for me to try and translate the code below to Lua).

The C++ Code:

bool IsEmpty(int Grid[SIZE][SIZE], int& row, int& col) { for (row = 0; row \< SIZE; ++row) { for (col = 0; col \< SIZE; ++col) { if (Grid[row][col] == EMPTY) return true; } } return false; } bool IsUsedInRow(int Grid[SIZE][SIZE], int row, int number) { for (int i = 0; i \< SIZE; ++i) { if (Grid[row][i] == number) return true; } return false; } bool IsUsedInCol(int Grid[SIZE][SIZE], int col, int number) { for (int i = 0; i \< SIZE; ++i) { if (Grid[i][col] == number) return true; } return false; } bool IsUsedInSubSquare(int Grid[SIZE][SIZE], int StartRow, int StartCol, int number) { for (int row = 0; row \< 3; row++) for (int col = 0; col \< 3; col++) if (Grid[row + StartRow][col + StartCol] == number) return true; return false; } bool IsLegal(int Grid[SIZE][SIZE], int row, int col, int number) { return !IsUsedInRow(Grid, row, number) && &nbsp; !IsUsedInCol(Grid, col, number) && &nbsp; !IsUsedInSubSquare(Grid, row - row % 3, col - col % 3, number); } bool SolveSudoku(int Grid[SIZE][SIZE]) { int row, col; // Check to see if there is no unassigned location, if so then we are done if (!IsEmpty(Grid, row, col)) return true; for (int i = 0; i \< 10; ++i) { // Check to see if the move is legal if (IsLegal(Grid, row, col, i)) { // Make tentative assignment Grid[row][col] = i; if (SolveSudoku(Grid)) return true; Grid[row][col] = EMPTY; } } return false; }

Here is my attempt at solving it in Lua:

function IsEmpty(Grid, row, col) for row = 1, 9 do for col = 1, 9 do if Grid[row][col] == 0 then return true; end end end return false; end function IsUsedInRow(Grid, row, number) for i = 1, 9 do if (Grid[row][i] == number) then return true end end return false end function IsUsedInCol(Grid, col, number) for i = 1, 9 do if (Grid[i][col] == number) then return true end end return false end function IsUsedInSubSquare(Grid, StartRow, StartCol, number) for row = 1, 3 do for col = 1, 3 do if (Grid[row + StartRow][col + StartCol] == number) then return true end end end return false; end function IsLegal(Grid, row, col, number) return not IsUsedInRow(Grid, row, number) and &nbsp; not IsUsedInCol(Grid, col, number) and &nbsp; not IsUsedInSubSquare(Grid, row - row % 3, col - col % 3, number) end&nbsp; function SolveSudoku(Grid) local row, col; -- How can I send those two by reference? -- Check to see if there is no unassigned location, if so then we are done if (not(IsEmpty(Grid, row, col))) then return true; end for i = 1, 10 do if (IsLegal(Grid, row, col,i)) then Grid[row][col] = i; if (SolveSudoku(Grid)) then return true; end Grid[row][col] = 0; end end return false; &nbsp; end

I hope when I submit this everything would be nicely indented, but I did leave a comment next to the problem. I’d really appreciate it if someone here could have a look at my Lua script and tell me how I can get this script to work because I honestly have no idea what to do at this point… :/ 

Thank you for reading my thread. :slight_smile:

The problem you will face is using functions to change values and expecting them to be changed after the function finishes. for example, you are expecting this to print 2 but it will print 1.

local a = 1 local function b(a) &nbsp; a = a + 1 end b(a) print (a)

you need to rethink and do something like this

local a = 1 local function b(a) &nbsp; a = a + 1 &nbsp; return a end a = b(a) print (a)

so you will need to re code the function(s) to return actual values and not yes/no

another BIG issue you’ll face converting C/C++ is array indices starting at 0 (versus Lua starting at 1)

looks like you’ve caught many of them, but not all…

fe:  i see a “for i = 1 to 10” which seems wrong for sudoku in ANY language

double- triple- quadruple-check all your math regarding row/col indexing

and do a “quintuple-check” if a modulo is involved

(modulo often works “perfect” in C, giving 0…N-1, but result is often “one too small” for Lua if used as array index)

and any indexing like “row + offset” where the row is likely on [1…9] but offset should maybe be on [0…8]

etc

The problem you will face is using functions to change values and expecting them to be changed after the function finishes. for example, you are expecting this to print 2 but it will print 1.

local a = 1 local function b(a) &nbsp; a = a + 1 end b(a) print (a)

you need to rethink and do something like this

local a = 1 local function b(a) &nbsp; a = a + 1 &nbsp; return a end a = b(a) print (a)

so you will need to re code the function(s) to return actual values and not yes/no

another BIG issue you’ll face converting C/C++ is array indices starting at 0 (versus Lua starting at 1)

looks like you’ve caught many of them, but not all…

fe:  i see a “for i = 1 to 10” which seems wrong for sudoku in ANY language

double- triple- quadruple-check all your math regarding row/col indexing

and do a “quintuple-check” if a modulo is involved

(modulo often works “perfect” in C, giving 0…N-1, but result is often “one too small” for Lua if used as array index)

and any indexing like “row + offset” where the row is likely on [1…9] but offset should maybe be on [0…8]

etc