PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Puzzle game, check connected gems in 2D array-VBForums
Results 1 to 8 of 8

Thread: Puzzle game, check connected gems in 2D array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    21

    Delete Me

    Delete Me
    Last edited by AnonymousX; Jul 21st, 2016 at 05:45 PM.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Puzzle game, check connected gems in 2D array

    Don't have time for a long reply sorry (4:55pm UK time).

    If the player is adding gems one at a time can't you just check whether that gem is in a group with any others?

    The general algorithm you want is a 'flood fill' algorithm. It applies to image processing, but can also be used here.
    Basically start where the play fired the gem, then push that position onto a queue. While the queue isn't empty get a position and check it's adjacent cells that aren't in your list of found positions. The gem in the adjacent cell matches the starting cell's colour (or whatever) then add it to your list of found positions and to your queue.

    When your queue is empty the list contains all gems in the group, or just the original starting gem.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    21

    Delete Me

    Delete Me
    Last edited by AnonymousX; Jul 21st, 2016 at 05:46 PM.

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Puzzle game, check connected gems in 2D array

    How will your blocks of three check work when say two blocks of two are joined by a fifth gem?

    Assuming that destroyable shapes can only be formed by the thrown gem you could perhaps use something like this passing the final resting place of the thrown gem.
    c# Code:
    1. private int markShape(int x, int y)
    2.         {
    3.             int ct = 0;
    4.             if (grid[x, y] != 0)
    5.             {
    6.                 recurseShape(x, y, ref ct); //count and mark members by inverting colour
    7.                 if (ct < MINSHAPESIZE)
    8.                 {
    9.                     ct = 0;
    10.                     recurseShape(x, y, ref ct); //shape too small, invert colours back to default
    11.                 }
    12.             }
    13.             return ct;
    14.         }
    15.  
    16.         private void recurseShape(int x, int y, ref int count)
    17.         {
    18.             count++;
    19.             grid[x, y] *= -1;
    20.  
    21.             if (x > 0 && grid[x, y] == -grid[x - 1, y])
    22.                 recurseShape(x - 1, y, ref count);
    23.  
    24.             if (y > 0 && grid[x, y] == -grid[x, y - 1])
    25.                 recurseShape(x, y - 1, ref count);
    26.  
    27.             if (x + 1 < WIDTH && grid[x, y] == -grid[x + 1, y])
    28.                 recurseShape(x + 1, y, ref count);
    29.  
    30.             if (y + 1 < HEIGHT && grid[x, y] == -grid[x, y + 1])
    31.                 recurseShape(x, y + 1, ref count);
    32.  
    33.         }
    If the shape has >= MINSHAPESIZE members then all grid indexes of the shape are made negative.

    Edit: tested it and fixed a stupid bug (was doubling the count if not eligible)
    Last edited by Milk; Feb 1st, 2011 at 07:49 PM.
    W o t . S i g

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2007
    Posts
    21

    Delete Me

    Delete Me
    Last edited by AnonymousX; Jul 21st, 2016 at 05:49 PM.

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Puzzle game, check connected gems in 2D array

    I don't know if it would work I would need to test it. Have you looked at the code I have suggested? It's similar to what SLH is talking about except rather than making a list it marks the eligible gems in place by inverting the value. I don't know if you can see but it lets you choose how many gems constitute a destroyable shape making it easy to change and giving you something that works for any size 3,4,8 whatever. Do you follow? From what you have told us it sounds like you have chosen to make it 3 gems because you can't get your head around using more.
    W o t . S i g

  7. #7
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Puzzle game, check connected gems in 2D array

    Quote Originally Posted by Milk View Post
    How will your blocks of three check work when say two blocks of two are joined by a fifth gem?
    Also check your algorithm works for three blocks of two connected by a seventh:

    Code:
     | |x| |
    -+-+-+-+-
     | |x| |
    -+-+-+-+-
    x|x|o|x|x
    For example firing a gem into the space marked "o"

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Puzzle game, check connected gems in 2D array

    If you are checking as the gems are being filled in (e.g. on cell click etc.) then I thnk you need just 4 checks:
    If x-1 exists and is same color then mark them
    If x+1 exists and is same color then mark them
    If y-1 exists and is same color then mark them
    If y+1 exists and is same color then mark them
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." Charles F. Kettering

    Read articles on My Blog 101 LINQ Samples JSON Validator XML Schema Validator "How Do I" videos on MSDN VB.NET and C# Comparison Good Coding Practices VBForums Reputation Saver String Enum Super Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width