- Home /
Using C# delegates and events to detect similar tiles.
Hello,
How to use delegates and events to select tiles of the same color? For example, I have a grid similar to this:
And I would like to know, how to use delegates and events to select the same tiles in one group (the same tiles contact each other - see the red tiles in bottom center of the image).
Could you give me a sample code/pseudocode or idea how to do this?
Answer by fafase · May 23, 2014 at 03:20 PM
Delegate and event are no algorithm they are tools/features from the language, a delegate won't tell you if you have aligned tiles. You need to define first the algorithm and then you can use a delegate or an event to call that method.
Delegates and events have many tutorials all over the net since they are not Unity related features.
Now for your algorithm, you need a search principle. First you check the first tile and look for all adjacent tiles, if one matches color, you add it to a queue AND a list. Then you start the same process with the queue until the queue is empty making sure you mark the tiles as visited so that you don't process the same ones over and over again.
When the queue is empty, you check the count of the list and if it is greater or equal 3 you have your tiles in the list for candidate of destruction. Repeat that process until you have done the whole grid and the list is empty.
so:
while grid is not entirely done take first and then next
Check adjacents
if same color add to queue and list
while queue is not empty back to 2
When queue is empty check list count
if greater or equal to 3 destroy all content of list
if grid is not entirely done back to 1
Answer by Tarlius · May 23, 2014 at 03:48 PM
Maybe I misunderstood the question... But you could do it with something like this I guess...
public event System.Action<Block> OnSelected;
public event System.Action<Block> OnDestroyed;
OnNeighbourDestroyed(Block b) {
OnSelected -= OnNeighbourSelected;
OnDestroyed -= OnNeighbourDestroyed;
}
void OnNeighbourSelected(Block b) {
if(this.isSelected) return; // Don't re-trigger own event for recursive pain
if(b.color = color) OnSelected();
}
void Init(Block upB, Block downB, leftB, Block rightB) {
RegisterBlockEvents(upB);
RegisterBlockEvents(downB);
RegisterBlockEvents(leftB);
RegisterBlockEvents(rightB);
}
void RegisterBlockEvents(Block b) {
OnSelected += OnNeighbourSelected;
OnDestroyed += OnNeighbourDestroyed;
}
However, with this kind of approach you will have lots of "fun" making sure all the events point to the right blocks all the time... It would probably be more simple to have a class controlling the board that has access to the colors that can search from a given starting point, for example:
int[,] blockColors
List FindSameColor(Vector2 start) {
points = new List
color = blockColors[start]
return Check(color, start, points
}
List Check(color, currentPoint, matchedPoints) {
if(matchedPoints.Contains(currentPoint) return
matchedPoints.Add(currentPoint);
if(blockUp.color == color) Check(color, blockUp, matchedPoints)
if(blockDown.color == color) Check(color, blockUp, matchedPoints)
if(blockLeft.color == color) Check(color, blockUp, matchedPoints)
if(blockRight.color == color) Check(color, blockUp, matchedPoints)
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to make different Rule Tiles interact with each other? 0 Answers
C# array equal to another array minus one entry. 1 Answer
How to unsubscribe from InputSystem event properly 0 Answers