Hi ,help needed in bishop movement c++..the code is woking fine giving desired output most of time but there is one thing unintentional happening in the algorithm..
to understand the problem plz see the diagram (ignore the 'oo' and 'o' and consider them double and single spaces respectively, they are just to keep diagram structure intact otherwise more than one spaces dont show up in forums)


oo|oo|oo|oA
------------------
oo|oo|oA|oB
------------------
oo|o◘|oo|oo
------------------
oo|oo|oo|oo

the black square(◘) is bishop ..according to chess rule it can move to location of either of the As..in my case it is perfeclty moving to A but problem is that it is also moving towards B which i dont want

below is the code of primary diagonal ...sr stands for source row ,sc for source column ,dr for destination row and dc for destination column

bool validBishop(char board[][8],int inputs[])
{
	 
	int  sr=inputs[0] , sc=inputs[1] , dr=inputs[2] , dc=inputs[3];
	
	
		bool ok= false;
       
		if(sr>dr && sc<dc)
                { 
			
		    for( int i=sr-1, j=sc+1;   i>=h,j<=k;    ) 
		    {
			   if(board[i][j] == ' ') 
			   {
				  ok1=true;
				  i--;
				  j++;
			   }
			else
			   {
				  ok=false;
				  break;
			   }
		   }

			if(ok==true)
			{
			  return true;
			}else
			{
			  return false;
       			}
               }
}

Edited 5 Years Ago by learner guy: n/a

As it is, all that you are checking is whether the region between the source and the destination is clear; you never check to see if the destination is actually on that diagonal.

bool validBishop(char board[][8],int inputs[])
{
    int  sr=inputs[0] , sc=inputs[1] , dr=inputs[2] , dc=inputs[3];
    int i, j;	
    bool clear = true;
       
    if(sr>dr && sc<dc)
    { 
        for( i=sr-1, j=sc+1; i>=h,j<=k; i--, j++ ) 
        {
            if(board[i][j] != ' ') 
            {
                clear = false;
                break;
            }
        }
    }

    return (clear && (i == dr) && (j == dc));
}

Edited 5 Years Ago by Schol-R-LEA: n/a

I just noticed something in the for() conditional:

for( i=sr-1, j=sc+1; i>=h,j<=k; i--, j++ )

First off, where are h and k coming from? They aren't declared in the function and they aren't arguments, so I assume that they are globals. What do they represent, the sizes of the chessboard? If so, then it will always check to the edge of the board, even if the intended destination is only partway across the board.

Second (and I really should have noticed this before), using the comma operator in the conditional (as opposed to the initializer or the increment) essentially ignores the second part of the conditional. What you want is

for( i=sr-1, j=sc+1; i>=h && j<=k; i--, j++ )

While this probably doesn't impact the problem you are posting about, it could be the source of other problems.

This article has been dead for over six months. Start a new discussion instead.