如何在不打破的情况下回到while循环的开头?

时间:2014-06-18 01:51:49

标签: java path-finding

我有一个while循环包含很多if:

public ArrayList<Position> findPath(Position destination) {
        boolean pathToDestinationCreated = false;
        boolean xReached = false;
        boolean yReached = false;
        int reachableX = 0;
        int reachableY = 0;
        int reachableXY = 0;

        int isUnreachable = 0;
        Position myPos = myPlayer().getPosition();
        ArrayList<Position> finalPath = new ArrayList<Position>();
        if (destination != null) {
            Position toAdd;
            while (!pathToDestinationCreated) {
                if (myPos.getX() != destination.getX() && myPos.getY() != destination.getY()) {
                    if (myPos.getX() < destination.getX() && myPos.getY() < destination.getY()) {
                        toAdd = new Position(myPos.getX() + 1, myPos.getY() + 1, myPos.getZ());
                        if (canReach(toAdd)) {
                            finalPath.add(toAdd);
                            myPos = toAdd;
                            isUnreachable = 0;
                        } else {
                            isUnreachable = 1;
                        }
                    } else if (myPos.getX() > destination.getX() && myPos.getY() < destination.getY()) {
                        toAdd = new Position(myPos.getX() - 1, myPos.getY() + 1, myPos.getZ());
                        if (canReach(toAdd)) {
                            finalPath.add(toAdd);
                            myPos = toAdd;
                            isUnreachable = 0;
                        } else {
                            isUnreachable = 1;
                        }
                    } else if (myPos.getX() > destination.getX() && myPos.getY() > destination.getY()) {
                        toAdd = new Position(myPos.getX() - 1, myPos.getY() - 1, myPos.getZ());
                        if (canReach(toAdd)) {
                            finalPath.add(toAdd);
                            myPos = toAdd;
                            isUnreachable = 0;
                        } else {
                            isUnreachable = 1;
                        }
                    } else if (myPos.getX() < destination.getX() && myPos.getY() > destination.getY()) {
                        toAdd = new Position(myPos.getX() + 1, myPos.getY() - 1, myPos.getZ());
                        if (canReach(toAdd)) {
                            finalPath.add(toAdd);
                            myPos = toAdd;
                            isUnreachable = 0;
                        } else {
                            isUnreachable = 1;
                        }
                    }
                } 

我会跳过剩下的代码,因为一面文字并没有帮助。

每当调用finalPath.add(toAdd)时,我都想回到while循环的开头。我该怎么做?

0 个答案:

没有答案