Hi there,
I have a question related to C++ functions. If a boolean return type function contains too many returns in it, does it cause any problem ??
In my program one of the bool function contains too many return statements, in if conditions. And interesting thing is that it is not giving an error and the program is not crashing but boolean function returns 128 value instead of 1 or 0.
Any idea, why it is giving this unusual result ?? The method is as follows. Thanks !
bool pathClear(int currentRow, int inputRow, int currentCol, int inputCol)
{
pathIsClear = false;
string currentCellValue = board[currentRow][currentCol];
if(checkInput(currentRow, inputRow, currentCol, inputCol) == true)
{
//conditions for player-1
if(currentCellValue[0] == 'p' && currentCellValue[1] == '1')
{
if( (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_1[0] == true && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[0] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[0] == true;
return true;
}
else if(firstTimeMoveArr_1[0] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '2' && firstTimeMoveArr_1[1] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_1[1] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[1] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[1] == true;
return true;
}
else if(firstTimeMoveArr_1[1] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '3' && firstTimeMoveArr_1[2] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_1[2] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[2] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[2] == true;
return true;
}
else if(firstTimeMoveArr_1[2] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '4' && firstTimeMoveArr_1[3] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_1[3] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[3] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[3] == true;
return true;
}
else if(firstTimeMoveArr_1[3] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '5' && firstTimeMoveArr_1[4] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_1[4] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[4] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[4] == true;
return true;
}
else if(firstTimeMoveArr_1[4] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '6' && firstTimeMoveArr_1[5] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_1[5] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[5] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[5] == true;
return true;
}
else if(firstTimeMoveArr_1[5] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '7' && firstTimeMoveArr_1[6] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_1[6] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[6] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[6] == true;
return true;
}
else if(firstTimeMoveArr_1[6] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '8' && firstTimeMoveArr_1[7] == false && inputRow == currentRow+2 && inputCol == currentCol && occupyCell[inputRow-1][inputCol-1] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_1[0] == false && inputRow == currentRow+1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_1[7] == true && inputRow == currentRow+1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_1[7] == false)
{
pathIsClear = true;
firstTimeMoveArr_1[7] == true;
return true;
}
else if(firstTimeMoveArr_1[7] == true)
{
pathIsClear = true;
return true;
}
}
else
return false;
}
//conditions for player-2
else if(currentCellValue[0] == 'p' && currentCellValue[1] == '2')
{
if( (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '1' && firstTimeMoveArr_2[0] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[0] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[0] == true;
return true;
}
else if(firstTimeMoveArr_2[0] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '2' && firstTimeMoveArr_2[1] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '2' && firstTimeMoveArr_2[1] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[1] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[1] == true;
return true;
}
else if(firstTimeMoveArr_2[1] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '3' && firstTimeMoveArr_2[2] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '3' && firstTimeMoveArr_2[2] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[2] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[2] == true;
return true;
}
else if(firstTimeMoveArr_2[2] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '4' && firstTimeMoveArr_2[3] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '4' && firstTimeMoveArr_2[3] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[3] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[3] == true;
return true;
}
else if(firstTimeMoveArr_2[3] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '5' && firstTimeMoveArr_2[4] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '5' && firstTimeMoveArr_2[4] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[4] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[4] == true;
return true;
}
else if(firstTimeMoveArr_2[4] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '6' && firstTimeMoveArr_2[5] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '6' && firstTimeMoveArr_2[5] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[5] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[5] == true;
return true;
}
else if(firstTimeMoveArr_2[5] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '7' && firstTimeMoveArr_2[6] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '7' && firstTimeMoveArr_2[6] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[6] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[6] == true;
return true;
}
else if(firstTimeMoveArr_2[6] == true)
{
pathIsClear = true;
return true;
}
}
else if( (currentCellValue[5] == '8' && firstTimeMoveArr_2[7] == false && inputRow == currentRow-2 && inputCol == currentCol && occupyCell[inputRow+1][inputCol+1] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_2[0] == false && inputRow == currentRow-1 && inputCol == currentCol && occupyCell[inputRow][inputCol] == false) || (currentCellValue[5] == '8' && firstTimeMoveArr_2[7] == true && inputRow == currentRow-1 && occupyCell[inputRow][inputCol] == false) )
{
if(firstTimeMoveArr_2[7] == false)
{
pathIsClear = true;
firstTimeMoveArr_2[7] == true;
return true;
}
else if(firstTimeMoveArr_2[7] == true)
{
pathIsClear = true;
return true;
}
}
else
return false;
}
}
else
{
return false;
}
}