I’m aware the code is quite messy but every so often I get a stack overflow due to listofmatches.
I tried to limit the amount of items going into the array, didnt work. Tried sleeping as well with love.timer.sleep which didnt work either. But I did print list of matches to find out that it was getting caught in an infinite loop.
checkforNil = function(self)
for i = 64, 56, -1 do
counter = 8
for j = i, 1, -8 do
--tempX = 0
tempY = 0
tempColor = 0
if gridTable[j].y == nil then
for test = j, 1, -8 do
if gridTable[test].y ~= nil then
--tempX = gridTable[i].x
tempY = gridTable[test].y
tempColor = gridTable[test].color
--gridTable[j].x = tempX
gridTable[j].y = ((counter) * 50) + (((counter) - 1) * 15)
gridTable[j].color = tempColor
--gridTable[test].x = nil
gridTable[test].y = nil
gridTable[test].color = nil
break
end
end
end
for k = 1, 8 do
if gridTable[k].y == nil then
--gridTable[k].x = gridTable[i].x
gridTable[k].y = 50
gridTable[k].color = love.math.random(7)
end
end
--print(gridTable[j].x, j,"j")
counter = counter - 1
end
counter = 8
end
game:matches(gridTable)
end,
removeMatches = function(self, grid)
for j, v in ipairs(listofMatches) do
grid[v].y = nil
grid[v].color = nil
end
listofMatches = {}
game:checkforNil()
end,
matches = function(self, grid)
matchTest = false
listofMatches = {}
local matches = 0
for i = 1, 63 do
for x,y in ipairs(listofMatches) do
if i == y then
goto continue
end
end
::continue::
for j = i + 1, 64 do
currentColor = grid[i].color
if currentColor ~= grid[j].color then
if matches > 1 then
matchTest = true
for k = i, j - 1 do
table.insert(listofMatches, k)
end
end
matches = 0
break
else
matches = matches + 1
end
end
end
for num = 1, 8 do
for vertical = num, 48, 8 do
for x, y in ipairs(listofMatches) do
if vertical == y then
goto continue
end
end
::continue::
for down = vertical + 8, 64, 8 do
currentColor = grid[vertical].color
if currentColor ~= grid[down].color then
if matches > 1 then
matchTest = true
for k = vertical, down - 8 , 8 do
table.insert(listofMatches, k)
end
end
matches = 0
break
else
matches = matches + 1
end
end
end
end
if matchTest then
game:removeMatches(grid)
game.points = game.points + 500
matchTest = false
end
end,
swap = function(swap1, swap2)
temp1 = gridTable[swap1].color
temp2 = gridTable[swap2].color
gridTable[swap1].color = temp2
gridTable[swap2].color = temp1
game:matches(gridTable)
end,
New contributor
user25690029 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.