0

ファイルから迷路を読み取り、それを 2D 配列に配置し、再帰を使用してナビゲートし、Pをどこにでも配置し、Vをどこにでも配置するプログラムを持っています。私のアルゴリズムのどこかで、迷路に V しかないという問題が発生します。println問題をデバッグしようとする多くのステートメントを入れました。私はJavaが初めてで、おそらく明らかな何かが欠けている可能性がありますが、私の人生の問題を見つけることはできません。

迷路コード:

public static boolean goNorth(){
        boolean success;
        if(maze[currCol][currRow - 1] == CLEAR){
            currRow = currRow - 1;
            maze[currCol][currRow] = PATH;
            if (maze[currCol][currRow] == maze[finishCol][finishRow]){ 
            success = true;
            } else {
                success = goNorth();
                if(!success){
                success = goWest();
                    if(!success){
                    success = goEast();
                        if(!success){
                        maze[currCol][currRow] = VISITED;
                        currRow = currRow + 1;
                        }
                    }
                }
            }
        } else {
            success = false;
    }
    return success;
}

public static boolean goWest(){
        boolean success;
        if(maze[currCol - 1][currRow] == CLEAR){
            currCol = currCol - 1;
            maze[currCol][currRow] = PATH;
            if (maze[currCol][currRow] == FINISH){
            success = true;
            } else {
            success = goWest();
                if(!success){
                success = goSouth();
                    if(!success){
                    success = goNorth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                        currCol = currCol + 1;
                        }
                        }
                    }
                }
            } else {
                success = false;
        }
        return success;
    }

public static boolean goEast(){
        boolean success;
        if(maze[currCol + 1][currRow] == CLEAR){
            currCol = currCol + 1;
            maze[currCol][currRow] = PATH;
            if (maze[currCol][currRow] == FINISH){
            success = true;
            } else {
            success = goEast();
                if(!success){
                success = goNorth();
                    if(!success){
                    success = goSouth();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currCol = currCol - 1;
                            }
                        }
                    }
                }
            } else {
                success = false;
        }
        return success;
    }

public static boolean goSouth(){
        boolean success;
        if(maze[currCol][currRow + 1] == CLEAR){
            currRow = currRow + 1;
            maze[currCol][currRow] = PATH;
            if (maze[currCol][currRow + 1] == FINISH){
            success = true;
            } else {
            success = goSouth();
                if(!success){
                success = goEast();
                    if(!success){
                    success = goWest();
                        if(!success){
                            maze[currCol][currRow] = VISITED;
                            currRow = currRow - 1;
                            }
                        }
                    }
                }
            } else {
                success = false;
        }
        return success;
    }

望ましい出力:

xxxxxxxxxxxxxxxxxxFx
xVVVVVxPPPPPPPxxxxPx
xVxxxxxPxxxxxPPPxxPx
xVxxxxxPxxxxxxxPxxPx
xVVVVVVPPPPPPxxPxxPx
xVxxxxxxxxxxPxxPPPPx
xxxxxxxxxxxxSxxxxxxx

私が得ている出力:

xxxxxxxxxxxxxxxxxxVx
xVVVVVxVVVVVVVxxxxVx
xVxxxxxVxxxxxVVVxxVx
xVxxxxxVxxxxxxxVxxVx
xVVVVVVVVVVVVxxVxxVx
xVxxxxxxxxxxVxxVVVVx
xxxxxxxxxxxxSxxxxxxx
4

2 に答える 2