Question: Can you please help me to implement this doesPathExist() method in java assignment? Basically, this method takes in a list of Rooms (constructed from .txt
Can you please help me to implement this doesPathExist() method in java assignment? Basically, this method takes in a list of Rooms (constructed from .txt file) , as well as names of the start and end room. It should return true if,from the start room , you can repeatedly move to connected neighbouring room until you reach the end room , or false if that is not possible .
Note: Every room is reachable from every other room on the same floor but you will notice there is no way to move between floors. If you ask whether a path exists between a ground floor room and a basement room, the answer should be false.
Here are my Room.java, RoomNavigator.java and .txt files.
RoomNavigator.java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class RoomNavigator { /** * Returns true if and only if a path exists between the start room and the end room, based on the description of * rooms provided by the allRooms list. The start and end rooms are specified by their names. */ public boolean doesPathExist(ArrayList allRooms, String startRoom, String endRoom) { // TODO: Implement. Step 1: create and populate your Room map with everything from the rooms list. // TODO: Implement. Step 2: create your breadth-first search queue and start it off with the initial room. // TODO: Implement. Step 3: in a loop, repeatedly process the next Room in the queue. return false; } /** Program execution begins here. */ public static void main(String[] args) { ArrayList allRooms = readRooms(); RoomNavigator navigator = new RoomNavigator(); // Paths between rooms that are on the same floor (expecting 'true'): lookForPath(navigator, allRooms, "Larder", "Gardens"); lookForPath(navigator, allRooms, "Upper Landing", "Attic"); lookForPath(navigator, allRooms, "Chapel", "Wine Cellar"); // Paths between rooms that are on different floors (expecting 'false'): lookForPath(navigator, allRooms, "Patio", "Game Room"); lookForPath(navigator, allRooms, "Tower", "Crypt"); lookForPath(navigator, allRooms, "Underground Lake", "Library"); } /** * Reads the "layout.txt" file and creates a list of Room objects with their connections set up accordingly. You * should not modify this method in any way. */ private static ArrayList readRooms() { ArrayList rooms = new ArrayList<>(); try { BufferedReader bufferedReader = new BufferedReader(new FileReader("layout.txt")); String line; while ((line = bufferedReader.readLine()) != null) { if (line.strip().isEmpty()) { continue; } int splitIndex = line.indexOf(';'); if (splitIndex < 0) { throw new RuntimeException( "Invalid layout file! Missing ';' to separate room from its connected neighbors: " + line); } Room currentRoom = new Room(line.substring(0, splitIndex).strip()); for (String connectedRoom: line.substring(splitIndex + 1).split(",")) { currentRoom.addConnectedRoom(connectedRoom.strip()); } rooms.add(currentRoom); } bufferedReader.close(); } catch (IOException exception) { throw new RuntimeException(exception); } return rooms; } /** * Tests your doesPathExist implementation against the given start and end rooms. You can run your own check by * calling lookForPath in main, similar to the existing calls. */ private static void lookForPath( RoomNavigator navigator, ArrayList allRooms, String startRoom, String endRoom) { System.out.printf( "Path from '%s' to '%s': %s%n", startRoom, endRoom, navigator.doesPathExist(allRooms, startRoom, endRoom)); } } Room.java file
import java.util.ArrayList; /** * Represents a single room in the RoomNavigator program. Each room keeps track of a name, as well as a list of the * names of the other rooms it's connected to. */ public class Room { private String name; private ArrayList connectedRooms; public Room(String name) { this.name = name; this.connectedRooms = new ArrayList<>(); } public String getName() { return name; } public ArrayList getConnectedRooms() { return new ArrayList<>(connectedRooms); } public void addConnectedRoom(String roomName) { connectedRooms.add(roomName); } } .txt file Entrance Hall; Foyer Foyer; Entrance Hall, Library, Dining Hall, Conservatory Conservatory; Foyer, Graveyard, Gardens Graveyard; Conservatory Gardens; Conservatory Library; Foyer, Storeroom Storeroom; Library, Larder Larder; Storeroom, Kitchen Kitchen; Larder, Charred Room, Dining Hall Charred Room; Kitchen Dining Hall; Kitchen, Patio, Foyer, Ballroom Ballroom; Dining Hall Patio; Dining Hall Upper Landing; Dusty Hallway Dusty Hallway; Upper Landing, Game Room, Collapsed Room, Bedroom Game Room; Dusty Hallway Collapsed Room; Dusty Hallway Bedroom; Dusty Hallway, Junk Room, Balcony, Creaky Hallway Balcony; Bedroom Junk Room; Bedroom, Abandoned Room Creaky Hallway; Bedroom, Abandoned Room, Chasm Chasm; Creaky Hallway, Tower Tower; Chasm Abandoned Room; Creaky Hallway, Junk Room, Attic Attic; Abandoned Room Basement Landing; Catacombs Catacombs; Basement Landing, Chapel, Crypt, Statuary Corridor Chapel; Catacombs Crypt; Catacombs Statuary Corridor; Catacombs, Underground Lake, Furnace Room, Gallery Underground Lake; Statuary Corridor Furnace Room; Statuary Corridor Gallery; Statuary Corridor, Research Laboratory, Vault Research Laboratory; Gallery Vault; Gallery, Wine Cellar Wine Cellar; Vault
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
