Hi!
I’ve realised that I only come here when I’m asking for help, which is a little selfish. So, with no further ado, I’d like to invite you to take part in a game of codegolf! Whoever can achieve the outcome with the fewest lines of lua code wins.
The rules?
- self.levmap is a table with 70 entries used to generate a 7 by 10 grid.
- if an entry is greater than 0, it is occupied.
- you need to get the width and height of the full tiles in the grid (self.width, self.height).
- you need to print the width and height once the sorting is complete.
For example.
if self.levmap =
{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,1,1,1,0,
0,0,0,1,1,1,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}
then
self.width= 3
self.height = 2
Here’s my entry (self taught amateur coder)
local toppest = 1
local bottomest = 11
local leftest = 8
local rightest = 1
for k, v in pairs(self.levmap) do
local x = ((k-1)%7)+1 --1, 7
local y = math.ceil(k/7) --1,10
if v > 0 then
if x < leftest then
leftest = x
end
if x > rightest then
rightest = x
end
if y < bottomest then
bottomest = y
end
if y > toppest then
toppest = y
end
self.height = (toppest-bottomest)+1
self.width = (rightest-leftest)+1
end
end
if self.levmaps[key+1] == nil then
print("size acquired! (josh don't forget to calculate tilesize here)",self.width,self.height)
end
end