- Home /
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
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.
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?
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.
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?
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.
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.
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
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.
Your answer
Follow this Question
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