Code not running past nested if to else

Hello, I've made a nested If statement to validate game piece movement. I've confirmed the nested ifs by themselves work perfectly, but I'm also trying to make an else statement after that for invalid moves (if valid move -> return true, else return false). The problem is it doesn't seem to run past that nested statement. I've tried testing it with basic couts just to see if it would output anything but I'm getting not getting my "invalid move" text or "outside of nested if" text. Any ideas?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
bool FBoard::moveO(int yAxisIn, int xAxisIn, int yAxisIn2, int xAxisIn2)
{
	if (gameState == UNFINISHED) //Game must still be in progress
	{
		if (gameBoard[yAxisIn][xAxisIn] == 'o') //must contain "o" piece
		{
			if ((xAxisIn == (xAxisIn2+1)) || (xAxisIn == (xAxisIn2-1))) //one space movement only
			{
				if (yAxisIn2 == (yAxisIn-1)) //row must decrease by 1
				{

					if ((yAxisIn2<8) && (xAxisIn2<8)) //array bounds checking
					{
						if (gameBoard[yAxisIn2][xAxisIn2] == '_') //unoccupied squares only
						{	
							//move "o" piece
							gameBoard[yAxisIn2][xAxisIn2] = 'o';
							
							//erase "o" piece from previous square
							gameBoard[yAxisIn][xAxisIn] = '_'; 		

							return true;
						}
					}
				}
			}
		}
	}
	else
	{	
		std::cout << "invalid move" << std::endl;	
		return false;
	}

	std::cout << "outside of nested ifs" << std::endl;
}
Last edited on
Like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
bool FBoard::moveO(int yAxisIn, int xAxisIn, int yAxisIn2, int xAxisIn2)
{
	bool result = false;
	if (gameState == UNFINISHED) //Game must still be in progress
	{
		if (gameBoard[yAxisIn][xAxisIn] == 'o') //must contain "o" piece
		{
			if ((xAxisIn == (xAxisIn2+1)) || (xAxisIn == (xAxisIn2-1))) //one space movement only
			{
				if (yAxisIn2 == (yAxisIn-1)) //row must decrease by 1
				{

					if ((yAxisIn2<8) && (xAxisIn2<8)) //array bounds checking
					{
						if (gameBoard[yAxisIn2][xAxisIn2] == '_') //unoccupied squares only
						{	
							//move "o" piece
							gameBoard[yAxisIn2][xAxisIn2] = 'o';
							
							//erase "o" piece from previous square
							gameBoard[yAxisIn][xAxisIn] = '_'; 		

							result = true;
						}
					}
				}
			}
		}
	}

	std::cout << "outside of nested ifs, result=" << result << std::endl;
	return result;
}

Thanks so much! I didn't even think of doing it that way!
I find lots of disagreement with Linus Torvalds, though one can't argue with Linux as proof, and I have nothing to compare. Yet one thing he offers I do agree with is this paraphrase: "If there's more than 3 nested brackets in your code, you need to fix your code".

He's more generally talking about functions vs inline code for clarity, but here's where it may well be something to think about, because this situation is what && is for within an if clause.

Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

if (   gameState == UNFINISHED                                  //Game must still be in progress
    && gameBoard[yAxisIn][xAxisIn] == 'o'                       //must contain "o" piece
    && ((xAxisIn == (xAxisIn2+1)) || (xAxisIn == (xAxisIn2-1))) //one space movement only
    && yAxisIn2 == (yAxisIn-1)                                  //row must decrease by 1
    && (yAxisIn2<8) && (xAxisIn2<8)                             //array bounds checking
    && gameBoard[yAxisIn2][xAxisIn2] == '_'                     //unoccupied squares only
   ) 
   {   
       //move "o" piece
       gameBoard[yAxisIn2][xAxisIn2] = 'o';
       
       //erase "o" piece from previous square
       gameBoard[yAxisIn][xAxisIn] = '_';      

       return true;  // or set a value to true
   }


I'm not checking the nature of these tests, but merely illustrating what it looks like to apply what amounts to the very same logic in a different way.

The other approach is to move all of those clauses into a "check" function which returns a bool, and where each test is inverted so each one returns early with false if they fail, ultimately returning true if the all pass.
Last edited on
Topic archived. No new replies allowed.