I'm trying to create a class that calculates for the next possible coordinate for a Bishop Piece for my chess program and I encountered this problem.

I used a JOptionPane to test for the results and I can't understand how it got that result.

The first JOptionPane shows false because I used the method for the first time.
FALSE MEANS THAT THE LIST IS NOT EMPTY
On the second and third JOptionPane I was expecting to see a false but it showed true.
Can someone explain to me how it became true.

I'm new to java but i have some experience in C#

import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JOptionPane;


public class Movement {
    
     public final static int NORTH=-1; 
     public final static int SOUTH=1;
     public final static int EAST=1;
     public final static int WEST=-1;
  
     public ArrayList<Point> getDiagonalPoints(Point origin){
         ArrayList<Point> list =new ArrayList<Point>();
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,NORTH,origin).isEmpty()); //OUTPUTS FALSE
         list.addAll(getPointsOf(WEST,NORTH,origin));
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,NORTH,origin).isEmpty());
//OUTPUTS TRUE
         list.addAll(getPointsOf(WEST,SOUTH,origin));
         JOptionPane.showMessageDialog(null,getPointsOf(WEST,SOUTH,origin).isEmpty());
         JOptionPane.showMessageDialog(null,list.size());
         //list.addAll(getPointsOf(EAST,SOUTH,origin));
         //list.addAll(getPointsOf(EAST,NORTH,origin));
         return list;
     }
     
     public ArrayList<Point> getPointsOf(int x,int y,Point origin){
         ArrayList<Point> options =new ArrayList<Point>(); 
         
         while(origin.x<8&&origin.x>=0&&origin.y<8&&origin.y>=0){
             options.add(new Point(origin.x,origin.y));
             origin.x+=x;
             origin.y+=y; 
         }
         return options;
     }
}

You call the method three times with the same input. What makes you expect different output?

Method getPointsOf modifies your origin instance of Point class (lines 32,33)
If it is unnecessary use a local "clone".

public static ArrayList<Point> getPointsOf(int x, int y, Point orig) {
        Point origin = new Point(orig);///////////////////
        ArrayList<Point> options = new ArrayList<Point>();
        //System.out.println("=== " + origin.x + " " + origin.y);
        while (origin.x < 8 && origin.x >= 0 && origin.y < 8 && origin.y >= 0) {
            options.add(new Point(origin.x, origin.y));
            //System.out.println("added " + origin.x + " " + origin.y);
            origin.x += x;
            origin.y += y;
        }
        return options;
    }

Method getPointsOf modifies your origin instance of Point class (lines 32,33)
If it is unnecessary use a local "clone".

public static ArrayList<Point> getPointsOf(int x, int y, Point orig) {
        Point origin = new Point(orig);///////////////////
        ArrayList<Point> options = new ArrayList<Point>();
        //System.out.println("=== " + origin.x + " " + origin.y);
        while (origin.x < 8 && origin.x >= 0 && origin.y < 8 && origin.y >= 0) {
            options.add(new Point(origin.x, origin.y));
            //System.out.println("added " + origin.x + " " + origin.y);
            origin.x += x;
            origin.y += y;
        }
        return options;
    }

Thank you I understood my mistake now :)

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.