Help with Tutorial - How to Add More Objects to a Grid?

Hi all, 

Hope you’re well. 

I’m a complete beginner that has been stuck on a piece of tutorial code for the past few days. Basically, the code and image below are a modified version of the code posted here (Corona Labs); it’s the setup for a basic checkers-type game. 

I have a quick question - the blue, yellow, and pink cards sit in a table and they’re distributed at random each time the game loads. However, for some reason I can’t work out, the code will only add 3-4 of them to the grid (there’s some kind of +/-1 function in there too).

All I want to do is retain the random element but fill the entire board with cards - so 16 (4x4) cards placed randomly. Which part of the code is responsible for the number of cards added? The checkerboard image itself isn’t important.

Thanks! And apologies if my code is a mess! I haven’t quite got the hang of anything yet …

Capture.png

----------------------------------------------------------------------------------------- -- -- -- ----------------------------------------------------------------------------------------- display.setStatusBar(display.HiddenStatusBar) centerX = display.contentWidth \* .5 centerY = display.contentHeight \* .5 local card local gridWidth = 4 local gridHeight = 4 local cellWidth = 80 local cellHeight = 80 local img = { "card-blue.png", "card-lblue.png", "card-pink.png", "card-yellow.png" } local grid = {} for i = 1, gridHeight do grid[i] = {} end --PLACES BACKGROUND-- local bg = display.newImageRect("Sky.png", 480, 700) bg.x = centerX bg.y = centerY --LOAD THE CHECKERBOARD-- local checkerBoard = display.newImageRect("table2.png", 320, 320) checkerBoard.x = display.contentCenterX checkerBoard.y = (300) local gbOffsetX = checkerBoard.x - ( checkerBoard.width \* checkerBoard.anchorX ) local gbOffsetY = checkerBoard.y - ( checkerBoard.height \* checkerBoard.anchorY ) --HANDLES TOUCH ON CARDS-- local function cardTouched(event) card = event.target if event.phase == "ended" then print("Touched!") transition.to ( event.target, { rotation=360, delta=true,} ) --card.isVisible = false end end --PLACES CARDS FROM TABLE, ADDS LISTENER TO EACH-- local function placeCards (xPos, yPos) local card = {} for y = 1, 4 do for x = 1, 4 do local r = math.random(1, #img); card = display.newImageRect(img[r], cellWidth, cellHeight ) if card then card:addEventListener ( "touch", cardTouched ) card.xPos = xPos card.yPos = yPos card:translate( 140, -27 ) card.x = (xPos - 1) \* cellWidth + (cellWidth \* 0.5) + gbOffsetX card.y = (yPos - 1) \* cellHeight + (cellHeight \* 0.5) + gbOffsetY end end end end local lastObject for i = 1, 4 do local xPos = math.random( 1, gridWidth ) local yPos = math.random( 1, gridHeight ) grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard) lastObject = grid[yPos][xPos] end

To fillful board use code

for i = 1, gridWidth do    for j = 1, gridHeight do         local xPos = i        local yPos = j        grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard)        lastObject = grid[yPos][xPos]     end    end

instead of

for i = 1, 4 do local xPos = math.random( 1, gridWidth ) local yPos = math.random( 1, gridHeight ) grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard) lastObject = grid[yPos][xPos] end

What ldurniat wrote is partially correct, it will work but you’ll in addition, create 256 cards instead of 16, as your placeCards function also iterates over all grid cells, so you get 4*4 * 4*4 = 256 cards

The placeCards function iterates over the 4x4 grid using the variables x/y but these are never used for anything - but they should either be used to calculcate the x/y position of the cards in the grid, then placeCards is all you need as it fills the grid already or you should remove the x/y iteration in place cards so it only creates a single card.

In your original code, you got the gaps in the grid, because you called placeCards 4 times in the loop at the end of your code which created a random position and then, at this position, created 16 cards, so in the end you had 64 cards.

[lua]

local function placeCards (xPos, yPos)

   local card = {}

   local r = math.random(1, #img);

      card = display.newImageRect(img[r], cellWidth, cellHeight )

      numCards = numCards + 1

      print( "NumCards "…tostring(numCards) )

         if card then

            card:addEventListener ( “touch”, cardTouched )

            card.xPos = xPos

            card.yPos = yPos

            card:translate( 140, -27 )

            card.x = (xPos - 1) * cellWidth + (cellWidth * 0.5) + gbOffsetX

            card.y = (yPos - 1) * cellHeight + (cellHeight * 0.5) + gbOffsetY

         end

      end

[/lua]

Brilliant! Thanks for the help both of you! It works :slight_smile:

Capture33.png

To fillful board use code

for i = 1, gridWidth do    for j = 1, gridHeight do         local xPos = i        local yPos = j        grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard)        lastObject = grid[yPos][xPos]     end    end

instead of

for i = 1, 4 do local xPos = math.random( 1, gridWidth ) local yPos = math.random( 1, gridHeight ) grid[yPos][xPos] = placeCards(xPos, yPos, checkerBoard) lastObject = grid[yPos][xPos] end

What ldurniat wrote is partially correct, it will work but you’ll in addition, create 256 cards instead of 16, as your placeCards function also iterates over all grid cells, so you get 4*4 * 4*4 = 256 cards

The placeCards function iterates over the 4x4 grid using the variables x/y but these are never used for anything - but they should either be used to calculcate the x/y position of the cards in the grid, then placeCards is all you need as it fills the grid already or you should remove the x/y iteration in place cards so it only creates a single card.

In your original code, you got the gaps in the grid, because you called placeCards 4 times in the loop at the end of your code which created a random position and then, at this position, created 16 cards, so in the end you had 64 cards.

[lua]

local function placeCards (xPos, yPos)

   local card = {}

   local r = math.random(1, #img);

      card = display.newImageRect(img[r], cellWidth, cellHeight )

      numCards = numCards + 1

      print( "NumCards "…tostring(numCards) )

         if card then

            card:addEventListener ( “touch”, cardTouched )

            card.xPos = xPos

            card.yPos = yPos

            card:translate( 140, -27 )

            card.x = (xPos - 1) * cellWidth + (cellWidth * 0.5) + gbOffsetX

            card.y = (yPos - 1) * cellHeight + (cellHeight * 0.5) + gbOffsetY

         end

      end

[/lua]

Brilliant! Thanks for the help both of you! It works :slight_smile:

Capture33.png