0

Hey guys,
I am at the very beginning of learning Java and my task right now is to code a maze (2D Array) and let a bot automatically solve that maze using only the right hand rule.
If I let the programm run it prints out an infinite number of the maze I choose. The programm must also print out the way the bot is "walking" step by step.
A little help would be very welcome!

import java.util.Scanner;

public class BB8Labyrinth {

    public static void outputMaze (char [][] maze) {

        System.out.print("");
        for (int i = 0; i < maze.length; i++) {
            for (int j = 0; j < maze[i].length; j++) {
                System.out.print(maze[i][j] + " ");
            }
        }
    }

    public static void mazeSolver (char maze [][], char planMaze [][], char botview, int botX, int botY) {

        maze[botY][botX] = botview;

        while (planMaze[botX][botY] != 'A') {

            switch (botview) {
                case '>' :
                    if (planMaze[botX][botY + 1] == '.') {
                        botview = '^';
                        break;
                    }else {
                        botY += 1;
                        outputMaze(planMaze);
                        break;
                    }
                case '^' :
                    if (planMaze[botX + 1][botY] == '.') {
                        botview = '<';
                        break;
                    }else {
                        botX += 1;
                        outputMaze(planMaze);
                        break;
                    }
                case '<' :
                    if (planMaze[botX][botY - 1] == '.') {
                        botview = 'v';
                        break;
                    }else {
                        botY -= 1;
                        outputMaze(planMaze);
                        break;
                    }
                case 'v' :
                    if (planMaze[botX - 1][botY] == '.') {
                        botview = '>';
                        break;
                    }else {
                        botX -= 1;
                        outputMaze(planMaze);
                        break;
                    }
            }
        }
    }

