0

I want to resolve a chess puzzle using Java. I code that Knight piece moves from begin field (1;1) anywhere, except for negative x and y, if everything is valid, put this visited field in list, else, return to previous. But it doesn't work at all, this condition is never true, it is negative all the time, and it doesn't go back to previous field, what may cause the problem?

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main
{
    static Vector now;
    static Vector prev;

    static List<Vector> visited = new ArrayList<>();

    public static void main(String[] args)
    {
        now = new Vector(); // sets x = 1, y = 1
        prev = now; // also x = 1, y = 1

        visited.add(now); // we are already on (1;1)

        generate();
    }

    static void generate()
    {
        Random rand = new Random();

        for (int i = 0; i < 50; i++)
        {
            int a = rand.nextInt(8);
            move(a);

            if((isValid()) && hasNotVisited()) // if x and y > 0 , because the smallest coord is (1;1), and check is we haven't visited that field
            {
                visited.add(now);
                prev = now; // previous coord is now, then make a new step
            }
            else
            {
                now = prev; // else, return to previous coord
                // For example, we are one (3;2), we move to (1;0), that's not valid, we should move back to (3;2)
            }
        }
    }

    static void move(int a)
    {
        switch (a){
            case 0:
                now.x += 2;
                now.y++;
                break;
            case 1:
                now.x += 2;
                now.y--;
                break;
            case 2:
                now.x -= 2;
                now.y++;
                break;
            case 3:
                now.x -= 2;
                now.y--;
                break;
            case 4:
                now.y += 2;
                now.x++;
                break;
            case 5:
                now.y += 2;
                now.x--;
                break;
            case 6:
                now.y -= 2;
                now.y++;
                break;
            case 7:
                now.y -= 2;
                now.y--;
                break;
        }
    }

    static boolean hasNotVisited()
    {
        for (Vector aVisited : visited) {
            if (aVisited == now)
                return false;
        }

        return true;
    }

    static boolean isValid()
    {
        return (0 < now.x && now.x <= 10) && (0 < now.y && now.y <= 10);
    }
}

Thank you!

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Dan Durnev
  • 89
  • 3
  • 12
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](https://stackoverflow.com/q/19843506/5221149) – Andreas Nov 06 '18 at 19:55
  • Possible duplicate of [What is the difference between == vs equals() in Java?](https://stackoverflow.com/q/7520432/5221149) – Andreas Nov 06 '18 at 19:56

1 Answers1

1

I guess the problem is that you are using if (aVisited == now) in your hasNotVisited method. You need if (aVisited.equals(now)) instead.

  • When using == you are checking if two variables are referencing the same instance of the Vector.
  • When using .equals you are checking if it concerns two Vector's with the same properties/values.

EDIT: I just noticed that Vector does not override equals. See also the source code of Vector. Alternatively you can use (if aVisited.x == now.x && aVisited.y == now.y) in your hasNotVisited method.

MWB
  • 1,830
  • 1
  • 17
  • 38