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 SlyyGuyy · Mar 07, 2014 at 07:51 PM · c#checkblocksround

Start new round after checking neighbors C#

In the game I'm working on, I want a new round to start when the player has 5 or less blocks that do not have matching neighbors. I already have a function that calls and checks for neighbors, but it doesn't initialize a new round. Do I need to do this in a new function or in the same function that checks for neighbors? I'll post my code here for my neighbor calling function and hopefully you all can add some assistance and help me progress!

 #region Find Neighbors
         public bool findNeighbor(GameObject clickedBlock) {
             
             Vector2 neighborPos = GetBlockPos(clickedBlock);
             bool foundNeighbor = false;
             
             if (neighborPos != new Vector2(-1,-1)) {
                 
                 Texture texType = clickedBlock.renderer.material.mainTexture;
                 
                 // Search for neighbor blocks going up
                 if (neighborPos.y > 0) {
                     if (grid[(int)neighborPos.x,(int)neighborPos.y-1].renderer.material.mainTexture == texType) {
                         foundNeighbor = true;    
                     }
                 }
                 // Search for neighbor blocks going right
                 if (neighborPos.x > 0) {
                     if (grid[(int)neighborPos.x-1,(int)neighborPos.y].renderer.material.mainTexture == texType) {
                         foundNeighbor = true;
                     }
                 }
                 
                 // Search for neighbor blocks going down
                 if (neighborPos.y < grid.GetLength(1)-1) {
                     if (grid[(int)neighborPos.x,(int)neighborPos.y+1].renderer.material.mainTexture == texType) {
                         foundNeighbor = true;
                     }
                 }
                 // Search for neighbor blocks going left
                 if (neighborPos.x < grid.GetLength (1)-1) {
                     if (grid[(int)neighborPos.x+1,(int)neighborPos.y].renderer.material.mainTexture == texType) {
                         foundNeighbor = true;
                     }
                 }
                 return foundNeighbor;
             } else { 
                 Debug.LogError ("NO BLOCKS WERE FOUND");
                 //New round here?
                 return false;
 
             }
         }
         #endregion
 
         #region checkForRoundEnd
         public bool checkForRoundEnd(GameObject clickedBlock) {
             Vector2 neighborBlocks = GetBlockPos(clickedBlock);
             bool newNeighbor = false;
             //Check for neighbors or count how many blocks are left
             //If there are less than 5 blocks or no neighbors start a new round
 
         }
         #endregion
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
1

Answer by SirCrazyNugget · Mar 10, 2014 at 02:41 AM

From the start of what you've posted passing a parameter clickedBlock it will have nothing to do with what you've posted in there.

Somewhere else in your code you'll have whatever code does something after the player has made a move, once this has been completed then you'll need to check whether there's any possible moves left (or less than 5 blocks).

Simply put after your players move code

 movesAvailable = false;
 for each block left in the game { //<-this is only pseudo
   if findNeighbor(block){
     movesAvailable = true;
   }
 }
 if(movesAvailable == false){
   //do restart code
 }

Ideally keep this is a separate function as mentioned by mattyman.

Comment
Add comment · Show 1 · 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 SlyyGuyy · Mar 13, 2014 at 09:50 PM 0
Share

Thanks for your answer SirCrazyNugget! This seems to be working a little better Here's how it looks:

 foreach(GameObject block in gameBoard.grid) {
         Debug.Log ("Hello blocks in grid");
         if(gameBoard.findNeighbor(block)) {
             Debug.Log ("STILL $$anonymous$$OVES AVAILABLE");
             movesAvailable = true;
         }
     }
     if(movesAvailable == false) {
         Debug.Log ("Hey man, there's no more moves");
         //roundControl(1);
     }

The only issue that it doesn't seem to want to drop out of the foreach loop and I believe this is because there are still blocks that are existing on the field, but there may not be any matches left. Because of this, it never returns false and thus never moves into the second if statement. Any suggestions on how to fix this?

avatar image
0

Answer by mattyman174 · Mar 07, 2014 at 11:44 PM

You should break it off into a new Function. Splitting code into meaningful methods is good practice as it helps with code management and debugging among other things. Only break up code blocks if it makes sense though, if your going to be using this RestartRound() Method multiple times elsewhere in your code, then its a good idea to have it in its own method. Whereas if you were only using the functionality once then it might not be as important to do so.

Comment
Add comment · Show 6 · 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 SlyyGuyy · Mar 08, 2014 at 03:36 PM 0
Share

Yeah, I agree, that makes sense. But would I want to execute it in the same way that's checking for neighbors in the function above? If not, how would I go about doing this?

avatar image mattyman174 · Mar 08, 2014 at 11:48 PM 0
Share

I want a new round to start when the player has 5 or less blocks that do not have matching neighbors

Theres your Search criteria for when you want to start a new round. Tally the players blocks that have neighbours and if that number is less than 5, start a new round.

avatar image SlyyGuyy · Mar 10, 2014 at 02:28 AM 0
Share

Right, I understand that that is my criteria and that's the way I need to go about creating a new round, I just don't understand how to actually implement that in code. I've attempted at checking for neighbors but I can't figure out how to implement the counting ability here.

avatar image SlyyGuyy · Mar 11, 2014 at 09:40 PM 0
Share

Okay so this is what I have set up in order to check neighbors for blocks and it's giving me a null reference exception stating that the object reference is not set to an instance of an object. For some reason. I am calling this function in the function that registers the user clicking on the blocks so that it calls this and checks for the grid to be empty. $$anonymous$$y apologies if I'm not being very clear, I've been trying to solve this issue for so long that nothing really makes sense anymore.

 #region check for emptiness so next round can be loaded
         public bool isEmpty() {
 
             for (int x = 0; x < 10; x++) {
                 Debug.Log ("Entered first loop");
                 for (int y = 0; y < 10; y++) {
                     Debug.Log ("Entered second loop");
                     // Search for neighbor blocks going up
                     if (y > 0) {
                         Debug.Log("Checking up!");
                         if (grid[x,y-1].renderer.material.mainTexture == grid[x,y-1].renderer.material.mainTexture) {
                             return false;
                         }
                     }
                     // Search for neighbor blocks going right
                     if (x > 0) {
                         Debug.Log("Checking to the right");
                         if (grid[x-1,y].renderer.material.mainTexture == grid[x-1,y].renderer.material.mainTexture) {
                             return false;
                         }
                     }
                     // Search for neighbor blocks going down
                     if (y < grid.GetLength(1)-1) {
                         Debug.Log("Checking down");
                         if (grid[x,y+1].renderer.material.mainTexture == grid[x,y+1].renderer.material.mainTexture) {
                             return false;
                         }
                     }
                     // Search for neighbor blocks going left
                     if (x < grid.GetLength (1)-1) {
                         Debug.Log("Checking left");
                         if (grid[x+1,y].renderer.material.mainTexture == grid[x+1,y].renderer.material.mainTexture) {
                             return false;
                         }
                     }
                 }
             }
             Debug.Log("NO NEIGHBORS LEFT");
             return true;
 
         }
         #endregion
avatar image mattyman174 · Mar 11, 2014 at 09:57 PM 0
Share
 if (grid[x+1,y].renderer.material.mainTexture == grid[x+1,y].renderer.material.mainTexture)

These statements dont seem to make sense. IF this blocks texture IS EQUAL TO the same blocks texture THEN do this

Also please provide more information on the Error you are now having.

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

21 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Trouble with Round Generating 0 Answers

how to check only once when a boolean is always true on update. it keeps looping 2 Answers

C# round transform.position floats to .5 2 Answers


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