    public static void main (String [] args) {

        int mazeSelection;
        final char WALL = '.';
        char path = ' ';
        char start = 'B';
        char finish = 'A';
        char planMaze [][];
        char botview = '>';

        int botX;
        int botY;

        Scanner scan = new Scanner(System.in);

        char maze1 [][] =
            {   {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'},
                {WALL,path,path,path,WALL,path,WALL,path,path,path,WALL,'\n'},
                {WALL,WALL,WALL,path,WALL,path,path,path,path,path,WALL,'\n'},
                {start,path,WALL,path,WALL,path,WALL,WALL,path,WALL,WALL,'\n'},
                {WALL,path,path,path,path,path,path,WALL,path,path,WALL,'\n'},
                {WALL,WALL,path,WALL,WALL,WALL,path,WALL,WALL,path,WALL,'\n'},
                {WALL,path,path,WALL,path,WALL,path,WALL,path,path,WALL,'\n'},
                {WALL,path,WALL,WALL,path,WALL,path,WALL,path,WALL,WALL,'\n'},
                {WALL,path,path,WALL,path,path,path,WALL,path,path,finish,'\n'},
                {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'}
            };

        char maze2 [][] =
            {   {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'},
                {WALL,path,path,path,path,path,path,path,WALL,path,WALL,path,path,WALL,path,WALL,'\n'},
                {WALL,path,WALL,path,WALL,WALL,WALL,path,WALL,path,path,path,path,path,path,WALL,'\n'},
                {WALL,path,WALL,path,WALL,path,WALL,path,WALL,path,WALL,WALL,WALL,WALL,path,WALL,'\n'},
                {WALL,path,WALL,path,WALL,path,WALL,WALL,WALL,path,WALL,path,path,WALL,path,WALL,'\n'},
                {start,path,WALL,path,WALL,path,path,path,path,path,WALL,WALL,path,path,path,WALL,'\n'},
                {WALL,WALL,WALL,path,WALL,WALL,path,WALL,WALL,path,WALL,path,path,WALL,WALL,WALL,'\n'},
                {WALL,path,path,path,WALL,path,path,WALL,path,path,WALL,WALL,path,WALL,path,WALL,'\n'},
                {WALL,path,path,path,path,path,WALL,WALL,path,path,WALL,path,path,path,path,finish,'\n'},
                {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'}
            };

        char maze3 [][] =
            {   {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'},
                {WALL,path,path,path,path,path,path,path,path,path,WALL,path,WALL,'\n'},
                {WALL,path,WALL,WALL,path,WALL,WALL,WALL,WALL,WALL,WALL,path,WALL,'\n'},
                {WALL,path,path,WALL,path,path,path,path,path,path,WALL,path,WALL,'\n'},
                {WALL,WALL,path,WALL,WALL,path,WALL,path,WALL,WALL,WALL,path,WALL,'\n'},
                {WALL,path,path,path,WALL,path,WALL,path,WALL,path,WALL,path,WALL,'\n'},
                {WALL,path,WALL,path,WALL,path,WALL,path,path,path,WALL,path,WALL,'\n'},
                {WALL,WALL,WALL,path,WALL,path,WALL,WALL,path,WALL,WALL,path,WALL,'\n'},
                {start,path,path,path,WALL,path,path,WALL,path,path,path,path,WALL,'\n'},
                {WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,path,WALL,'\n'},
                {WALL,path,path,path,path,path,path,path,WALL,path,path,path,WALL,'\n'},
                {WALL,path,path,WALL,WALL,WALL,WALL,path,path,path,WALL,WALL,WALL,'\n'},
                {WALL,path,path,path,path,path,WALL,path,WALL,path,path,path,WALL,'\n'},
                {WALL,WALL,WALL,WALL,finish,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,'\n'}
            };

        System.out.println("Herzlich Willkommen zum BB-8 Labyrinth-Löser von Michael Seidel und Luca Wagenhäuser!");
        System.out.println("\nEs gibt verschiedne Wege für BB-8, die ihn zum Ziel führen.\nSie haben 3 verschiedene Labyrinthe zur Auswahl, die Ihr Droide BB-8 durchlaufen kann um zu seinem alten Freund R2D2 zu gelangen.");
        System.out.println("\nBitte wählen Sie mit den Eingaben: 1, 2 oder 3 aus welches Labyrinth BB-8 durchlaufen soll.");
            mazeSelection = scan.nextInt();

            if (mazeSelection < 1 || mazeSelection > 3) {
                System.out.println("Sie haben eine Falsche Zahl eingegeben. Bitte geben Sie entweder 1,2 oder 3 ein.");
                mazeSelection = scan.nextInt(); //Hier muss die Überprüfung noch abgeschlossen werden, sodass dann die neu eingegebenen Werte verwendet werden

            }else {

                        if (mazeSelection == 1) {
                            System.out.println("Sie haben sich für Labyrinth 1 Entschieden.\nDies ist Labyrinth 1:");
                            planMaze = maze1;
                            botX = 0;
                            botY = 3;
                            outputMaze(maze1);
                            mazeSolver(maze1, planMaze, botview, botX, botY);

                        }else if (mazeSelection == 2) {
                            System.out.println("Sie haben sich für Labyrinth 2 Entschieden.\nDies ist Labyrinth 2:");
                            planMaze = maze2;
                            botX = 0;
                            botY = 5;
                            outputMaze(maze2);

                       }else {
                            System.out.println("Sie haben sich für Labyrinth 3 Entschieden.\nDies ist Labyrinth 3:");
                            planMaze = maze3;
                            botX = 0;
                            botY = 8;
                            outputMaze(maze3);
                           }
            }

        scan.close();
        }
}
3
Contributors
2
Replies
28
Views
6 Months
Discussion Span
Last Post by rproffitt
0

If I let the programm run it prints out an infinite number of the maze I choose

It looks like the program wil print the entire maze each time it tries each step in finding the solution - so not infinite, but just far too many.

Starting to debug a program on full-size data is a mistake - there's too much happening all at once.
Create a tiny maze (eg 2x2) with just 1 path thru it and test it on that, then a 3x3 with a path and two dead ends. With such trivial cases you can see exactly what's happening and where it's going wrong. Fix those and a full-size maze will probably be OK.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.