Jump to content

View more

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How to match a 2d pattern in a 2d array?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 thescholar   Members   


Posted 30 October 2012 - 04:01 PM

I'm trying to figure out how it's possible to match a 2D pattern inside a 2D array as shown below;

2D array
1, 0, 0, 1, 0, 1
1, 0, 1, 1, 1, 1
0, 1, 0, 0, 1, 0
1, 0, 1, 1, 0, 1
0, 0, 0, 0, 1, 0
1, 1, 1, 1, 0, 1

2D pattern to be matched
1, 0, 1
0, 1, 0
1, 0, 1

Let's mention that the 2D array can be larger in other contexts. I'm also using 0 and 1 as values, but it could be any other IDs. I just tried to keep my example simple. Anyone know what would be the most efficient and fastest way to match all patterns in the 2d array in a C++ or C# program (or whatever)?

It's for a game I'm trying to make. When clicking with the mouse the player set tiles on the grid, so in real-time I need to check if the pattern condition is met each time the player click to change a tile.

URLs or tutorials with similar topics would be greatly appreciated. Thank you for your insights!

Edited by thescholar, 30 October 2012 - 04:02 PM.

#2 Olof Hedman   Members   


Posted 30 October 2012 - 04:14 PM

You need to iterate through your pattern and test it ID by ID at position (0,0), then move to (1,0) do the same, etc, for all positions.
As soon as you find a mismatch, you can move on, so maybe not as slow as you think.

If a pattern appeared, it must contain the newly placed tile right?
So you don't have to check through the whole array, only in a box roughly 2x the size of your pattern, around the position where the user clicks.

Edited by Olof Hedman, 30 October 2012 - 04:20 PM.

#3 Rorakin   Members   


Posted 30 October 2012 - 05:31 PM

The brute force method above by Olof is the fastest you can achieve for matching any random pattern to any random 2d grid during run-time.

However, assuming you have a finite set of defined dimensions for your pattern (3x3, 2x2, 4x4 for example), then you could create separate data structures for the large 2d grid at the beginning of your program. So, for example, you create a 4x4 grid of unique IDs from your 6x6 2d array for 3x3 pattern matching. The IDs calculated can be created from a hashmap function that you define to return a unique ID for a given 3x3 input, and then at runtime you simply pass in your 3x3 pattern to the function and then look to see if the ID returned is in your 4x4 grid. You would have to create separate data structures in the beginning of your program for the different dimensions of patterns you planned to support.

#4 thescholar   Members   


Posted 31 October 2012 - 02:12 PM

Thanks guys, very helpful!

#5 DDoS   Members   


Posted 03 November 2012 - 08:48 AM

i think brute force is not the fastest method possible, but it's the easiest to implement.
how are you storing your data?
an adaption of a text search algorithm like boyer moore could be faster, also clustering the 0,1 into integers, and compare at integer level would be alot faster

if you need all matches of the pattern i'd use 2d convolution on the input data, with the pattern as filter kernel.
if your data is really really huge you can also speed up the convolution by fourier transform...

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.