Alright, this is a stright-up lua question.
I need to take the following steps in my game:
1 - select four random empty cells from a grid of horizontal magnitude hSize and vertical magnitude vSize.
2 - pass those four cells to another function, which might take a long time to process them
3 - select one of the four elements, and mark it filled. (We might also "fill" other elements at this time)
4 - repeat.
My current (working) implementation uses three tables -
* "grid", which stores the data for each cell of the grid
* "sorter", which is a numerically-indexed list of size hSize*vSize, and stores a list of paired grid coordinates in each element
* "currentSelections", which is a numerically-indexed list of size four, and stores the four coordinate pairs for the currently selected cells.
My script looks like this:
Code: Select all
grid = {}
sorter = {}
curList = {}
for i=1,4 do -- initialize currentList
curList[i] = {}
curList[i].x = 0
curList[i].y = 0
end
for x = 1,hSize do -- initialize other tables
grid[x] = {}
for y = 1, vSize do
grid[x][y] = {}
--data for each cell goes in here, but is not relevant now
sortNumber = x + hSize*(y-1)
sorter[sortNumber] = {}
sorter[sortNumber].xC = x
sorter[sortNumber].yC = y
end
end
function PickFour()
local i
for i = 1, 4 do
index = math.random(1,table.maxn(sorter))
selectedX = sorter[index].xC
selectedY = sorter[index].yC
curList[i].x = selectedX
curList[i].y = selectedY
table.remove(sorter, index)
end
end
function ReturnThree(pick) -- pick is a value from 1 to 4
for i = 1,4 do
if(i ~= pick) then
place = table.maxn(sorter) + 1
sorter[place] = {}
sorter[place].xC = curList[i].x
sorter[place].yC = curList[i].y
end
end
end
It's 3:30am, and I'm not satisfied with this bit of script. I should probably get to sleep though. Maybe someone will have an observation for me in the morning.
--Mr. Strange