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?

Try debugging your code to see why the variable in question is set to the values it is by adding print outs to show where it is set and with what values. For example for this:
valid1 = !(a<size);
add just before that statement printout of ("a=" + a + ", size=" + size + ", valid1=" + valid1);

Lines 14 and 16 guarantee that the deltas will both be >= 0, so
if(deltaX<0 && deltaY<0) will always be false

Another argument for adding print outs of ALL the variables in your code to see what values they have as the code is executed.

Yes Norm, you're right. I going to be a lot firmer on this next time.
NewOrder: you heard it here: no more free debugging of code unless you have already tried to debug it with relevant printouts. Leave the print statements in your code so we can see what you have tried.

Norm, i must get done with this project, as soon as i get the answer i will mark this thread as solved.


james. i posted 2 codes. the second code (first post) is the debugging. look carefully ,


Lines 14 and 16 guarantee that the deltas will both be >= 0, so
if(deltaX<0 && deltaY<0) will always be false

how do i correct it. should i remove those lines altogether?
i thought

#
deltaX=deltaX<0?-deltaX:deltaX;
#

#
deltaY=deltaY<0?-deltaY:deltaY;

Wouldn't lines 14 and 16 be clearer if you used Math.abs() instead of the home-made version?

in line 33,

valid1 = !(a<size);

why do you put it this way instead of

valid1 = a>=size;

which is easier to read?

as soon as i get the answer

One way to get the answer is for YOU to debug YOUR code to see why it is not doing what you want it to do.

public  boolean isKingThreatened(ChessPiece[][]pieces,int columnStart ,int columnEnd,int rowEnd, int kingRow, int kingColumn) {
		

	
	  valid = false;
		
		for(int i=rowEnd;i<pieces.length;i++)
			for(int j=columnEnd;j<pieces[i].length;j++)
				
		   if(pieces[i][j]!=null &&  pieces[i][j]==pieces[kingColumn][kingRow])
			   valid=true;
		   else
			   valid=false;
			  	
		return valid;
		
	}


  
	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;
		
			int deltaY=rowEnd-rowStart;
			
                         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--){
							if(pieces[i][j]==null && columnStart>columnEnd && rowStart>rowEnd){						
							   rowStart--;
                               columnStart--;    
								a++;
                                                         
							  }
		                }
				}
			
							
							
			}					
			valid1 = !(a<size); // this works, but the rest dont (valid2,3,4)
			//2++
			if (deltaX>0 && deltaY>0){
				

					
				for(int i=rowStart;i<rowEnd;i++){
					for(int j=columnStart;j<columnEnd;j++){
							if(pieces[i][j]==null && columnStart<columnEnd && rowStart<rowEnd){						
							rowStart++;
                            columnStart++;  
								a++;
							}
								
								
					}
				}
				
			}
                    valid2 = !(a<size); 
  
			//3-+
	       if(deltaX>0 && deltaY<0){
	   		
			for(int i=rowStart-1;i>=rowEnd;i--){
				for(int j=columnStart;j<columnEnd;j++){
						if(pieces[i][j]==null && columnStart<columnEnd && rowStart>rowEnd){						
						rowStart--;
                         columnStart++;  
							a++;
						}
						
						
							
				}
			}
		}
                    valid3 = !(a<size);


			//4+-
			if (deltaX<0 && deltaY>0){
				
				
				for(int i=rowStart-1;i<rowEnd;i++){
					for(int j=columnStart;j>=columnEnd;j--){
							if(pieces[i][j]==null && columnStart>columnEnd && rowStart<rowEnd){						
							rowStart++;
                            columnStart--;  
								a++;
							}
							
								
					}
				}
				
			}
			
			valid4 = !(a<size);
			
			
			
			
			
			

		

	
		return valid1 || valid2 || valid3 || valid4;
	



        }

}

i followed what james said. the first part works. the others dont? why is that if i copied and pasted the first part to other parts and did a few modifications with the minus signs?!?


*Norm1, i did debugg it,. if you want me to debug it in eclipse, teach me, cause i dont know how

Why don't the other parts work? Have you tried debugging the code to see where the execution flow goes and what the variable values are?
You need to do that to understand what the problem with your code is.

Sorry, I know nothing about how to use your IDE.

*Norm1, i did debugg it,. if you want me to debug it in eclipse, teach me, cause i dont know how

If you've debugged it, then it's working, and you're done. If you still have bugs, you need to debug it. Do you mean you ran a debugger on it? A debugger is a tool that you use to isolate bugs and fix them, it won't fix them on its own!

A program is data flowing through a machine. Your bugs are now all centered on the fact that the data flowing through your machine is not representing the problem that you want to represent. To find why this is happening, you have to know what the data ought to be at any point in the program, and you have to know what it actually is. Norm is assuming you know what the data should be at each step in the process, and so he's suggesting that you need to look at what it actually is.
If he's right in the first part, then you need to follow his advice or you'll never finish this. If he's wrong, and you don't know what you want the data to be at each step of the code, then you need to follow my advice and break things down until you can follow your own code.

In no case will you get much more than these lectures unless you start taking some advice from people who know how to do this. We're not telling you this stuff to torment you, my friend. This is just how you fix your problem.

commented: Well put +2

listen, no way eclipse will give me an answer. it is about logic which isnt working along the way. and i am trying to test every piece of code to see why.
i think the problem is in one of the variables i defined above. the loops seem okay, cause they do work, when i copy and paste and check them seperately


Here is how i debugg the code and it is okay for now.

copy and paste this into a notepad and run it. you will see that the loop work. they all work in the same way.. but when i put them in the method the first one only moves the bishop :(

public class ChessInterface1{
	public static void main(String[] arg){
		
		boolean valid4=false;
	
				  
int rowStart=7;
int columnStart=2;
int columnEnd =3;
int rowEnd=8;
	int a=0;
	int size=rowStart-rowEnd;
	 
	 Math.abs(size);
      if(0<rowStart){
    		for(int i=rowStart;i<rowEnd;i++){
				for(int j=columnStart;j<columnEnd;j++){
						while( columnStart<columnEnd && rowStart<rowEnd){	
							
						rowStart++;
                        columnStart++;  
							a++;
							System.out.println("This is size "+size);
							System.out.println("This is row "+i);
							System.out.println("This is column "+j);
							System.out.println("This is a "+a);
						}
							
							
				}
			}
			
		}
				valid4 = !(a<size);
	            if(valid4==true){
	            	System.out.println("true");
	            	
	            }
	            else{
	            	System.out.println("false");
	            }
	




}

}

if(0<rowStart){

Can you explain what the purpose of this statement is?

In general your code has NO documenting comments. You have lots of logic without any comments saying what you want the logic to do. If you mistype a compare and say use a < vs a > there is NO way any one reading the code can tell what you intended and then see that the code is incorrect.

i build it as a simulation.
in my original code
it was something like

if(deltaX>0 && deltaY>0)

i just simulated what i have as my real code.
norm. i checked all hte signs plenty of times.

i thought i was clear with the variable names i chose.(apart from deltaX & size).
it simply checks squares where there is null, and counts each time when there is null.
if the number of moves between starting and ending position is 3.
then i need to count those positions and find out if there are 3 nulls. thats what the loops do..
a++
is the counter
if it counted 3, then valid is true. if less then false.
the first set of loops works, the others dont, thats what puzzles me. i check all the 3 seperately, and they do work. you could copy the code above and play with the minus signs. you see that they give a true answer.

That all means that somewhere in the method doesnt work

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; // whats the distance of movement?
                         if(size<0){
                         size=size*(-1);
                          }
                           
			int deltaX=columnEnd-columnStart;// spaces to move (column) determines the direction, up, down, right left
		
			int deltaY=rowEnd-rowStart;
			
                         int a=0;
						 
    
			//1--
			if(deltaX<0 && deltaY<0){// if the direction is up left
					
				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);
			
			
			//2++
			if (deltaX>0 && deltaY>0){
				

					
				for(int i=rowStart;i<rowEnd;i++){
					for(int j=columnStart;j<columnEnd;j++){
							while(pieces[i][j]==null && columnStart<columnEnd && rowStart<rowEnd){						
							rowStart++;
                            columnStart++;  
								a++;
							}
								
								
					}
				}
				
			}
                    valid2 = !(a<size); 
					
  
			//3-+
	       if(deltaX>0 && deltaY<0){// direction left downwards
	   		
			for(int i=rowStart-1;i>=rowEnd;i--){
				for(int j=columnStart;j<columnEnd;j++){
						while(pieces[i][j]==null && columnStart<columnEnd && rowStart>rowEnd){						
						rowStart--;
                         columnStart++;  
							a++;
						}
						
						
							
				}
			}
		}
                    valid3 = !(a<size);

			//4+-
			if (deltaX<0 && deltaY>0){
				
				
				for(int i=rowStart-1;i<rowEnd;i++){
					for(int j=columnStart;j>=columnEnd;j--){
							while(pieces[i][j]==null && columnStart>columnEnd && rowStart<rowEnd){						
							rowStart++;
                            columnStart--;  
								a++;
							}
							
								
					}
				}
				
			}
			
			valid4 = !(a<size);
			
			
			
			
			
			

		

	
		return valid1 || valid2 || valid3 || valid4;
	



        }

}

Posting comments in the posts in this thread doesn't make the code any easier to understand the next time you post the code because the comments are not in the code.

For example. I don't understand the purpose of this statement:
valid4 = !(a<size);

Don't bother to explain in a post. The explanation should be in the source code.

remember when i used

