Given the starting point in a maze you are to find and mark

Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (representing the foot-paths). There is only one exit from the maze (represented by E). You may move vertically or horizontally in any direction that contains a 0; you may not move into a square with a 1. If you move into the square with an E, you have exited the maze. If you are in a square with 1s on three sides, you must go back the way you came and try another path. You may not move diagonally. For this program, use can ONLY use a single linked list.

Program Requirements:

Your program should use single linked list ONLY for finding the path.

Input of program: Input is a 30x20 array of characters (1s, 0s, and E) from an ASCII text data file (maze.txt); as follows:

E0001110000000100100

11100011101110001111

11111000101000111000

00001110101100010010

01011000101111000110

00001110000110011110

11011100110110111000

00011110110111111101

01011011110110100001

01000000000110110111

11011011010010000000

01010010011000101011

01111000101110101110

00001110000111011001

01101011101101000011

11000110100111011010

01110000100100110011

11010111110110000000

01110100011000111110

00011001000011100010

01000000000110110111

11011011010010000000

01010010011000101011

01111000101110101110

00001110000111011001

01101011101101000011

11000110100111011010

01110000100100110011

11010111110110000000

01110100011000111110

Each data line consists of one row of maze. Starting points (i.e. a row, column pair) in the maze will be input from the keyboard.

Output of program: Echo print the maze complete with numbered rows and columns prior to asking the user for their starting point. For each entry into the maze, print the complete maze with a S in the starting point followed by the words ‘I am free’ if you have found a path out of the maze or the words ‘Help, I am trapped’ if you cannot. Your output could be in two formats:

Print the path (by using a series of pluses (+)) you took through the maze should one be found, OR Print the path as a single linked list.

Solution

package mazeGame;

                import java.io.*;

                import java.util.*;

                public class mazeGame {

                static String[][]maze;

                public static void main(String[] args)

                    {

                maze=new String[20][20];

                maze=fillArray(\"mazefile.txt\");

                    }

                public static String[][]fillArray(String file)

                    {

                     maze = new String[20][20];//establish 20 X 20 array to hold maze

                        try{

                        Scanner sc = new Scanner(new File(file));

                        for(int row = 0; row < 20; row++)

                        {

                            for(int col = 0; col < 20; col++)

                            {

                                maze[row][col] = sc.next();//from file to array

                            }//end for

                            sc.nextLine();//move to next line of input

                        }//end for

                printArray();

                       }//end try

                        catch(FileNotFoundException fe)

                        {

                        System.out.println(\"Error: file mazefile.txt not found!\");//exception handling

                        }//end catch

                     return maze;

                    }

                public static void printArray()

                        {

                    System.out.println();

                    for(int row=0;row<20;row++)

                    {

                        for(int col=0;col<20;col++)

                        {

                            System.out.print(maze[row][col]+\" \");

                        }

                        System.out.println();

                    }

                }

                public void location()

                {

                    System.out.println(\"Please enter from the Matrix location.\");

                Scanner StartingPoint = new Scanner(System.in);

                String index = StartingPoint.nextLine();

                String[] indices = index.split(\" \");

                    }

                public String getByte( String arrayValue, int index)

                    throws IllegalArgumentException, ArrayIndexOutOfBoundsException

                {

                return arrayValue;

                    }

                                String x = arrayValue;

                switch (x) {

                case 0:

                    push();

                    S = StartingPoint;

                    System.out.println(\"S\");

                    break;

                case 1:

                    Move();

                    System.out.println(\"Help, I am trapped\");

                    break;

                case E:

                    Exit();

                    System.out.println(\"I am free\");

                    break;

                default:

                    doSomethingElse();

                }

                }

                public interface Stack

                {

                void push(Object X);

                void pop();

                Object top();

                Object move();

                boolean isEmpty();

                public void push(Object X)

                {

                if( topofStack + 1 == theArray.length)

                     fillArray();

                   maze[++topOfStack ] = x;

                }

                public void pop()

                    {

                     if(isEmpty())

                         throw new UnderflowException( \"ArrayStack pop\");

                     topOfStack--;

                    }

                public Object top()

                {

                    if( isEmpty())

                        throw new UnderflowException( \"ArrayStack top\");

                    return theArray[topOfStack];

}

}

Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (re
Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (re
Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (re
Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (re
Given the starting point in a maze, you are to find and mark a path out of the maze which is represented by a 30x30 array of 1s (representing hedges) and 0s (re

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site