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) && !IsUsedInCol(Grid, col, number) && !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 not IsUsedInCol(Grid, col, number) and not IsUsedInSubSquare(Grid, row - row % 3, col - col % 3, number) end 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; 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.