#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# 2D Table problem

edited January 2015 Posts: 5

I've read Ignatz's Lua ebook and I decided I was going to try and recreate a minesweeper like game but I'm having problems with these 2D tables without getting messy.

This here is the section of code I have a problem with. I'm trying to make a 10x10 grid which I can accomplish with this code block, I just copy and paste the whole section and change the "es[i]" to "es[i]" but as you can see that gets messy real quick... (The 10x10 grid is being drawn from left to right, bottom to top)

``````for i=1,n do
es[i]=squares(initx+50*i,inity+50)
end

--when es[i] inity+ an extra 50 (inity+100) and same for esi3
``````

This here is my second problem, pretty much the same problem as the one above. Those ones in the square brackets need to be 2's, then 3's etc...

``````for i=1,n do
rect(es[i].x,es[i].y,es[i].w,es[i].h)
end
``````

Any help would be greatly appreciated sorry if the code didn't get pasted right (I don't think it did)

Tagged:

• Posts: 5,396

Try this

``````es={}
for i=1,n do
es[i]={}
for j=1,n do
es[i][j]=squares(initx+50*i,inity+50*j)
end
end
``````

However, I would not do it this way, if what you are doing is drawing a grid. I would draw the grid just once, to an image in memory, then sprite that image to the screen in draw.

btw, format your code by putting `~~~` at the start and end

• edited January 2015 Posts: 5

Cool thanks, that works perfectly and I should be able to complete the rest of the code problem free I imagine. So you're saying you would sprite an image and then draw a grid over? If so, why's that because then I won't be able to change the colour of the squares that are pressed on to add a flag or mine etc? Unless I sprite them as well

• edited January 2015 Posts: 5,396

@BrodieRoberts - You'll probably want an outline for each square of the grid, and that's best done with a single image. Then you can fill the inside of selected squares with coloured rectangles.

You're going to be doing a lot of drawing in the squares, so rather than repeat the `initx+50*i,inity+50*j`type code over and over, you might consider writing a function that returns the x,y coords for a given grid reference, eg

``````--NB put the grid size in a variable to make it easy to change
--I'm assuming you put it in GridSize
function GridRef(col,row)
return initx+GridSize*col,inity+GridSize*row
end
``````

filling your squares with colour creates messy code, so maybe it's a good idea to put that in a function too

``````function FillSquare(col,row,colr)
local x,y=GridRef(col,row)
pushStyle()
fill(colr)
--fill the inside of the square, omit the border
rect(x+1,y+1,GridSize-2,GridSize-2)
popStyle()
end
``````

Then filling the grid square 3,4 is as easy as

``````--assumes colours are stored in variable names
--in this case, MARKED (makes changes easy)
FillSquare(3,4,MARKED)
``````
• Posts: 5

Thanks for that Ignatz, I'll look into it when I have a bit more time. Those +50 things aren't there anymore, they were just a quick fix so I could post to here. All of my variables can be changed easily and are put into classes. Thanks for the detailed and quick replies 