简体   繁体   English

如何用Java解决这个国际象棋骑士问题?

[英]How to solve this chess knight problem in Java?

I want to resolve a chess puzzle using Java. 我想使用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. 我编码为Knight片段从开始字段(1; 1)移动到任何地方,除了负的x和y之外,如果所有内容都有效,则将此访问字段放入列表中,否则返回到上一个字段。 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! 谢谢!

I guess the problem is that you are using if (aVisited == now) in your hasNotVisited method. 我想问题是您在hasNotVisited方法中使用了if (aVisited == now) You need if (aVisited.equals(now)) instead. 您需要if (aVisited.equals(now))代替。

  • When using == you are checking if two variables are referencing the same instance of the Vector . 使用==您要检查两个变量是否引用Vector的同一实例。
  • When using .equals you are checking if it concerns two Vector 's with the same properties/values. 使用.equals您要检查它是否涉及两个具有相同属性/值的Vector

EDIT: I just noticed that Vector does not override equals . 编辑:我只是注意到Vector不会覆盖equals See also the source code of Vector . 另请参见Vector源代码 Alternatively you can use (if aVisited.x == now.x && aVisited.y == now.y) in your hasNotVisited method. 或者,您可以在hasNotVisited方法中使用(if aVisited.x == now.x && aVisited.y == now.y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM