How do I detect shapes in a grid of b&w diamonds?

Get help using Construct 2

Post » Sat Jan 30, 2016 10:38 am

How do I detect shapes in a grid of b&w diamonds?

Imagine a grid of diamonds, say 5x5, on the top of a cube. Each diamond is black or white. I want to be able to detect various arrangements of diamonds. For example, a larger diamond made up of 4 white diamonds or a line of 3 white diamonds diamonds and 1 black diamond. What would be a good way to do this?
B
10
S
4
G
3
Posts: 103
Reputation: 2,984

Post » Sat Jan 30, 2016 11:18 am

Try using an array to represent the information on the grid. For example, in a 5x5 array, have white diamonds represented by 1 and black diamonds represented by 2.

For each specific arrangement you are interested in, use "For each element" to create a set of conditions to check.

Ex: Line of 3 white diamonds and a black diamond

Array - For Each XY Element
Array.CurValue = 1
Array.At(Array.CurX+1,Array.CurY)=1
Array.At(Array.CurX+2,Array.CurY)=1
Array.At(Array.CurX+3,Array.CurY)=2

You can also use "black" and "white" as a string in the array, but numbers are usually easier to deal with.
Mistakes were made.
B
47
S
23
G
107
Posts: 1,571
Reputation: 60,133

Post » Sat Jan 30, 2016 12:00 pm

Thanks @oosyrag. Is there a way to do this without arrays or are arrays far easier than other methods?
B
10
S
4
G
3
Posts: 103
Reputation: 2,984

Post » Sun Jan 31, 2016 8:21 am

You might be able to do it by using the object's location, but I can't think of how to pick and compare locations of multiples of the same object off the top of my head.

Arrays are very useful in storing and manipulating data.
Mistakes were made.
B
47
S
23
G
107
Posts: 1,571
Reputation: 60,133

Post » Fri Feb 05, 2016 11:21 am

I'm giving arrays a go. I can't figure out how to link the 5x5 grid of sprites to the array. Is it based on position? Would this mean an isometric grid would be more difficult than a square grid?

EDIT: I'm having a go. I may be onto something...
B
10
S
4
G
3
Posts: 103
Reputation: 2,984

Post » Fri Feb 12, 2016 2:47 am

Thanks for the help @oosyrag. I see what you did there.

Now, I'm trying to figure out the most efficient way of relaying information from the diamonds to the array and from the array to the diamonds. For example, syncing up a sprite to switch from black to white while the array switches from 1 to 2 and having that trigger from the sprite side or from the array side.
B
10
S
4
G
3
Posts: 103
Reputation: 2,984

Post » Fri Feb 12, 2016 11:25 pm

Two ways I would consider -

Have the sprite objects always update to whatever value is in the array at any time. Clean, but might be a little more taxing on the system to update everything every tick. Depends on the scale of your project, this might not even be a concern.

Or, whenever you have an action to change the array, also add an action to change the respective sprite. Doubles up on actions in your event sheet, but it is the most efficient.

Regarding isometric grid vs square grid, it is all the same to the array. The isometric grid is a little trickier when positioning your sprites, but that should be all. Each sprite diamond should still be associated with one array cell. If it makes things easier, you can use an pair of instance variables on the sprite object to permanently reference its array position. ex. in DiamondSprite object, add variables Xposition = 1 Yposition = 3 , Then you can use those to look for the data with Array.At(DiamondSprite.Xposition, DiamondSprite.Yposition), which would give you the value in the array at 1,3
Mistakes were made.
B
47
S
23
G
107
Posts: 1,571
Reputation: 60,133


Return to How do I....?

Who is online

Users browsing this forum: Artpunk, Cool Gamez, marcoscodas and 10 guests