Search Unity

Simple algo for match 3

Discussion in 'Scripting' started by pretender, Nov 13, 2017.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    865
    Hi guys i am trying to figure out match 3 algo. I have a board with with objects that differs in one of the qualities.
    I need to check all four directions and get a list of adjacent objects that match the same quality. Is there a some kind of simple algo? the problem is that i can make a list of matches but they overlap... check out the image
     

    Attached Files:

  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    With a grid, just check each direction. Start at the piece that gets moved and subtract 1. If it matches, subtract another 1 and if it matches, you have a match 3. If not, check another direction. Granted, you may have multiple matches, so there is a bit more to it, but that's the basics.
     
    ADNCG likes this.
  3. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    I have never done this but if I understand you right, this is how I would go about it:

    1. Create a list for visited cells
    2. Create a list for cells with the same color
    3. Iterate through the board cell by cell (skip destroyed ones)
    3b) Put the current cell in list 1 and list 2 and in a queque
    4. Run a while loop until the queque is empty
    5. Dequeque and get neigbours of current cell
    6. For each neighbor
    6.a) if in list 1 ‘continue loop’
    6.b) else add to list 1
    6.c) if same color add to list 2 and add it’s neighbors to queque
    7) at end of while loop check if list 2 size is >2 and if it is ‘remove / destroy) pieces from board -> Continue loop (point 3)

    For sure better solutions exist, I answered more to give me something to think about ;)