if(a<size){
valid true
else
false

it is the same thing. i just shortened to one line.

i am going to make the same technique with the rook (this time it is going to be easier to understand)
and post it in 20 or so mins

here is the rooks code:
principles are all the same as with the bishop. but that is an easier code:

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)
				  	  
			  for(j=columnStart;j<columnEnd;j++){
					
			           if(pieces[rowStart][j]==null && pieces[rowStart][j]!=pieces[rowEnd][columnEnd]){// CHECK EACH SQUARE IF IT IS NULL
				       b++; // IF NULL , DO B+1
				 
		             }
		}			
	}		  
		valid5 = !(b<SpacesY); // 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)
						  	  
					  for(j=columnStart-1;j<columnEnd;j--){
							  
						    if(pieces[rowStart][j]==null && pieces[rowStart][j]!=pieces[rowEnd][columnEnd]){
						     b++;
						 
				   
				}			
			}	
		 Math.abs(SpacesY);		// TURNS MY SPACE FIGURE FROM NEGATIVE TO POSITIVE AND COMPARES WITH B  
		valid6 = !(b<SpacesY);		  
					  
					  
		   if(SpacesY==0 & SpacesX>0) // DOWN SIDE (CHECK FOR NULL)
						for( i=rowStart;i<columnEnd;i++)
						{		  	  
							 
								
						       if(pieces[i][columnStart]==null && pieces[i][columnStart]!=pieces[rowEnd][columnEnd]){ // TESTING THE NUMBER OF NULLS AT THE DOWNWARD ROW.
							   b++;
								 
						   
						}			
					}	
		   valid7 = !(b<SpacesX);		  
							  
							  
							  
							  
				if(SpacesY==0 & SpacesX<0) // UP SIDE (CHECK FOR NULL)			  
								for( i=rowStart-1;i<columnEnd;i--)
								{		  	  
									 
											
						           if(pieces[i][columnStart]==null && pieces[i][columnStart]!=pieces[rowEnd][columnEnd]){ // CHECKS THE UPPER ROW.
								   b++;
										 
								   
								}			
							}	
				 Math.abs(SpacesY);		// TURNS MY SPACE FIGURE FROM NEGATIVE TO POSITIVE AND COMPARES WITH B  
					valid8 = !(b<SpacesX);
					
		return valid5 || valid6 || valid7 || valid8;
	}		
}

i am going to check it now. if i have no mistake. i hope i have no mistakes

remember when i used

if(a<size){
valid true
else
false

it is the same thing. i just shortened to one line.

Norm was not asking what it does. He was asking what it means. What does this represent in your calculations?

Math.abs(SpacesY); // TURNS MY SPACE FIGURE FROM NEGATIVE TO POSITIVE AND COMPARES WITH B
valid8 = !(b<SpacesX);

Here is an example of a completely redundant comment.

The abs method returns a positive value
(b<SpaceX) compares b with SpaceX

The comments don't tell any one why you need a positive number
or why you want to compare it with another number.

the code is not perfect, i am shortening and correcting it now. i just made it quickly to show you the idea.
here is an easy representation.
i have an array of 3 objects:
array[2]
0=[null] 1=[null] 2=[object]

now i dont know where the object is. so what i am trying to do is check it. how do i do it? by running a loop

for(int i=0;i<2;i++){
array
}

now i havent got a condition.

what i did told the program to count the number of times it counters the null in the array. so i did this
( i shortened now the whole mess that i did. and for simplicity i did something like this. array difference , all same)

while(array==null & array<array[3]){// so not to hit the limit of the array
i++
}

this what will happen. the loop will go through:
0=[null]
1=[null]
2= oops an, object, i must stop the count

whats the count? 2.


now if we look at the chess example. i need to get from a postion to position. suppose i want to get to array[2] from array[0]

now i take the distance from the start to finish array[0] to 2 = 3 (0,1,2 = 3 positions in the array)

in the example, i reached 2 only , cause there was an object.

now i need to set a boolean variable and an if statement

if(b<distance)
valid=false;
else
valid=true;

return valid (which should be false)

what it means is that i cant reach position 3 cause it occupies an object.

that is the whole logic and that is what i am trying to do.

if you have questions. ask something specific

if you have questions. ask something specific

Nothing from me.

ideally something like that. thats what i am trying to do"

public class ChessInterface1{
 public static void main(String[] args){	
	int b=0;
	int columnStart=4;
	int columnEnd=2;
	int SpacesX=columnStart-columnEnd;
	int array[]=new int[5];
	array[0]=1;
	array[1]=2;
	array[2]=0;
	array[3]=0;
	array[4]=5;
	boolean valid7=false;
	
	while(array[4-b]!=0 && columnStart>columnEnd){
	     b++;
	     columnStart--; 
	     System.out.println(columnEnd);
	     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);
	}
}

output:

2
1
The value is false
The gap between positions is :2
The array spaces counted in the loop are :1

here is a demo of how it should work. if you run it, you will find out..
but in practice it is more difficult, cause java wont listen :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.