i have two codes:
This one works.
public class ChessInterface1{
public static void main(String[] arg){
boolean valid4=false;
int rowStart=8;
int columnStart=3;
int columnEnd =2;
int rowEnd=1;
int a=0;
int size=rowStart-rowEnd;
Math.abs(size);
for(int i=rowStart-1;i>=rowEnd;i--){
for(int j=columnStart-1;j>=columnEnd;j--){
while(columnStart>columnEnd && rowStart>rowEnd){
rowStart--;
columnStart--;
a++;
System.out.println("comparable variable is :"+a);
System.out.println("The column number is :"+j);
System.out.println("The row number is :"+i);
System.out.println("The size number is :"+size);
}
}
}
valid4 = !(a<size);
if(valid4==true){
System.out.println("true");
}
else{
System.out.println("false");
}
}
}
This one is a method: same code but the loop is wrapped in if:
public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
boolean valid1=false;
boolean valid2=false;
boolean valid3=false;
boolean valid4=false;
int size=rowStart-rowEnd;
if(size<0){
size=size*(-1);
}
int deltaX=columnEnd-columnStart;
deltaX=deltaX<0?-deltaX:deltaX;
int deltaY=rowEnd-rowStart;
deltaY=deltaY<0?-deltaY:deltaY;
int a=0;
//1--
if(deltaX<0 && deltaY<0){
for(int i=rowStart-1;i>=rowEnd;i--){
for(int j=columnStart-1;j>=columnEnd;j--){
while(pieces[i][j]==null && columnStart>columnEnd && rowStart>rowEnd){
rowStart--;
columnStart--;
a++;
}
}
}
valid1 = !(a<size);
}
return valid1
}
}
valid one is never returned as true. why is that.? why does the program sees the methodds local variable as false all the time/? is it cause it is wrapped in if method?