Java Assignment: CaveExplorer Path-Finding Project

 

Assignment Overview



This assignment guides you in building a set of classes to support a program that finds paths through a cave.
By the end of Unit 4, you will have a program that can:

  • Read a 2D cave layout from a file,

  • Search the layout to find a path to a mirror pool,

  • Print the path to take.

In this unit, you will:

  • Build the infrastructure for the project,

  • Store cave data in a two-dimensional structure,

  • Search the structure for a simple path from start to finish,

  • Read cave data from a text file.


Preparation

  • See the course announcements for a link to the GitHub repository for this assignment.


Directions

Write a class CaveExplorer that includes the following methods and features:

Required Methods & Functionality

  1. Constructor (no parameters):

    • Creates the 2D structure shown below (hardcoded).

    • Cave layout uses:
      'R' = rock, '.' = clear path, 'M' = mirror pool, 'S' = self (start).

    • Example layout (hardcoded):



      R R R R R R R . . S R R R . R R R R R . M R R R R R R R R R
    • For the initial configuration, toString() would output:
      "RRRRRR\nR..SRR\nR.RRRR\nR.MRRR\nRRRRRR\n"

  2. toString() method:

    • Returns a string (including new lines) showing the current state of the cave.

  3. solve() method:

    • Returns true if there is a path from 'S' (self/start) to 'M' (mirror pool), false otherwise.

    • Must search in four directions (N/E/S/W) and avoid infinite loops.

  4. getPath() method:

    • Returns a string showing the path directions to reach the mirror pool.

    • For the example, it would return "wwsse" (West, West, South, South, East).

    • Returns an empty string if there is no path.

  5. Constructor (with one String parameter):

    • Receives the name of a text file containing the cave layout.

    • The file begins with two integers (number of rows and columns), followed by the layout itself.

    • Example file contents:


      5 6 RRRRRR R..SRR R.RRRR R.MRRR RRRRRR
    • The cave layout should have a path requiring at least 4 moves in at least two directions.

    • There must be exactly one path through the cave (from any location, only one unvisited move is possible at each step).

  6. main method:

    • Test your class by creating at least two CaveExplorer objects.

    • For each object:

      • Solve for the path,

      • Print the starting layout,

      • Print the final layout,

      • Print the path taken (if it exists).

    • Follow this order for your outputs.


Additional Notes

  • If you need help reading from a file, refer to the section on Scanners:
    Java Notes on Using Scanners

  • You may use your favorite editor to modify the text file with the cave data.


Solution

import java.io.*;import java.util.*;public class CaveExplorer {

// data fieldschar [][] structure  ;static ArrayList<Character> ThePath ;static ArrayList<String> readFromFile  ;

//Constructor with no parameterspublic CaveExplorer() {

structure  = new char[][] { { 'R', 'R', 'R', 'R', 'R', 'R' },

                                { 'R', '.', '.', 'S', 'R' , 'R'},

                                { 'R', '.', 'R', 'R', 'R' , 'R'},

                                { 'R', '.', 'M', 'R', 'R' , 'R'},

                                { 'R', 'R', 'R', 'R', 'R', 'R' }, };

ThePath = new ArrayList();readFromFile = new ArrayList();                }

//toString - no parameters, returns a stringpublic String toString(){

String result ="";

for(int i = 0 ; i < structure.length ; i++){

    for(int j = 0 ; j < structure[0].length ; j++){

        result += structure[i][j];     } result +="\n";    }

return result;    }

//solve - no parameters, returns a boolean true if there is a path to the mirror pool, and false if there is notpublic boolean solve(){

boolean travel[][] = new boolean[structure.length][structure[0].length];

boolean checked = false;

for (int i = 0; i < structure.length; i++) {

for (int j = 0; j < structure[0].length; j++) {

   if (structure[i][j] == 'S'&& !travel[i][j])

       if (checkpath(structure, i, j, travel)) {           checked = true;           break;       }}}

if (checked)   return true;else   return false;

   }

// checkpath to pass through 2d array character  to find the pathpublic boolean checkpath(char structure[][],int i, int j,boolean travel[][]){

if ((i >= 0 && i < structure.length&& j >= 0&& j < structure[0].length)&& structure[i][j] != 'R'&& !travel[i][j]) {       travel[i][j] = true;if (structure[i][j] == 'M')   return true;if (checkpath(structure, i - 1,j, travel)) {ThePath.add('N');   return true;}if (checkpath(structure, i, j - 1, travel)) {ThePath.add('W');   return true;}if (checkpath(structure, i + 1, j, travel)) {ThePath.add('S');   return true;}if (checkpath(structure, i, j + 1,travel)) {ThePath.add('E');   return true;}}return false;

   }

//getPath - no parameters, returns a String showing the path taken to get to the mirror poolpublic String getPath() {if(ThePath.size() == 0)            solve();

String result ="";

Collections.reverse(ThePath);

for(Character getpath :ThePath)            result += getpath;

return result;

}

// Constructor with one String parameterpublic CaveExplorer (String fname) throws Exception {

Scanner input =null;ThePath = new ArrayList();readFromFile = new ArrayList();ThePath.clear();int row , col;

try{   input = new Scanner(new File(fname));} catch (FileNotFoundException e ) {

           System.out.println("File not found "  );

       } catch (Exception e ) {

           System.out.println(e );

       }row = input.nextInt();col = input.nextInt();input.nextLine();

structure  = new char[row][col];String value ="";

while (input.hasNextLine()) {      value   = input.nextLine() ;   readFromFile.add(value);        }int index = 0 ;for(String data:readFromFile) {         if(data.length()==col) {         structure[index++] = data.toCharArray();         }         else {         throw new Exception("Error ");         }        }   }

public static void main(String[] args) {

// first objectCaveExplorer  cave = new CaveExplorer();

System.out.println(cave.toString());System.out.println("There is a path : "+cave.solve());System.out.println("getPath: "+cave.getPath());

// second  object

try {

CaveExplorer cave2 = new CaveExplorer("testcave.txt");

System.out.println("\n\n"+cave2.toString());System.out.println("There is a path : "+cave2.solve());System.out.println("getPath: "+cave2.getPath());

}catch (Exception e) { System.out.println(e );       }

   }

}

testcave.txt

5 6RRRRRRR..SRRR.RRRRR.MRRRRRRRRR



📩 Need a similar solution? Email me: adel455@hotmail.com












Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming