Order of operation; if statement; for loop

Hi,

This is a question on order of operations.

I am still pretty new to coding. I am working on creating a board type game. They lay out is a grid system(an array) like checkers or chess, but with a third element, so (x,y,z) not just (x,y) to find, use, and access game pieces and their positioning.

I have created a function named “Logictest_2” to find game pieces and thier colors in a -z direction; south to north or bottom to top direction (according to my array) of the game piece played. The way the code seems like it should execute to me is…

Line # 9 (if(playedpiece.boardspotz >= 1)) should run if the condition is found true.

Line # 11-23 (for loop) should run, and if a piece/pieces are found

Line # 24 (if(iftest == true)) should run and inturn run

Line # 26 (switch case). Now lets say (case Color.White:) was ran and

Line # 30 (if(redfound != true || whitefound == true) ran.

My Debug.Log should read

"# " + i + “for LOOP PLAYED!!!”

“SWITCH CASE WHITE PLAYED WITH REDFOUND != TRUE”

in that order. But this is not the case. Here is my code.

public void Logictest_2(ControllerScript.BoardGame gamepiece)
	{
		ControllerScript.BoardGame playedpiece = gamepiece;
		ControllerScript.BoardGame newpiece = playedpiece;
		int amountinlist;
		piecelist.Add(playedpiece);
		bool iftest = false;

		if(playedpiece.boardspotz >= 1)
		{
			for(int i = 0; i < 3; i++)
			{
				if(controllerscript.thegameboard[i, playedpiece.boardspoty, playedpiece.boardspotz -1].peice == true)
				{
					newpiece = controllerscript.thegameboard[i, playedpiece.boardspoty, playedpiece.boardspotz -1];
					iftest = true;
					Debug.Log("# " + i + "for LOOP PLAYED!!!!!");
				}
				else
				{
					break;
				}
			}
			if(iftest == true)
			{
				switch (newpiece.color)
				{
				case Color.White:
					whitefound = true;
					if(redfound != true || whitefound == true)
					{
						amountinlist = piecelist.Count;
						whitefound = true;
						Logictest_2(newpiece);
						Debug.Log("SWITCH CASE WHITE PLAYED WITH REDFOUND != TRUE");
					}
					if(redfound == true)
					{
						Debug.Log("WHITE PIECE FOUND AFTER RED FOUND");
						// CALL COLOR CHANGING FUNCTION!!!!!!!
					}
						break;
				case Color.Red:
					redfound = true;
					amountinlist = piecelist.Count;
					if(whitefound == true)
					{
						piecelist.Add(newpiece);
					    // CALL COLOR CHANGING FUNCTION!!!!!!!
						Debug.Log("THIS WOULD OF CHANGED ALL THE WHITES TO RED");
					}
					else if(redfound == true)
					{
						Logictest_2(newpiece);
						Debug.Log("SWITCH CASE RED PLAYED");

					}
					break;
				default:

					break;
				}
			}

		}
		else if(redfound == true)
		{
			// CALL COLOR CHANGING FUNCTION!!!!!
			Debug.Log("THIS WAS PLAYED BECAUSE NO MORE SPOTS AVAILABLE TO CHECK AND RED WAS CALLED");
		}
		else
		{
			//CLEAR THE LIST!!!!!!
			Debug.Log("CLEAR THE LIST CALLED");
		}

	}

According to the the console window the Debug.Log shows that the (for loop) runs for however many pieces are found in the -z direction(3 in my example) and then runs the (switch case), but on top of that it seems to run the (switch case) in reverse to the pieces that the (for loop) found. Below in the image is a snippet of a game play I tested and the Debug.Log associated with it. Note, the only game pieces that call this function are the red oval ones. The order of play was(as seen in the image).

  1. red oval at top was played first to test the

    else
    {
    //CLEAR THE LIST!!!
    Debug.Log(“CLEAR THE LIST CALLED”);
    }

part of code. Witch ran perfect.

  1. red round flat, third down from top was played.

  2. white oval on the red round piece.

  3. whie oval second from the top

  4. red oval on very bottom played and called function.

  5. red oval on very bottom played and called function.

Now how I read the Debug.Log the (for loop) ran correctly on the first pieces shown by the # of loops ran. But then it ran again and then again.
Next the (switch case) ran but in reverse the the pieces found. The Debug.Log “WHITE PIECE FOUND AFTER RED FOUND” should never have ran at all.
Can someone please explain to me the order of operation happening here? I am completely confused. What or why it is happening.
Now i’m sure I could circumvent this problem with breaking the code down into few more functions but then I wouldn’t lean what is really happening here.

Thank you in advance for any info, advice and or answers.

Your first problem is at line 19: the else statement (with it’s break) should not be there. If the first if-test fails, you say to break out of the loop. “iftest” can never be set to true if the first space you test has “.piece” is false.

Second, I don’t think the “redfound” will work like you expect either. Your top level statements say:

if(playedpiece.boardspotz >= 1)
{
}
else if(redfound == true)
{
}
else
{
}

You probably want to put the “redfound” check at the end of your first if-statement, like

if(playedpiece.boardspotz >= 1)
{
  ... other code ...
  if( redfound )
  {
  }
  else
  {
  }
}

[Side note: The inside of an if-statement needs to result in a boolean value. For example, if you have integers, you need to compare them to create a boolean value (like “value >= 1”). Since “redfound” is already a boolean value, you can simply use “if( redfound )”. In the future, if you only want the if-condition to execute when the value is false, you can use “if( !redfound )”. The “!” means “not”.]

Thank you for your response William, especially the side note about the boolean. Turns out my function runs pretty much as I wanted it to with a few exceptions to the double if-statements in the switch-cases. The main problem was at lines 34,35 and 54,55. I had the Debug.Log line after calling the function again, so of course it would not log the Debug.Log. As far as the if-statements in the switch-case lines 37-41 should be moved above the first if-statement(lines 30-36). In addition to that, each if-statement needs a “break;” in them at the end for the way I want it to work.

For as many times as I have learned it over and over I still haven’t learned it. For you newer people to coding if there’s one thing id like to say I’ve learned it’s PRIORITY, PRIORITY, PRIORITY! I thought I had learned this when I wrote my first game. But apparently I still need to learn.