#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Variable visibility, how to overcome?


    i have this method. it doesnt matter what the method does.
    (it checks chess piece move validity)

    here it is:



    Code:
    	public boolean piecesCollision(ChessPiece[][]pieces,int rowStart,int columnStart ,int columnEnd,int rowEnd) {
    		
    		  boolean valid1=false;// this is visible and it affect the return
    		  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;
    			//1--
    			if(deltaX<0 && deltaY<0){
    				int a=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++;
                                                             
    							}
    		                          }
    				}
    			
    							if(a==size){
    								valid1=true; those variables arent visible for some reason.
    							}
    							else{
    								valid1=false;
    							}
    							
    			}					
    			
    			//2++
    			if (deltaX>0 && deltaY>0){
    				
    				int a=0;	
    				for(int i=rowStart;i<rowEnd;i++){
    					for(int j=columnStart;j<columnEnd;j++){
    							while(pieces[i][j]==null){						
    							
    								a++;
    							}
    							if(a<size){
    								valid2=false;
    							}
    							else{
    								valid2=true;
    							}
    							
    								
    					}
    				}
    				
    			}
    			//3-+
    	       if(deltaX>0 && deltaY<0){
    	   		int a=0;	
    			for(int i=rowStart;i>rowEnd;i--){
    				for(int j=columnStart;j<columnEnd;j++){
    						while(pieces[i][j]==null ){						
    						
    							a++;
    						}
    						if(a<size){
    							valid3=false;
    						}
    						else{
    							valid3=true;
    						}
    						
    							
    				}
    			}
    			}
    			//4+-
    			if (deltaX<0 && deltaY>0){
    				
    				int a=0;	
    				for(int i=rowStart;i<rowEnd;i++){
    					for(int j=columnStart;j>columnEnd;j--){
    							while(pieces[i][j]==null){						
    							
    								a++;
    							}
    							if(a<size){
    								valid4=false;
    							}
    							else{
    								valid4=true;
    							}
    							
    								
    					}
    				}
    				
    			}
    			
    			
    			
    			
    			
    			
    			
    			
    
    		
    
    	
    		return valid1 || valid2 || valid3 || valid4;// only sees the variables that were first defined as false
    	}
    
    
    
    
    
    
    }
    when i delete the return variables eclipse tells me that the variables that were first defined (boolean valid1=false arent being used.
    but they do, since i use them inside the loops. what i think happens is by the time the method finishes, the variables (valid1,valid2,valid3...) arent reached to return. and therefore it is a problem. how do you overcome this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Leuven (Belgium)
    Posts
    168
    Rep Power
    273
    first: you shouldn't be doing things like this
    Code:
    if(a<size){valid2=false;}else{valid2=true;}
    it's kind of redundant since you can just as well do
    Code:
    valid2 = !(a<size)
    Regarding your question: assigning a value to a variable is not considered as using the variable by eclipse. So it gives a warning.

    Comments on this post

    • jzd agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by HeadOnAPlate
    first: you shouldn't be doing things like this
    Code:
    if(a<size){valid2=false;}else{valid2=true;}
    it's kind of redundant since you can just as well do
    Code:
    valid2 = !(a<size)
    Regarding your question: assigning a value to a variable is not considered as using the variable by eclipse. So it gives a warning.

    okay. but when valid is true. it doesnt return true.
    it goes by the first variables assigned. e.g. boolean valid=false.
    no matter if valid changes to valid=true later.

    the algorithm works.
    you could try it. seperate class: only a test:

    Code:
     public class ChessInterface1{ 	public static void main(String[] arg){ 		 		 	 				   int rowStart=8; int columnStart=3; int columnEnd =2; int rowEnd=7; 	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);  							} 		              } 				} 				 	 	if(a==size){ 		System.out.println("true"); 	} 	else{ 		System.out.println("false"); 	} 	} }
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2007
    Posts
    1,939
    Rep Power
    3122
    NewOrder,

    1. Please use correct capitalization and punctuation when you post so that others can easily read it.
    2. Your code is in a CODE tag but is not formatted at all. Use the preview button before your post to verify that your post looks like you think it will.
    3. I don't really understand post #3, both of HeadOnAPlate's points are valid but neither seem to be related to your statement about the algorithm or returning values.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by jzd
    NewOrder,

    1. Please use correct capitalization and punctuation when you post so that others can easily read it.
    2. Your code is in a CODE tag but is not formatted at all. Use the preview button before your post to verify that your post looks like you think it will.
    3. I don't really understand post #3, both of HeadOnAPlate's points are valid but neither seem to be related to your statement about the algorithm or returning values.



    Code:
    	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);			
    							
    			}					
    			
    			//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){						
    							
    			a++;
    							}
    								
    								
    					}
    				}
    				
    			}
                        valid2 = !(a<size);   
    			//3-+
    	       if(deltaX>0 && deltaY<0){
    	   		
    	for(int i=rowStart;i>rowEnd;i--){
    		for(int j=columnStart;j<columnEnd;j++){
    	while(pieces[i][j]==null ){						
    						
    			a++;
    						}
    						
    						
    							
    				}
    			}
    		}
                        valid3 = !(a<size);
    
    
    			//4+-
    			if (deltaX<0 && deltaY>0){
    				
    				
    		for(int i=rowStart;i<rowEnd;i++){
    		for(int j=columnStart;j>columnEnd;j--){
    	while(pieces[i][j]==null){						
    							
    				a++;
    							}
    							
    								
    					}
    				}
    				
    			}
    			
    			valid4 = !(a<size);
    			
    			
    			
    			
    			
    			
    
    		
    
    	
    		return valid1 || valid2 || valid3 || valid4;
    	
    
    
    
    }
    
    }
    the return valid1. wont let me return what is near the loop
    e.g. valid4 = !(a<size);
    it goes straight to :
    boolean valid4=false;

    how can i make my variables (including variable a) visible. so everything will be updated and saved and feed inside return, (in this case valid4)



    This code :


    Code:
    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");
    	            }
    	}
    }
    works!

    it is similar to the first one. the only difference is that the loops of the first code are wrapped in if!!
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    This problem is also cross posted at http://www.daniweb.com/forums/thread311282.html
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Leuven (Belgium)
    Posts
    168
    Rep Power
    273
    Are you sure your code is doing what it should be doing?

    As far as I can tell only case 2,
    Code:
    //2++ 	if (deltaX>0 && deltaY>0){
    will ever be executed because of this code fragment:
    Code:
    int deltaX=columnEnd-columnStart;
    deltaX=deltaX<0?-deltaX:deltaX;
    int deltaY=rowEnd-rowStart;
    deltaY=deltaY<0?-deltaY:deltaY;
    If either deltaX or deltaY is equal to 0, you will always return false.

    Have you used a debugger to step through the algorithm while the program is running to see if it returns the expected values? As an alternative you could also write some unit tests to validate the programming logic.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by HeadOnAPlate
    Are you sure your code is doing what it should be doing?

    As far as I can tell only case 2,
    Code:
    //2++ 	if (deltaX>0 && deltaY>0){
    will ever be executed because of this code fragment:
    Code:
    int deltaX=columnEnd-columnStart;
    deltaX=deltaX<0?-deltaX:deltaX;
    int deltaY=rowEnd-rowStart;
    deltaY=deltaY<0?-deltaY:deltaY;
    If either deltaX or deltaY is equal to 0, you will always return false.

    Have you used a debugger to step through the algorithm while the program is running to see if it returns the expected values? As an alternative you could also write some unit tests to validate the programming logic.
    headpoint. what do you mean. can you expand.
    the ifs including the deltaY and X greater or smaller than 0, are responsible for deciding what diagnal side the bishop will go.
    i checked it and it should work.

    i have eclipse as a debugging mechanism. i have no idea how to use it with my code. i did already debugging , as i showed you. the exact same code works perfectly, if you put it in a seperate class (look up)


    here is my code.
    it moves only the first part.


    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; /// this all works!!!but the rest dont. why? i copied and pasted teh whole thing with a few modifications?!?!

    }


    Code:
      
    	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);
    			//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;
    	
    
    
    
            }
    
    }
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Leuven (Belgium)
    Posts
    168
    Rep Power
    273
    Code:
    deltaX=deltaX<0?-deltaX:deltaX;
    this line of code makes it so that deltaX will always, I repeat ALWAYS, be positive.
    The same is true for deltaY because of the following line:
    Code:
    deltaY=deltaY<0?-deltaY:deltaY;
    how do you expect a bishop to move in a direction that is not right and up?

    I still think your programming logic is faulty, since you have about 75% of your code that will never ever be accessed because of the reason I specified above.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by HeadOnAPlate
    Code:
    deltaX=deltaX<0?-deltaX:deltaX;
    this line of code makes it so that deltaX will always, I repeat ALWAYS, be positive.
    The same is true for deltaY because of the following line:
    Code:
    deltaY=deltaY<0?-deltaY:deltaY;
    how do you expect a bishop to move in a direction that is not right and up?

    I still think your programming logic is faulty, since you have about 75% of your code that will never ever be accessed because of the reason I specified above.

    Head. if you look at the post above, i removed both lines and it works.

    here is my updated code. valid1 works, valid2,valid3,valid4 dont.
    i wish i knew why. i checked them seperately, they work, but inside the method they dont.

    could you have a look:




    Code:
    
    	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--){
    							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){
    	   		
    			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;
    	
    
    
    
            }
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    Have you tried following the advice you have been given on the other forum? Do I need to repeat it here or can you go back and read it there?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0
    Originally Posted by NormR
    Have you tried following the advice you have been given on the other forum? Do I need to repeat it here or can you go back and read it there?
    Norm1, i have all my windows open simultaneously. i have been working on that problem since the morning. i tested every method. it works. i debugged all the loops. now i am going to look at the piececollision method to identify a small bug in the way i designed my logic, there might be a problem. i cant find it though

IMN logo majestic logo threadwatch logo seochat tools logo