How to use array to check a space is empty or not in puzzle game
I would suggest using a certain symbol or number for an empty space. In that way your array is always occupied with values even though no marker/piece is placed on the “board”. For example, if your pieces is marked 1 to 10 you could use 0 (zero) for an empty place.
can you give me a simple example code
Say you have a 4x4 board with certain pieces on it, maybe player1 and player2 with corresponding numbers in the array.
local myArray = {{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}} local isEmpty = function(row, col) if myArray[row][col] == 0 then return true else return false end end
you can then ask if the space you are looking for is empty with the isEmpty function. Hope this helps…
Can you check my code how to change local box = {} to local myArray = {{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}}
local box = {} for i =0 ,10 do box[i]={} for j = 0,4 do box[i][j]=display.newRect(0,0,35,35) box[i][j].x = j \*40+ 60 box[i][j].y = i \* 40 + 50 box[i][j].alpha = 0.1 end end local block = display.newRect(0,0,35,35) block:setFillColor(222,222,222) block.x=box[0][2].x block.y=box[0][2].y
and how to drop the block and check the space in empty or not
Thanks
Maybe this could help you… Added a function: isPlayer.
local boxArray ={{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}} local isEmpty = function(row, col) if boxArray[row][col] == 0 then return true else return false end end local isPlayer = function(player,row, col) if boxArray[row][col] == player then return true else return false end end local box = {} for i =1 ,4 do --row box[i]={} for j = 1,4 do -- col box[i][j]=display.newRect(0,0,35,35) box[i][j].x = j \*40+ 60 box[i][j].y = i \* 40 + 50 box[i][j].alpha = 1 if isEmpty(i,j) then -- Empty square is gray box[i][j]:setFillColor(50,50,50) elseif isPlayer(1,i,j) then -- Player 1 is red box[i][j]:setFillColor(250,0,0) elseif isPlayer(2,i,j) then -- Player 2 is blue box[i][j]:setFillColor(0,0,250) else print("Something is wrong in the box...") end end end -- local block = display.newRect(0,0,35,35) -- block:setFillColor(222,222,222) -- block.x=box[0][2].x -- block.y=box[0][2].y
You could notice how I made boxArray separate from box. I think it is a good idea to have different data structures that handle the logic side of things and other code that handles the drawing of objects.
Can I use this on Tetris style game and how to add my block image into box array or I need to create array for block
It do think it is possible, but it is not exactly trivial. You have to think your operations over carefully. For example how does it work when you pivot a block? Where is the pivot point for each block and how will they move and what happens when the path is blocked?
It could be helpfull to store all different kinds of blocks in an separate array (or since it is lua, a table…). You can then enumerate the blocks and easily add new ones if you wish.
How can I add my block image into array and make it be 1
I am not sure I understand what you mean by that. The images is in the table named box.
You can always tag your image-objects with your own variable. Like this:
box[i][j].player = 1
As I see it the boxArray keeps track of your pieces and holds the current state of the game. The table named box is merely the visual representation of boxArray, but has very little to do with the game logic. But that is a matter of taste and design. There are other solutions, and this might not suit your needs or style of programming.
I would suggest using a certain symbol or number for an empty space. In that way your array is always occupied with values even though no marker/piece is placed on the “board”. For example, if your pieces is marked 1 to 10 you could use 0 (zero) for an empty place.
can you give me a simple example code
Say you have a 4x4 board with certain pieces on it, maybe player1 and player2 with corresponding numbers in the array.
local myArray = {{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}} local isEmpty = function(row, col) if myArray[row][col] == 0 then return true else return false end end
you can then ask if the space you are looking for is empty with the isEmpty function. Hope this helps…
Can you check my code how to change local box = {} to local myArray = {{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}}
local box = {} for i =0 ,10 do box[i]={} for j = 0,4 do box[i][j]=display.newRect(0,0,35,35) box[i][j].x = j \*40+ 60 box[i][j].y = i \* 40 + 50 box[i][j].alpha = 0.1 end end local block = display.newRect(0,0,35,35) block:setFillColor(222,222,222) block.x=box[0][2].x block.y=box[0][2].y
and how to drop the block and check the space in empty or not
Thanks
Maybe this could help you… Added a function: isPlayer.
local boxArray ={{1,0,0,2}, {1,0,0,2}, {1,0,0,2}, {1,0,0,2}} local isEmpty = function(row, col) if boxArray[row][col] == 0 then return true else return false end end local isPlayer = function(player,row, col) if boxArray[row][col] == player then return true else return false end end local box = {} for i =1 ,4 do --row box[i]={} for j = 1,4 do -- col box[i][j]=display.newRect(0,0,35,35) box[i][j].x = j \*40+ 60 box[i][j].y = i \* 40 + 50 box[i][j].alpha = 1 if isEmpty(i,j) then -- Empty square is gray box[i][j]:setFillColor(50,50,50) elseif isPlayer(1,i,j) then -- Player 1 is red box[i][j]:setFillColor(250,0,0) elseif isPlayer(2,i,j) then -- Player 2 is blue box[i][j]:setFillColor(0,0,250) else print("Something is wrong in the box...") end end end -- local block = display.newRect(0,0,35,35) -- block:setFillColor(222,222,222) -- block.x=box[0][2].x -- block.y=box[0][2].y
You could notice how I made boxArray separate from box. I think it is a good idea to have different data structures that handle the logic side of things and other code that handles the drawing of objects.
Can I use this on Tetris style game and how to add my block image into box array or I need to create array for block
It do think it is possible, but it is not exactly trivial. You have to think your operations over carefully. For example how does it work when you pivot a block? Where is the pivot point for each block and how will they move and what happens when the path is blocked?
It could be helpfull to store all different kinds of blocks in an separate array (or since it is lua, a table…). You can then enumerate the blocks and easily add new ones if you wish.
How can I add my block image into array and make it be 1