Calculating area consisting of several gameobjects

I’m trying to create a board game in the style of Carcassone, where players place tiles on a board. Each tile is divided in two or more colors, so that when you attach tiles you create larger areas of a color. Those areas could span several tiles. Scoring is based on the total size and shape of the area, so that larger areas would score higher.

In Unity each tile is a gameobject with a texture. Under the hood each tile has an ID so that I know what colors it consists of.

I’m stuck in how to find contiguous areas. Any pointers on where to start?

Imagine the following grid:
image

Each larger square is a gameobject tile, each having a different layout. i want to calculate the size of the larger red area (7 squares)

1 Like

Hello there, perhaps an drawing could help us understand the mechanic. Also, which engine you will be using?

Hi, thanks for the reply. I amended the original post with an example. The engine is Unity 2017.2

1 Like

Lol, fun thing, I have done exactly this same algorithm yesterday to find adjacent biomes and it is still available online, not sure if it is the best approach for that though, Im using my mobile now, a few hours from now I’ll be on my pc so i can give further help if needed:

https://pastebin.com/bPfKScAc
Was used to identify biome areas in the biomemap generated below
bandicam_2018-01-14_09-21-41-786

You will probably have to combine the tiles into an array2d accounting for every subtile, from this array you can use an variation of the algorithm I’ve shared. I’m sure it isn’t as performart as it could be, but it works, I will iterate upon it eventually.

Looks cool. I’ll have a look through it tomorrow to see if it helps me out. I’m a beginner at C# still, so we’ll see if anything makes sense to me. Still, thanks for the effort :slight_smile:

1 Like

May be you could store a 2x2 array in addition to ID field, and also have a Grid component that would do calculations on this tiles.
Something like for colours you would have:

red = 1;
green = 2;
blue = 3;

So in a Grid you could retrieve info about each tile, and you get an array like this:

3 2 2 3 2 2
2 1 1 1 2 3
2 2 1 1 1 3
3 3 3 1 3 3
3 2 3 3 1 1
3 2 3 2 2 1

And now to work with this array

1 Like

Let us know if you need help once you start implementing it :slight_smile:

I’ve figured it out! I went with Maxim’s idea of giving each tile in the grid a color code. I then went through each node in the grid to give it a unique number and to find out its direct neighbors and check to see if this has the same color. I then made a list of the matches and set their unique numbers to the same as the tile I am checking.

When I went through the enire grid, all the tiles in the grid with a connecting color have the same unique number. Then I only need to count these numbers to know the score.

https://pastebin.com/132WcrTr

For this I had to make a three dimensional array (something I never done before, I learned something new :slight_smile:) to store each xy-coordinate and unique number. And found out its a real pain to keep track of which dimension to read and set. But in the end I learned amazing new tricks, like rotating and transposing 2D arrays.

As said I’m fairly new to C# (and programming in general), so I’m sure my code can be greatly improved. But so far I’m pretty happy that it works at all.

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms