I am trying to create an array with two dimensions where the row/col size is determined by user input, 6,9,12,15. They array can have no duplicates in either direction, no numbers in row duped and no numbers in cols duped.
the numbers are generated randomly with the upper bound dependent on the above mentioned user input.
Example:
1, 5, 3, 4, 2, 6
3, 4, 1, 2, 6, 5
4, 6, 2, 3, 5, 1
… etc
the included code does fine on the first row but working the next row and checking the column(s) against the prior rows eludes me. Can someone please assist?
--[[Array - 2 dimensions: goal, two dimensional array of flexible size (arrayBounds). Array to have random numbers of range in 1 - nBounds. Rows require no duplicate values and the columns no duplicate values: 1,3,6,2,5,4 3,5,2,4,6,1 5,4,1,6,3,2 ... etc. Issue: Can get the initial row but stumped getting the column/row duplicate validation. --]] -- initial array declaration array = {} local rSeed = math.randomseed(os.time()) local mR = math.random local checkRow = {} local checkCol = {} local usedRowList = {} local usedColList = {} local arrayBounds = 6 local arrayCheckMax = 21 local cValue = 0 local cNValue = 0 local notNew = true local buildArrayRow local buildArrayCol local buildArray local seedCell seedCell = function() local cellVal = mR(1,6) return cellVal end -- zero filled , this is a test for the creation of an array buildArray = function() -- build initial empty for a=1, arrayBounds do array[a] = {} for b=1, arrayBounds do array[a][b] = 0 end end return array end ---------------------------------------------------------------------- buildArrayRow = function(currArray, a, bLimit) local usedSum = 0 local i = 0 local inList = true local rowdupes = false local notNew = false local complete = false local dCnt = 0 print("buildArrayRow: incoming -a- value is "..a.." ") for b = 1, bLimit do -- build row data notNew = true cValue = seedCell() -- get random cell value limited by the arrayBounds usedRowList[b] = cValue -- load used number print("buildArrayRow: cell value "..cValue.." used list value "..usedRowList[b].." b is "..b.." col a is "..a.." ") if b == 1 then array[a][b] = cValue elseif b == arrayBounds then -- check to see if this is the last cell for the row, -- sum all prior values, subtract from max and assign result -- set exit condition on result assignment local usedSum = 0 for k = 1, b - 1, 1 do usedSum = usedSum + usedRowList[k] end -- end for array[a][b] = arrayCheckMax - usedSum notNew = false else print("buildArrayRow: b not 1 b is "..b.." a is "..a.." ") -- -- validation of new numbers for row -- print("buildArrayRow: start pass "..b.." ==================================================") while notNew do -- loop until new value is attained complete = false dCnt = 0 while not complete do -- loop until value for cell is completely validated for j = 1, b - 1, 1 do -- is it already used? print("buildArrayRow: in usedList process: cValue is "..cValue.." usedList value "..usedRowList[j].." index is "..j.." ") if usedRowList[j] == cValue then -- value is already used print("buildArrayRow: getting next value from seedCell(); used = cval") cValue = seedCell() -- get new value print("buildArrayRow: next value from seedCell() is cValue "..cValue.." ") complete = true elseif usedRowList[j] ~= cValue and j \< b then dCnt = dCnt + 1 print("buildArrayRow: incrementing count value dCnt "..dCnt.." ") if dCnt == b then array[a][b] = cValue usedRowList[b] = cValue -- load to used list notNew = false complete = true break end -- end dCnt check end -- end usedList check end -- for loop end end -- completed while end end -- end notNew loop print("buildArrayRow: end pass "..b.." ==================================================") end -- end b check val end -- end b loop if a == 1 then for k = 1, a , 1 do for f = 1, 6, 1 do print("buildArrayCol: check array "..array[a][f].." ") end -- end inner for end -- end outer for else for k = 1, a -1 , 1 do for f = 1, 6, 1 do print("buildArrayCol: check array "..array[a][f].." ") end --end inner for end -- end outer for end -- end if/else return(currArray) end -- end buildArrayRow ---------------------------------------------------------------------- buildArrayCol = function(currArray) local usedSum = 0 local i = 0 local inList = true local rowdupes = false local notNew = false local complete = false local dCnt = 0 local b = 0 for a = 1, arrayBounds do -- populate array b = 1 notNew = true cValue = seedCell() -- get random cell value limited by the arrayBounds usedColList[a] = cValue -- load to used number print("buildArrayCol: a is "..a.." ") if a == 1 then buildArrayRow(currArray, a, arrayBounds) elseif a == arrayBounds then -- check to see if this is the last cell for the row, -- sum all prior values, subtract from max and assign result -- set exit condition on result assignment local usedSum = 0 for k = 1, a - 1, 1 do usedSum = usedSum + usedColList[k] end -- end for array[a][b] = arrayCheckMax - usedSum notNew = false else print("buildArrayCol: a not 1 a is "..a.." ") -- -- validation of new numbers for row -- print("buildArrayCol: start a pass "..a.." ==================================================") while notNew do -- loop until new value is attained complete = false dCnt = 0 while not complete do -- loop until value for cell is completely validated for j = 1, a, 1 do -- is it already used? print("buildArrayCol: in usedList process: cValue is "..cValue.." usedColList value "..usedColList[j].." index is "..j.." ") if usedColList[j] == cValue then -- value is already used print("buildArrayCol: getting next value from seedCell(); used = cval") cValue = seedCell() -- get new value print("buildArrayCol: next value from seedCell() is cValue "..cValue.." ") complete = true elseif usedColList[j] ~= cValue and j \< a then dCnt = dCnt + 1 print("buildArrayCol: incrementing count value dCnt "..dCnt.." ") if dCnt == a then print("buildArrayCol: array is a"..a.." b "..b.." ") array[a][b] = cValue usedColList[a] = cValue -- load to used list buildArrayRow(array[a][b],a ,b) notNew = false complete = true break end -- end dCnt check end -- end usedList check end -- for j loop end end -- completed while end print("buildArrayCol: out of completed loop") end -- end notNew loop print("buildArrayCol: end pass "..a.." ==================================================") end -- end a check val if a == 1 then for k = 1, a , 1 do for f = 1, 6, 1 do print("buildArrayCol: check array a = 1 "..array[a][f].." ") end -- end inner for end -- end outer for else for k = 1, a -1 , 1 do for f = 1, 6, 1 do print("buildArrayCol: check array a = "..a.." "..array[a][f].." ") end -- end inner for end -- end outer for end -- end if/else end -- end a for loop return currArray end -- end buildArrayCol ---------------------------------------------------------------------- -- -- MAIN -- buildArray() print("MAIN: back from buildArray") for i, k in pairs(array) do -- validate empty array print("MAIN: row ",i," cells ", k[1], k[2], k[3], k[4], k[5], k[6]) end -- end validation empty array as test buildArrayCol(array) for i, k in pairs(array) do -- validate completed array print("row ",i," cells ", k[1], k[2], k[3], k[4], k[5], k[6]) end -- end validation empty array as test -- end main