2D Table problem

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][1]” to “es[i][2]” 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

--when es[i][2] 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

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

Try this

for i=1,n do
    for j=1,n do

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

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

@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*jtype 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

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)
    --fill the inside of the square, omit the border

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)

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 :slight_smile: