thanks again, that code worked great. I have copied the whole program below in case anyone wants to see it in action. There is a bug in it that causes the large sudoku display to sometimes display 1 or 2 incorrect numbers (the correct solution is the green sudoku square on the right of the screen). I don’t want to put any more time into this project as I have other ones on the go that need attention, but if anyone finds the bug please let me know.
--LIST OF FUNCTIONS local generate local printGenerateButton local setUpGame local mainAction local printSudoku local printSolution local printSolution local solutionDisplay1 local newAutotable local FONT\_NAME = "Avenir-Light" --------------------- --SET UP GAME ---------------------- setUpGame = function() math.randomseed( os.time() ) myDisplayGroup = display.newGroup() printGenerateButton() --local currentSquareRecord = newAutotable(3) currentSquareRecord = {{}} sudoku = {{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8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},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}} end --------------------- --MAIN ACTION ---------------------- mainAction = function() local iBacktrackedFromGetRandom = false local iBacktrackedFromTop = false local currentSquareRecordIndex = 0 local vspace = 50 local hspace = 30 local stringy local currentSquareDisplay local subsquareMatch1 = false local subsquareMatch2 = false local columnMatch = false local rowMatch = false local currentSquare = 1 local numberChosen = 0 local function addNextSquareToRecord() currentSquareRecordIndex = currentSquareRecordIndex + 1 tempTable = {} table.insert(currentSquareRecord, tempTable) table.insert(currentSquareRecord[currentSquareRecordIndex], currentSquare) -- print(currentSquareRecord[currentSquareRecordIndex][1]) end -- function --------------------- -- WHILE --------------------- while (currentSquare \<= 81) do addNextSquareToRecord() --IS CURRENT SQUARE OUT OF NUMBERS? REPLENISH AND GO BACK A SQUARE if (#sudoku[currentSquare]==0) then --print("at top and cs is empty") sudoku[currentSquare] = {1,2,3,4,5,6,7,8,9} --currentSquare = currentSquare - 1 print("at the bit I removed cs is "..currentSquare) addNextSquareToRecord() --NO, THERE ARE STILL NUMBERS else --PICK RANDOM NUMBER FROM CURRENTSQUARE'S LIST ---------------- --GETRANDOM ---------------- local function getRandom() if (#sudoku[currentSquare]==0) then --print("in get random and cs is empty") sudoku[currentSquare] = {1,2,3,4,5,6,7,8,9} currentSquare = currentSquare - 1 addNextSquareToRecord() iBacktrackedFromGetRandom = true else local x = (math.random(1,#sudoku[currentSquare])) numberChosen = sudoku[currentSquare][x] --print("in getRandom() and numberchosen is "..numberChosen) --REMOVE NUMBERCHOSEN FROM THE CURRENTSQUARE'S LIST for y = 1,#sudoku[currentSquare] do if sudoku[currentSquare][y] == numberChosen then table.remove(sudoku[currentSquare],y) table.insert(currentSquareRecord[currentSquareRecordIndex], numberChosen) table.insert(solutionForDisplay[currentSquare],numberChosen) end -- if end -- for y end -- if end -- function getRandom getRandom() --VERiFY NUMBER IS OK -------------------------------- --FUNCTION: LOOK BACK DOWN THE CURRENT ROW -------------------------------- local function lookBackDownRow() rowMatch = false local j = currentSquare - 1 local limit = 0 if (currentSquare \<= 9) then limit = 0 elseif (currentSquare \<= 18) then limit = 9 elseif (currentSquare \<= 27) then limit = 18 elseif (currentSquare \<= 36) then limit = 27 elseif (currentSquare \<= 45) then limit = 36 elseif (currentSquare \<= 54) then limit = 45 elseif (currentSquare \<= 63) then limit = 54 elseif (currentSquare \<= 72) then limit = 63 elseif (currentSquare \<= 81) then limit = 72 end while (j \> limit) do if (numberChosen == solution[j]) then rowMatch = true break end j = j - 1 end -- while j end -- function lookBackDownRow -------------------------------- --FUNCTION: LOOK UP CURRENT COL -------------------------------- local function lookUpColumn() columnMatch = false local y = currentSquare - 9 --if (currentSquare \> 9) then while (y \> 0) do if (numberChosen == solution[y]) then --print("in looking up and found match") columnMatch = true break end -- if y = y - 9 end -- while --end -- if end -- function lookUpColumn -------------------------------- --FUNCTION: checkSubsquare1 -------------------------------- local function checkSubsquare1() subsquareMatch1 = false if (currentSquare % 9 == 1 or currentSquare % 9 == 4 or currentSquare % 9 == 7) then if ( (numberChosen == solution[currentSquare - 7]) or (numberChosen == solution[currentSquare - 8]) ) then subsquareMatch1 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 10]) or (numberChosen == solution[currentSquare - 8]) ) then subsquareMatch1 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or (numberChosen == solution[currentSquare - 11]) ) then subsquareMatch1 = true end end end -- function checkSubsquare1 -------------------------------- --FUNCTION: checkSubsquare2 -------------------------------- local function checkSubsquare2() subsquareMatch2 = false if (currentSquare % 9 == 1 or currentSquare % 9 == 4 or currentSquare % 9 == 7) then if ( (numberChosen == solution[currentSquare - 7]) or (numberChosen == solution[currentSquare - 8]) or (numberChosen == solution[currentSquare - 16]) or (numberChosen == solution[currentSquare - 17]) ) then subsquareMatch2 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 8]) or (numberChosen == solution[currentSquare - 10]) or (numberChosen == solution[currentSquare - 19]) or (numberChosen == solution[currentSquare - 17]) ) then subsquareMatch2 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or (numberChosen == solution[currentSquare - 11]) or (numberChosen == solution[currentSquare - 19]) or (numberChosen == solution[currentSquare - 20]) ) then subsquareMatch2 = true end end end -- function checkSubsquare1 --------------------------------- -- CALL THE VERIFICATION FUNCTIONS --------------------------------- if ((currentSquare \>= 10 and currentSquare \<=18) or (currentSquare \>= 37 and currentSquare \<=45) or (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or (currentSquare \>= 46 and currentSquare \<=54) or (currentSquare \>= 73 and currentSquare \<=81) ) then checkSubsquare2() end if (currentSquare \> 9) then lookUpColumn() end if (currentSquare % 9 ~= 1 ) then lookBackDownRow() end while (columnMatch == true or rowMatch == true or subsquareMatch1 == true or subsquareMatch2 == true) do getRandom() --table.insert(solutionForDisplay[currentSquare],numberChosen) if (currentSquare \> 9) then lookUpColumn() end if (currentSquare % 9 ~= 1 ) then lookBackDownRow() end if ((currentSquare \>= 10 and currentSquare \<=18) or (currentSquare \>= 37 and currentSquare \<=45) or (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or (currentSquare \>= 46 and currentSquare \<=54) or (currentSquare \>= 73 and currentSquare \<=81) ) then checkSubsquare2() end end --ASSIGN VERIFIED NUMBER TO THE SOLUTION ARRAY solution[currentSquare] = numberChosen columnMatch = false rowMatch = false currentSquare = currentSquare + 1 --currentSquareRecordIndex = currentSquareRecordIndex + 1 end -- else end -- while end--end of mainAction --------------------- --printSolution - prints entire generation history from currentSquareRecord ---------------------- printSolution = function() local boxes = {} local numbers = {} local counter = 0 for row = 1, 9 do for col = 1, 9 do local x, y = 20 \* col + 5\*col, 20 \* row + 5\*row --if row%3==1 then row = row + 10 end counter = counter + 1 boxes[counter] = display.newRect( x, y, 20, 20 ) boxes[counter]:setFillColor(1, (col%3)\*.1, row\*.1) numbers[counter] = display.newText("", x, y, FONT\_NAME, 12) end end local sudocoro local nextNumber = function() coroutine.resume(sudocoro) end sudocoro = coroutine.create(function() for i = 1, #currentSquareRecord do for j = 2, #currentSquareRecord[i] do local numberIndex, value = currentSquareRecord[i][1], currentSquareRecord[i][j] numbers[numberIndex].text = value numbers[numberIndex].alpha = 0 transition.to(numbers[numberIndex], {alpha = 1, onComplete = nextNumber, time = 100}) coroutine.yield() end end end) nextNumber() for a=1,#currentSquareRecord do for b=1,#currentSquareRecord[a] do print(currentSquareRecord[a][b]) end end end--printSolution --------------------- --printSolution2 prints the correct solution from the solution array ---------------------- printSolution2 = function() local b = 1 local j = 1 local stringy = "" local hspacer = 300 local vspacer = 50 for b=1,81 do local stringy = tostring(solution[b]) solutionDisplay1 = display.newText(myDisplayGroup,stringy,hspacer,vspacer,FONT\_NAME,15) solutionDisplay1:setFillColor(0, 1, 1) hspacer = hspacer + 15 if (b % 3 == 0 and b % 9 ~= 0) then hspacer = hspacer + 5 end if (b == 27 or b == 54) then vspacer = vspacer + 5 end if (b % 9 == 0) then vspacer = vspacer + 15 hspacer = 300 end end -- for -- -- end--printSolution2 ---------------------- -- GENERATE ---------------------- generate = function(event) if ( event.phase == "began" ) then display.getCurrentStage():setFocus(event.target) event.target:setFillColor(.87,.3,.1,.3) elseif ( event.phase == "ended" ) then display.getCurrentStage():setFocus(nil) generateButton:removeSelf() generateButton = nil if(myDisplayGroup ~= NULL) then myDisplayGroup:removeSelf() end setUpGame() mainAction() printSolution() printSolution2() end end ---------------------- -- printGenerateButton ---------------------- printGenerateButton = function() generateButton = display.newText("generate",400,200,native.systemFont,30) generateButton:setFillColor(1,1,1) generateButton:addEventListener("touch", generate) end ---------------------- --END FUNCTIONS ---------------------- --local splashBackground = display.newImageRect( "pics/score\_background.png",display.contentWidth,display.contentHeight) --splashBackground.anchorX=0 -- splashBackground.anchorY=0 printGenerateButton()