Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ideas966 · Aug 02, 2013 at 03:52 AM · gridpuzzleblocksrecursion

Problem searching grid using recursive function

Hello, I am working on a falling-block puzzle game similar to Tetris or Lumines. Players guide blocks as they fall stack them on the bottom of a grid. The way blocks are cleared is that each block needs to be "marked" by a nearby bomb block is exploded. Once all blocks in a chain (connected blocks of same color) are marked, that chain is cleared from the board.

I wrote a series of functions to check and see when a chain is completely marked and to clear it. However I am having a problem where blocks are getting cleared before the entire chain is marked.

I use this function on each marked block to see if the chain it is in is completely marked or not. It is supposed to check each connected marked (called primed in code) block of the same color and return false as soon as one block in the chain is connected to an unchecked block of the same color, and return true otherwise. So the function should assume the chain is fully marked until a single block is determined to be in the chain that is not marked, at which point it should stop working and return false.

 public bool checkFullChain(int x, int y, int type) //return true if the chain is full
     {
         bool check = true;
         gridSearched[x,y] = true; // mark each block as searched to avoid infinite looping
         if(connectedToUnprimed(x, y, type)) //this function determines if the block is touching an unchecked block or not
         {
             Debug.Log("incomplete chain at: " + x +","+ y);
             check = false;    //the chain is incomplete if a single primed block is touching a single unprimed block
             return check;
         }
         else
         {
             if(this.GetComponent<gameController2>().grid[x+1,y] == type && gridSearched[x+1,y] == false && check == true) //check to see if the rest of chain is connected to an unprimed block or not
             {
                 check = checkFullChain(x+1, y, type);
             }
             else if(this.GetComponent<gameController2>().grid[x-1,y] == type && gridSearched[x-1,y] == false && check == true)
             {
                 check = checkFullChain(x-1, y, type);
             }
             else if(this.GetComponent<gameController2>().grid[x,y+1] == type && gridSearched[x,y+1] == false && check == true)
             {
                 check = checkFullChain(x, y+1, type);
             }
             else if(this.GetComponent<gameController2>().grid[x,y-1] == type && gridSearched[x,y-1] == false && check == true)
             {
                 check = checkFullChain(x, y-1, type);
             }
         }
         return check; //return true if the chain has no unprimed blocks in it
     }

So do you think that this function works as intended? I feel like the error I'm getting is that as soon as the seed block for a chain is not touching an unchecked block, it is clearing it out, as if the entire chain isn't being checked. Am I using recursion wrong? I'm thinking that the function is returning true as soon as a single recursive statement returns true, or something. What do you think? I want to at least determine if this function works properly or not so I can know if I should check a different function.

Thanks for any help.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by VioKyma · Aug 04, 2013 at 01:59 AM

For clarity you may even want to use one Boolean for each adjacent block position, then

 if(left && right && top && bottom)
 {
     return true;
 }
 else
 {
     return false;
 }

this is more for your own ability to follow the logic, but this seems to be where the problem lies so it may help in show the issue.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VioKyma · Aug 02, 2013 at 04:33 AM 0
Share

It seems that Jamora is right. The else if usage means that if one of those cases is true, the rest will be skipped, which is not what you want to happen (if I understand your algorithm correctly). If you want the algorithm to check every one of those conditions, simply remove the else from all the if statements (EDIT: that is lines 17, 21 and 25) and it will sequentially check each case.

avatar image Ideas966 VioKyma · Aug 05, 2013 at 11:07 PM 0
Share

Thanks a lot for your help guys. I got it working now by switching to a 4-boolean system as suggested by viokyma. Thanks!

avatar image VioKyma VioKyma · Aug 06, 2013 at 05:18 AM 0
Share

Excellent, glad I could help sort it out.

avatar image VioKyma · Aug 14, 2013 at 01:04 AM 0
Share

Please mark this as the answer if it has helped so others can see this question is resolved. Thanks :)

avatar image
1

Answer by Jamora · Aug 02, 2013 at 04:05 AM

One thing that strikes odd is that you use else if's. Only one of your if clauses are being entered... Maybe I just didn't understand your algorithm properly.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ideas966 · Aug 02, 2013 at 01:42 PM 0
Share

