i have this method , which returns true no matter what numbers i feed into it:
public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
int j=columnStart;
int i=rowStart;
boolean valid5=false;
boolean valid6=false;
boolean valid7=false;
boolean valid8=false;
int b=0;
int SpacesX=rowEnd-rowStart;
int SpacesY=columnEnd-columnStart;
if(SpacesY>0 & SpacesX==0){ // RIGHT SIDE (CHECK FOR NULL)
while(pieces[rowStart][columnStart+b]==null && columnStart<columnEnd){
b++;
columnStart++;
}
if(b<SpacesY){
valid5=false;
}
else{
valid5=true;
} // IF B SMALLER THAN THE DIFFERENCE BETWEEN COLUMNSTART & COLUMNEND (SPACESY) , GIVE FALSE. OTHERWISE GIVE TRUE.
}
if(SpacesY<0 & SpacesX==0){ // LEFT SIDE (CHECK FOR NULL)
while(pieces[rowStart][columnStart-b]==null && columnStart>columnEnd){
b++;
columnStart--;
}
if(b<SpacesY){
valid6=false;
}
else{
valid6=true;
}
}
if(SpacesY==0 & SpacesX>0){ // DOWN SIDE (CHECK FOR NULL)
while(pieces[i+b][columnStart]==null && rowStart<rowEnd){ // CHECKS THE down ROW.
b++;
rowStart++;
}
if(b<SpacesX){
valid7=false;
}
else{
valid7=true;
}
}
if(SpacesY==0 & SpacesX<0){ // UP SIDE (CHECK FOR NULL)
while(pieces[rowStart-b][columnStart]==null && rowStart>rowEnd){ // CHECKS THE UPPER ROW.
b++;
rowStart--;
}
if(b<SpacesX){
valid8=false;
}
else{
valid8=true;
}
}
return valid5 || valid6 || valid7 || valid8;
}
}
it returns allways true. how come?
the arguments are different.. each type, i decide on what they will be . the objects are fixed. the objects include chess pieces 16*2
that returns true.
i debugged it , by writing a similar code with print statements and it works. while the other doesnt!!
public class ChessInterface1{
public static void main(String[] args){
int b=0;
int columnStart=5;
int columnEnd=2;
int SpacesX=columnStart-columnEnd;
int array[]=new int[5];
array[0]=1;
array[1]=0;
array[2]=3;
array[3]=4;
array[4]=5;
boolean valid7=false;
if(columnStart>columnEnd){
while(array[4-b]!=0 && columnStart>columnEnd){
// CHECKS THE UPPER ROW.
b++;
columnStart--;
System.out.println(array[4-b]);
System.out.println(b);
}
}
if(b<SpacesX){
valid7=false;
System.out.println("The value is false");
}
else{
valid7=true;
System.out.println("The value is true");
}
System.out.println("The gap between positions is :"+SpacesX);
System.out.println("The array spaces counted in the loop are :"+b);
}
}
what happens. is my ifs condition get penetrated. i cant conceptually understand. why the while loop doesnt stop when there is a non null object.
could you look simple at the structure of the code. cause i think i have a few variables that are too exposed or too hidden. but which ones/