Need Help! Random Stacking of Boxes

Hi GoodDay to All,

Hi I am new here in Corona and also new in coding, I cant find any tutorials in what I want to code , 

I need to stack of box 7x4 layers with 4 different colors and changes position every start, but what I need is in every line of stack there is 3 same color positioned divided with any color should be only 3 same color in every stack… sorry for bad english, please see attached illustration of what I want to achive.

Thanks aLot

Arch

–Load & start physics

local physics = require(“physics”)

physics.setDrawMode( “hybrid” )

physics.start()

–draw a brick

–create a red box

local boxPositions = {

xGrid = {400,440},

yGrid = {50,90,130,170,210,250,290}

}

local boxCount 

local boxCount = (#boxPositions.xGrid * #boxPositions.yGrid) / 2

local function  spawnBox()

local box = display.newRect(0,0,40,40) 

box.x = boxPositions.xGrid[math.random(1,2)] 

box.y = boxPositions.yGrid[math.random(1,7)]

physics.addBody(crate, “static”,{density = 2.0 , friction =  0.5, bounce = 0.3})

end

timer.performWithDelay(100, spawnBox,boxCount)

So far i have come to this code…But it still multiple objects are spawning at the same spot…

So, do you want 3 blocks of the same colour in each column, separated by blocks of any other random colour?

This should not be difficult to write and I think that’s what I see in your image.

Yes sir thats what I need. but im stuck in that code I posted…

I was working on a little code to generate this for you, but ran out of time… so here is a description.

Write a routine to fill a table with random colours. Fill each column one at a time. When you fill a column, randomly choose a colour as a primary colour. Do not use the primary colour when filling that column. Once the column is full, add the primary colour at gaps, starting with either index [1], [2] or [3] and skip every other row:

local rnd = math.random(1,3) for i=rnd, rnd+5, 2 do     colourTbl[i]=primary end

Once that is done, just create your blocks in the correct locations on the screen and set their colour to the relative colour table entry.

Thank you Sir,

I will work on to it…

–Load & start physics

local physics = require(“physics”)

physics.setDrawMode( “hybrid” )

physics.start()

–draw a brick

–create a red box

local boxPositions = {

xGrid = {400,440},

yGrid = {50,90,130,170,210,250,290}

}

local boxCount 

local boxCount = (#boxPositions.xGrid * #boxPositions.yGrid) / 2

local function  spawnBox()

local box = display.newRect(0,0,40,40) 

box.x = boxPositions.xGrid[math.random(1,2)] 

box.y = boxPositions.yGrid[math.random(1,7)]

physics.addBody(crate, “static”,{density = 2.0 , friction =  0.5, bounce = 0.3})

end

timer.performWithDelay(100, spawnBox,boxCount)

So far i have come to this code…But it still multiple objects are spawning at the same spot…

So, do you want 3 blocks of the same colour in each column, separated by blocks of any other random colour?

This should not be difficult to write and I think that’s what I see in your image.

Yes sir thats what I need. but im stuck in that code I posted…

I was working on a little code to generate this for you, but ran out of time… so here is a description.

Write a routine to fill a table with random colours. Fill each column one at a time. When you fill a column, randomly choose a colour as a primary colour. Do not use the primary colour when filling that column. Once the column is full, add the primary colour at gaps, starting with either index [1], [2] or [3] and skip every other row:

local rnd = math.random(1,3) for i=rnd, rnd+5, 2 do     colourTbl[i]=primary end

Once that is done, just create your blocks in the correct locations on the screen and set their colour to the relative colour table entry.

Thank you Sir,

I will work on to it…