printing contents of a table with transitions

Hi

I’ve got numbers in a table like this:

{{1,2,3},{4,5,6}}

I’m trying to display these numbers on the screen one after the other, probably for around a quarter second each. I’ve made various attempts to print a table of numbers, including this recursive one below, but I’m at a loss. Any ideas?

thanks.

--DOES NOT WORK :-( local solutionDisplay = "" myNumTable = {1,2,3,4,5,6} local function timedDisplay(i) while (i \<= 6) do local numToString = tostring(myNumTable[i]) solutionDisplay &nbsp;= &nbsp;display.newText(numToString,50\*i,100,FONT\_NAME,10) solutionDisplay:setFillColor(0,0,0) i = i + 1 transition.to( solutionDisplay, { time=1500, delay=1000, alpha=0, &nbsp;onComplete=timedDisplay(i)} ) end end timedDisplay(1)

When you call timedDisplay() the first time, it immediately creates six objects and adds transitions to them. After 1500 milliseconds, each of these call timedDisplay() again, so that will create 36 objects, each with transitions. And on it goes.

There’s a million ways you could go about this, but here’s one:

local solutions = {} local myNumTable = {1,2,44,4,5,6} local currentIndex = 1 local function timedDisplay() solutions[currentIndex] = display.newText(myNumTable[currentIndex],50\*currentIndex,20,"nyala",10) solutions[currentIndex]:setFillColor(0,0,0) transition.to( solutions[currentIndex], { time=1500, delay = 500, alpha=0} ) currentIndex = currentIndex + 1 end timer.performWithDelay(1000, timedDisplay, #myNumTable)

Notice that all the newText images are kept in a (poorly named) table called solutions. This is important, as it allows you to remove them later if necessary.

hi - thanks for that. Your code does exactly what I want to achieve, but when I apply it to my collection of objects, they all appear simultaneously instead of one after the other, and then they pulsate once a second. 

Here’s a snippet of my code. The collection of numbers I’m printing is a table of tables so I have 2 indices. For each value of c there is also a backtrack that may remove display objects from the screen.

printSolution = function() ... for c = 1,#currentSquareRecord-1 do ... tableToHoldObjects[c][d-1] = &nbsp;display.newText(myDisplayGroup,stringy,hspace,vspace,FONT\_NAME,10) transition.to(tableToHoldObjects[c][d-1], { time=1500, delay = 500, alpha=0} ) tableToHoldObjects[c][d-1]:setFillColor(0, 0, 0)

and then 

timer.performWithDelay(1000, printSolution, #currentSquareRecord-1)

ok I should have the timer.perform … line inside the function. Getting closer…

Can’t you just put all the numbers into the one table and then do it?

local solutions = {} local myNumTable = {{1,2},{44,4,5,6}} local listOfNums = {} for i = 1, #myNumTable do for j = 1, #myNumTable[i] do listOfNums[#listOfNums + 1] = myNumTable[i][j] end end local currentIndex = 1 local function timedDisplay() solutions[currentIndex] = display.newText(listOfNums[currentIndex],50\*currentIndex,20,"nyala",10) solutions[currentIndex]:setFillColor(0,0,0) transition.to( solutions[currentIndex], { time=1500, delay = 500, alpha=0} ) currentIndex = currentIndex + 1 end timer.performWithDelay(1000, timedDisplay, #listOfNums)

hi

I’m trying to print all the numbers that were ever generated in the making of a sudoku., My data looks something like this:

{{1,1},{2,3},{3,1,4},.....{60,3,5},......{60,4,9,8}...{81,9}}

the first number of each subtable represents the sudoku square. The number(s) after it are the numbers that were generated to fit in that square. You can see how in the 3rd subtable the computer generated a 1 but that was rejected because there’s already a 1 in the first square, so it generated another number, a 4, which fits, and so it continued on. Because the algorithm involves backtracking, squares can be revisited, as in the example of square 60 above. My goal is to show these numbers in place in the 9X9 grid. So at the start we’d see a 1 in space 1, a 3 in space 2, another 1 in space 3, then that 1 would remove itself and a 4 would appear in its place, and so on. In the case of 60, the first time it would display 3, then  5. When it revisits 60, the 5 removes itself and then we see 4, 9, then finally 8.

That is tricky. It’s simple enough to access the numbers you want with for-loops, and set off the transitions, but then the transitions will all fire off at the same time. If only there was a way to break out of the for-loops temporarily, and then pick up later where you left off…

ah, coroutines!

local solutions = {{1, 2}, {2, 2, 3}, {3, 8}, {4, 5}, {5, 8, 4}, {6, 7}, {7, 1}, {6, 4, 3}, {8, 4}, {9, 6, 4}, {2, 1, 3}} local boxes = {} local numbers = {} local counter = 0 for row = 1, 3 do for col = 1, 3 do local x, y = 50 \* col + 5\*col, 50 \* row + 5\*row counter = counter + 1 boxes[counter] = display.newRect( x, y, 50, 50 ) boxes[counter]:setFillColor(1, 0, 0) numbers[counter] = display.newText("", x, y, "courier", 16) end end local sudocoro local nextNumber = function() coroutine.resume(sudocoro) end sudocoro = coroutine.create(function() for i = 1, #solutions do for j = 2, #solutions[i] do local numberIndex, value = solutions[i][1], solutions[i][j] numbers[numberIndex].text = value numbers[numberIndex].alpha = 0 transition.to(numbers[numberIndex], {alpha = 1, onComplete = nextNumber, time = 1000}) coroutine.yield() end end end) nextNumber()

cool, thanks. I will try this out when i get a chance and post back.

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() ) &nbsp;myDisplayGroup = display.newGroup() &nbsp;printGenerateButton() --local currentSquareRecord = newAutotable(3) currentSquareRecord = {{}} &nbsp;sudoku =&nbsp; {{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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,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9}} --------------------------------- &nbsp;solution = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} &nbsp;solutionForDisplay =&nbsp; &nbsp;{{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}} 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 = &nbsp;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&nbsp; --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 &nbsp;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&nbsp; -------------------------------- --FUNCTION: LOOK BACK DOWN THE CURRENT ROW -------------------------------- local function lookBackDownRow() rowMatch = false local j = currentSquare - 1 local limit = 0 if (currentSquare &nbsp;\<= 9) then limit = 0 elseif (currentSquare &nbsp;\<= 18) then limit = 9 elseif (currentSquare &nbsp;\<= 27) then limit = 18 elseif (currentSquare &nbsp;\<= 36) then limit = 27 elseif (currentSquare &nbsp;\<= 45) then limit = 36 elseif (currentSquare &nbsp;\<= 54) then limit = 45 elseif (currentSquare &nbsp;\<= 63) then limit = 54 elseif (currentSquare &nbsp;\<= 72) then limit = 63 elseif (currentSquare &nbsp;\<= 81) then limit = 72 end while (j &nbsp;\> 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 &nbsp;\> 0) do if (numberChosen == solution[y]) then --print("in looking up and found match") columnMatch = true&nbsp; break end -- if y = y - 9 end -- while&nbsp; --end -- if&nbsp; 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&nbsp; (numberChosen == solution[currentSquare - 8]) &nbsp;) then subsquareMatch1 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or&nbsp; currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 8]) &nbsp;) then subsquareMatch1 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 11]) &nbsp;) 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&nbsp; (numberChosen == solution[currentSquare - 8]) or&nbsp; (numberChosen == solution[currentSquare - 16]) or&nbsp; (numberChosen == solution[currentSquare - 17]) &nbsp;) then subsquareMatch2 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 8]) or&nbsp; (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 19]) or&nbsp; (numberChosen == solution[currentSquare - 17]) &nbsp;) then subsquareMatch2 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 11]) or&nbsp; (numberChosen == solution[currentSquare - 19]) or&nbsp; (numberChosen == solution[currentSquare - 20]) &nbsp;) then subsquareMatch2 = true end end end -- function checkSubsquare1 --------------------------------- -- CALL THE VERIFICATION FUNCTIONS --------------------------------- if ((currentSquare \>= 10 and currentSquare \<=18) or&nbsp; (currentSquare \>= 37 and currentSquare \<=45) &nbsp;or&nbsp; (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or&nbsp; (currentSquare \>= 46 and currentSquare \<=54) &nbsp;or&nbsp; (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&nbsp; rowMatch == true or&nbsp; subsquareMatch1 == true or&nbsp; 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&nbsp; (currentSquare \>= 37 and currentSquare \<=45) &nbsp;or&nbsp; (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or&nbsp; (currentSquare \>= 46 and currentSquare \<=54) &nbsp;or&nbsp; (currentSquare \>= 73 and currentSquare \<=81) ) then checkSubsquare2() end end --ASSIGN VERIFIED NUMBER TO THE SOLUTION ARRAY solution[currentSquare] = &nbsp;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 &nbsp; &nbsp; for col = 1, 9 do &nbsp; &nbsp; &nbsp; &nbsp; local x, y = 20 \* col + 5\*col, 20 \* row + 5\*row &nbsp; &nbsp; &nbsp; &nbsp; --if row%3==1 then row = row + 10 end &nbsp; &nbsp; &nbsp; &nbsp; counter = counter + 1 &nbsp; &nbsp; &nbsp; &nbsp; boxes[counter] = display.newRect( x, y, 20, 20 ) &nbsp; &nbsp; &nbsp; &nbsp; boxes[counter]:setFillColor(1, (col%3)\*.1, row\*.1) &nbsp; &nbsp; &nbsp; &nbsp; numbers[counter] = display.newText("", x, y, FONT\_NAME, 12) &nbsp; &nbsp; end end local sudocoro local nextNumber = function() &nbsp; &nbsp; coroutine.resume(sudocoro) end sudocoro = coroutine.create(function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i = 1, #currentSquareRecord do &nbsp; &nbsp; &nbsp; &nbsp; for j = 2, #currentSquareRecord[i] do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local numberIndex, value = currentSquareRecord[i][1], currentSquareRecord[i][j] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[numberIndex].text = value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[numberIndex].alpha = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(numbers[numberIndex], {alpha = 1, onComplete = nextNumber, time = 100}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coroutine.yield() &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; 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&nbsp; local j = 1 local stringy = "" local hspacer = 300 local vspacer = 50 for b=1,81 do local stringy = tostring(solution[b]) solutionDisplay1 = &nbsp;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) &nbsp; &nbsp;if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(event.target) &nbsp; &nbsp; event.target:setFillColor(.87,.3,.1,.3) &nbsp; &nbsp;elseif &nbsp;( event.phase == "ended" ) then &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(nil) &nbsp; &nbsp; generateButton:removeSelf() &nbsp; &nbsp; generateButton = nil if(myDisplayGroup ~= NULL) then &nbsp; 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()

When you call timedDisplay() the first time, it immediately creates six objects and adds transitions to them. After 1500 milliseconds, each of these call timedDisplay() again, so that will create 36 objects, each with transitions. And on it goes.

There’s a million ways you could go about this, but here’s one:

local solutions = {} local myNumTable = {1,2,44,4,5,6} local currentIndex = 1 local function timedDisplay() solutions[currentIndex] = display.newText(myNumTable[currentIndex],50\*currentIndex,20,"nyala",10) solutions[currentIndex]:setFillColor(0,0,0) transition.to( solutions[currentIndex], { time=1500, delay = 500, alpha=0} ) currentIndex = currentIndex + 1 end timer.performWithDelay(1000, timedDisplay, #myNumTable)

Notice that all the newText images are kept in a (poorly named) table called solutions. This is important, as it allows you to remove them later if necessary.

hi - thanks for that. Your code does exactly what I want to achieve, but when I apply it to my collection of objects, they all appear simultaneously instead of one after the other, and then they pulsate once a second. 

Here’s a snippet of my code. The collection of numbers I’m printing is a table of tables so I have 2 indices. For each value of c there is also a backtrack that may remove display objects from the screen.

printSolution = function() ... for c = 1,#currentSquareRecord-1 do ... tableToHoldObjects[c][d-1] = &nbsp;display.newText(myDisplayGroup,stringy,hspace,vspace,FONT\_NAME,10) transition.to(tableToHoldObjects[c][d-1], { time=1500, delay = 500, alpha=0} ) tableToHoldObjects[c][d-1]:setFillColor(0, 0, 0)

and then 

timer.performWithDelay(1000, printSolution, #currentSquareRecord-1)

ok I should have the timer.perform … line inside the function. Getting closer…

Can’t you just put all the numbers into the one table and then do it?

local solutions = {} local myNumTable = {{1,2},{44,4,5,6}} local listOfNums = {} for i = 1, #myNumTable do for j = 1, #myNumTable[i] do listOfNums[#listOfNums + 1] = myNumTable[i][j] end end local currentIndex = 1 local function timedDisplay() solutions[currentIndex] = display.newText(listOfNums[currentIndex],50\*currentIndex,20,"nyala",10) solutions[currentIndex]:setFillColor(0,0,0) transition.to( solutions[currentIndex], { time=1500, delay = 500, alpha=0} ) currentIndex = currentIndex + 1 end timer.performWithDelay(1000, timedDisplay, #listOfNums)

hi

I’m trying to print all the numbers that were ever generated in the making of a sudoku., My data looks something like this:

{{1,1},{2,3},{3,1,4},.....{60,3,5},......{60,4,9,8}...{81,9}}

the first number of each subtable represents the sudoku square. The number(s) after it are the numbers that were generated to fit in that square. You can see how in the 3rd subtable the computer generated a 1 but that was rejected because there’s already a 1 in the first square, so it generated another number, a 4, which fits, and so it continued on. Because the algorithm involves backtracking, squares can be revisited, as in the example of square 60 above. My goal is to show these numbers in place in the 9X9 grid. So at the start we’d see a 1 in space 1, a 3 in space 2, another 1 in space 3, then that 1 would remove itself and a 4 would appear in its place, and so on. In the case of 60, the first time it would display 3, then  5. When it revisits 60, the 5 removes itself and then we see 4, 9, then finally 8.

That is tricky. It’s simple enough to access the numbers you want with for-loops, and set off the transitions, but then the transitions will all fire off at the same time. If only there was a way to break out of the for-loops temporarily, and then pick up later where you left off…

ah, coroutines!

local solutions = {{1, 2}, {2, 2, 3}, {3, 8}, {4, 5}, {5, 8, 4}, {6, 7}, {7, 1}, {6, 4, 3}, {8, 4}, {9, 6, 4}, {2, 1, 3}} local boxes = {} local numbers = {} local counter = 0 for row = 1, 3 do for col = 1, 3 do local x, y = 50 \* col + 5\*col, 50 \* row + 5\*row counter = counter + 1 boxes[counter] = display.newRect( x, y, 50, 50 ) boxes[counter]:setFillColor(1, 0, 0) numbers[counter] = display.newText("", x, y, "courier", 16) end end local sudocoro local nextNumber = function() coroutine.resume(sudocoro) end sudocoro = coroutine.create(function() for i = 1, #solutions do for j = 2, #solutions[i] do local numberIndex, value = solutions[i][1], solutions[i][j] numbers[numberIndex].text = value numbers[numberIndex].alpha = 0 transition.to(numbers[numberIndex], {alpha = 1, onComplete = nextNumber, time = 1000}) coroutine.yield() end end end) nextNumber()

cool, thanks. I will try this out when i get a chance and post back.

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() ) &nbsp;myDisplayGroup = display.newGroup() &nbsp;printGenerateButton() --local currentSquareRecord = newAutotable(3) currentSquareRecord = {{}} &nbsp;sudoku =&nbsp; {{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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,2,3,4,5,6,7,8,9},{1,2,3,4,5,6,7,8,9}} --------------------------------- &nbsp;solution = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} &nbsp;solutionForDisplay =&nbsp; &nbsp;{{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}} 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 = &nbsp;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&nbsp; --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 &nbsp;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&nbsp; -------------------------------- --FUNCTION: LOOK BACK DOWN THE CURRENT ROW -------------------------------- local function lookBackDownRow() rowMatch = false local j = currentSquare - 1 local limit = 0 if (currentSquare &nbsp;\<= 9) then limit = 0 elseif (currentSquare &nbsp;\<= 18) then limit = 9 elseif (currentSquare &nbsp;\<= 27) then limit = 18 elseif (currentSquare &nbsp;\<= 36) then limit = 27 elseif (currentSquare &nbsp;\<= 45) then limit = 36 elseif (currentSquare &nbsp;\<= 54) then limit = 45 elseif (currentSquare &nbsp;\<= 63) then limit = 54 elseif (currentSquare &nbsp;\<= 72) then limit = 63 elseif (currentSquare &nbsp;\<= 81) then limit = 72 end while (j &nbsp;\> 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 &nbsp;\> 0) do if (numberChosen == solution[y]) then --print("in looking up and found match") columnMatch = true&nbsp; break end -- if y = y - 9 end -- while&nbsp; --end -- if&nbsp; 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&nbsp; (numberChosen == solution[currentSquare - 8]) &nbsp;) then subsquareMatch1 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or&nbsp; currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 8]) &nbsp;) then subsquareMatch1 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 11]) &nbsp;) 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&nbsp; (numberChosen == solution[currentSquare - 8]) or&nbsp; (numberChosen == solution[currentSquare - 16]) or&nbsp; (numberChosen == solution[currentSquare - 17]) &nbsp;) then subsquareMatch2 = true end end if (currentSquare % 9 == 2 or currentSquare % 9 == 5 or currentSquare % 9 == 8) then if ( (numberChosen == solution[currentSquare - 8]) or&nbsp; (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 19]) or&nbsp; (numberChosen == solution[currentSquare - 17]) &nbsp;) then subsquareMatch2 = true end end if (currentSquare % 9 == 3 or currentSquare % 9 == 6 or currentSquare % 9 == 0) then if ( (numberChosen == solution[currentSquare - 10]) or&nbsp; (numberChosen == solution[currentSquare - 11]) or&nbsp; (numberChosen == solution[currentSquare - 19]) or&nbsp; (numberChosen == solution[currentSquare - 20]) &nbsp;) then subsquareMatch2 = true end end end -- function checkSubsquare1 --------------------------------- -- CALL THE VERIFICATION FUNCTIONS --------------------------------- if ((currentSquare \>= 10 and currentSquare \<=18) or&nbsp; (currentSquare \>= 37 and currentSquare \<=45) &nbsp;or&nbsp; (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or&nbsp; (currentSquare \>= 46 and currentSquare \<=54) &nbsp;or&nbsp; (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&nbsp; rowMatch == true or&nbsp; subsquareMatch1 == true or&nbsp; 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&nbsp; (currentSquare \>= 37 and currentSquare \<=45) &nbsp;or&nbsp; (currentSquare \>= 64 and currentSquare \<=72) ) then checkSubsquare1() end if ((currentSquare \>= 19 and currentSquare \<=27) or&nbsp; (currentSquare \>= 46 and currentSquare \<=54) &nbsp;or&nbsp; (currentSquare \>= 73 and currentSquare \<=81) ) then checkSubsquare2() end end --ASSIGN VERIFIED NUMBER TO THE SOLUTION ARRAY solution[currentSquare] = &nbsp;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 &nbsp; &nbsp; for col = 1, 9 do &nbsp; &nbsp; &nbsp; &nbsp; local x, y = 20 \* col + 5\*col, 20 \* row + 5\*row &nbsp; &nbsp; &nbsp; &nbsp; --if row%3==1 then row = row + 10 end &nbsp; &nbsp; &nbsp; &nbsp; counter = counter + 1 &nbsp; &nbsp; &nbsp; &nbsp; boxes[counter] = display.newRect( x, y, 20, 20 ) &nbsp; &nbsp; &nbsp; &nbsp; boxes[counter]:setFillColor(1, (col%3)\*.1, row\*.1) &nbsp; &nbsp; &nbsp; &nbsp; numbers[counter] = display.newText("", x, y, FONT\_NAME, 12) &nbsp; &nbsp; end end local sudocoro local nextNumber = function() &nbsp; &nbsp; coroutine.resume(sudocoro) end sudocoro = coroutine.create(function() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i = 1, #currentSquareRecord do &nbsp; &nbsp; &nbsp; &nbsp; for j = 2, #currentSquareRecord[i] do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; local numberIndex, value = currentSquareRecord[i][1], currentSquareRecord[i][j] &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[numberIndex].text = value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; numbers[numberIndex].alpha = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition.to(numbers[numberIndex], {alpha = 1, onComplete = nextNumber, time = 100}) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; coroutine.yield() &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; 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&nbsp; local j = 1 local stringy = "" local hspacer = 300 local vspacer = 50 for b=1,81 do local stringy = tostring(solution[b]) solutionDisplay1 = &nbsp;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) &nbsp; &nbsp;if ( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(event.target) &nbsp; &nbsp; event.target:setFillColor(.87,.3,.1,.3) &nbsp; &nbsp;elseif &nbsp;( event.phase == "ended" ) then &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(nil) &nbsp; &nbsp; generateButton:removeSelf() &nbsp; &nbsp; generateButton = nil if(myDisplayGroup ~= NULL) then &nbsp; 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()