编写一个在java中查找可能目的地的方法

编写一个在java中查找可能目的地的方法,java,algorithm,Java,Algorithm,我编写了一个方法,用于在用户掷骰子时指定可能的目的地。为此,我使用曼哈顿距离,但问题是,当此方法计算此距离时,也会从空家移动,并显示额外的答案。我怎样才能修好它??提前谢谢 public static Spot[][] spotsMatrix = new Spot[40][25]; public static Spot currentSpot; private void ShowDestination(Spot currentSpot, int dice_Value) { Spot

我编写了一个方法,用于在用户掷骰子时指定可能的目的地。为此,我使用曼哈顿距离,但问题是,当此方法计算此距离时,也会从空家移动,并显示额外的答案。我怎样才能修好它??提前谢谢

public static Spot[][] spotsMatrix = new Spot[40][25];
public static Spot currentSpot;

private void ShowDestination(Spot currentSpot, int dice_Value) {

    Spot Destination = null;
    if (currentSpot.Index_X > 0 && currentSpot.Index_X < 40
            && currentSpot.Index_Y > 0 && currentSpot.Index_Y < 25) {
        for (int i = currentSpot.Index_X - dice_Value; i <= currentSpot.Index_X
                + dice_Value; i++) {
            for (int j = currentSpot.Index_Y - dice_Value; j <= currentSpot.Index_Y
                    + dice_Value; j++) {
                if (spotsMatrix[i][j] != null) {
                    Destination = spotsMatrix[i][j];
                    if (Math.abs(Destination.Index_X - currentSpot.Index_X)
                            + Math.abs(Destination.Index_Y
                                    - currentSpot.Index_Y) == dice_Value) {
                        Destination.setDestination();
                    }
                }
            }
        }

    }
} 
Spot.java

public class Spot extends JButton{


public int spot_X;
public int spot_Y;
public int Index_X;
public int Index_Y;
public int spot_Type;
public boolean spot_IsDestination;


public Spot(final int Index_X,final int Index_Y, int s_Type){

    this.spot_X = 200+Index_X *20;
    this.spot_Y = 20+Index_Y *20;
    this.Index_X = Index_X;
    this.Index_Y = Index_Y;
    this.spot_Type = s_Type;

    //this.setText(s_Index+"");
    this.setBounds(this.spot_X, this.spot_Y, 20, 20);
    this.setBorder(BorderFactory.createEmptyBorder());
    switch(s_Type){
    case 0: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_0.png")); break;
    case 1: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_1.png")); break;
    case 2: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_1.png")); break;
    }
    this.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(Index_X +" "+Index_Y);

        }
    });


}



public void setDestination(){
    this.spot_IsDestination = true;
    this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_Destination.png"));
    //....
}

}
我试着写一个递归算法,但在这种情况下我也有问题!!如果可以,请为这两种情况提供帮助。谢谢

public class Spot extends JButton{


public int spot_X;
public int spot_Y;
public int Index_X;
public int Index_Y;
public int spot_Type;
public boolean spot_IsDestination;


public Spot(final int Index_X,final int Index_Y, int s_Type){

    this.spot_X = 200+Index_X *20;
    this.spot_Y = 20+Index_Y *20;
    this.Index_X = Index_X;
    this.Index_Y = Index_Y;
    this.spot_Type = s_Type;

    //this.setText(s_Index+"");
    this.setBounds(this.spot_X, this.spot_Y, 20, 20);
    this.setBorder(BorderFactory.createEmptyBorder());
    switch(s_Type){
    case 0: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_0.png")); break;
    case 1: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_1.png")); break;
    case 2: this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_1.png")); break;
    }
    this.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            System.out.println(Index_X +" "+Index_Y);

        }
    });


}



public void setDestination(){
    this.spot_IsDestination = true;
    this.setIcon(new ImageIcon("/Users/Desktop/forest/spot_Destination.png"));
    //....
}

}