I actually had my code like that before and it still had the same problem (in fact there's no discernible difference between using if statements and else if statements).

$$anonymous$$y code now looks like this but I still have the same problem:

 public bool checkFullChain(int x, int y, int type) //return true if the chain is full
     {
         bool check = true;
         gridSearched[x,y] = true; // mark each block as searched to avoid infinite looping
         if(connectedToUnprimed(x, y, type))
         {
             Debug.Log("incomplete chain at: " + x +","+ y);
             check = false;    //the chain is incomplete if a single primed block is touching a single unprimed block
             return check;
         }
         else
         {
             if(this.GetComponent<gameController2>().grid[x+1,y] == type && gridSearched[x+1,y] == false && check == true) //check to see if the rest of chain is connected to an unprimed block or not
             {
                 check = checkFullChain(x+1, y, type);
             }
             if(this.GetComponent<gameController2>().grid[x-1,y] == type && gridSearched[x-1,y] == false && check == true)
             {
                 check = checkFullChain(x-1, y, type);
             }
             if(this.GetComponent<gameController2>().grid[x,y+1] == type && gridSearched[x,y+1] == false && check == true)
             {
                 check = checkFullChain(x, y+1, type);
             }
             if(this.GetComponent<gameController2>().grid[x,y-1] == type && gridSearched[x,y-1] == false && check == true)
             {
                 check = checkFullChain(x, y-1, type);
             }
         }
         return check; //return true if the chain has no unprimed blocks in it
     }

So do you see anything else that could be wrong, or is my problem probably in a different function?

avatar image Jamora · Aug 02, 2013 at 08:49 PM 0
Share

It would seem that the last condition in your ifs act much akin to an else clause. Remove the && check == true and ins$$anonymous$$d deter$$anonymous$$e the state of check with

 check = check && checkFullChain(x, y-1, type);
avatar image Ideas966 · Aug 02, 2013 at 10:11 PM 0
Share

Could you elaborate a little more? Where should that line of code that you written go? Is it part of the if statement or inside each if statement?

You're saying each If statement should look like this?

 if(this.GetComponent<gameController2>().grid[x,y-1] == type && gridSearched[x,y-1] == false)
             {
                 check = check && checkFullChain(x, y-1, type);
             }
 
avatar image Jamora · Aug 03, 2013 at 08:13 AM 0
Share

That's exactly what I'm saying, does it still not work?

avatar image Ideas966 · Aug 03, 2013 at 06:24 PM 0
Share

Correct. I still have see$$anonymous$$gly the exact same problem. $$anonymous$$y code looks like this now:

 public bool checkFullChain(int x, int y, int type) //return true if the chain is full
     {
         bool check = true;
         gridSearched[x,y] = true; // mark each block as searched to avoid infinite looping
         if(connectedToUnprimed(x, y, type))
         {
             Debug.Log("incomplete chain at: " + x +","+ y);
             check = false;    //the chain is incomplete if a single primed block is touching a single unprimed block
             return check;
         }
         else
         {
             if(this.GetComponent<gameController2>().grid[x+1,y] == type && gridSearched[x+1,y] == false) //check to see if the rest of chain is connected to an unprimed block or not
             {
                 check = check && checkFullChain(x+1, y, type);
             }
             if(this.GetComponent<gameController2>().grid[x-1,y] == type && gridSearched[x-1,y] == false)
             {
                 check = check && checkFullChain(x-1, y, type);
             }
             if(this.GetComponent<gameController2>().grid[x,y+1] == type && gridSearched[x,y+1] == false)
             {
                 check = check && checkFullChain(x, y+1, type);
             }
             if(this.GetComponent<gameController2>().grid[x,y-1] == type && gridSearched[x,y-1] == false)
             {
                 check = check && checkFullChain(x, y-1, type);
             }
         }
         return check; //return true if the chain has no unprimed blocks in it
     }

I just want to make sure this function is working how it is supposed to (maybe another part of my code is working wrong), although I'm pretty sure this is the function causing the problem. As soon as a marked block is touching 2 or more other marked blocks and isn't touching an unmarked blocks, all marked blocks in the chain get cleared.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Puzzle piece rotation 0 Answers

How to let player move for specific number of times in gird base system.,Implement a move system in grid where player can move for specific number then dies if he did not meet game end/ 0 Answers

Stacking Lego pieces 2 Answers

Grid Based Mouse Drag 1 Answer

Make my grid fall and spawn new objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges