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).
-
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.
-
red round flat, third down from top was played.
-
white oval on the red round piece.
-
whie oval second from the top
-
red oval on very bottom played and called function.
-
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.