Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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  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 

Room lay out in a digram:

image text in transcribed

image text in transcribed

image text in transcribed

Additional Information that would help:

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Ground Floor Charred Room Patio Larder Kitchen Dining Hall Ballroom Storeroom Library Foyer Conservatory Graveyard Entrance Hall Gardens Upper Floor Attic Abandoned Room Creaky Hallway Chasm Tower Junk Room Bedroom Balcony Collapsed Room Dusty Hallway Game Room Upper Landing Basement Wine Cellar Vault Gallery Research Laboratory Underground Lake Statuary Corridor Furnace Room Crypt Catacombs Chapel Basement Landing Overview Welcome to Room Navigator! In this assignment, you'll use the collections you've learned about to implement a path search algorithm that can navigate rooms in a maze. Or in our case, a maze-like house with multiple floors and an unreasonably large number of rooms. (If you've played the board game Betrayal at House on the Hill, you might recognize the room names; this assignment was very much inspired by the game! But don't worry if you're not familiar with it, as this assignment has nothing to do with the actual game). The Room Navigator program represents rooms with the simple Room class, which is defined for you in the Room.java file. Every Room keeps track of its name and a list of other neighboring Rooms that are connected. For example: Room room = // ... String roomName = room.getName(); ArrayList connectedRoomNames = room.getConnectedRooms(); There is a text file included in the project starter code named layout.txt. Inside, you'll see a list of room names, each followed by a semicolon and then a comma-separated list of other room names. The text file is used by the Room Navigator program to construct a list of Rooms with all of their connections set up. You don't have to implement this part -- it's already done for you! If you'd like a visual representation of how the rooms are laid out, I've uploaded a "Room Layout file on iLearn which shows what is described in layout.txt. Your task is to implement doesPathExist. The method takes in a list of Rooms (constructed from the "layout.txt file as described above), as well as the names of a start room and an end room. It should return true if, from the start room, you can repeatedly move to a connected neighboring room until you reach the end room, or false if that is not possible. To accomplish this, you'll make use of several different collections and a simple breadth-first search algorithm. See Breadth-First Search below for the detailed steps! When your implementation is complete, you should be able to call doesPathExist with different starting rooms and ending rooms and find varying answers (see the main method, which has a few sample calls). Every room is reachable from every other room on the same floor, but you'll notice there's no way to move between floors. If you ask whether a path exists between a ground floor room and a basement room, for example, the answer should be false. Breadth-First Search At a Glance The algorithm we'll use to determine whether we can find a path from a start location to a destination is called breadth-first search. 1. Begin at the start location (the starting room). 2. Look at every location that is one step away. Are any of these the destination? 3. Look at every location that is one step away from the rooms in #2. Are any of these the destination? 4. Look at every location that is one step away from the rooms in #3. Are any of these the destination? 5. etc.... At any point in this process, if a location you're looking at is the destination, then you've found a path and you're finished! But if you continue searching and eventually exhaust all of your search options without finding the destination, you can safely say that no such path exists. In the Room Navigator program, the locations that are one step away from a room are all it's connected neighbors. We will use the getConnectedRooms () method in implementing breadth-first search! But first... Setting Up Your Collections If you try to jump straight into implementing the breadth-first search algorithm described above, you're going to run into a major problem fairly quickly. Suppose you have a Room object and want to know what rooms you can reach. Room room = // ... ArrayList connectedRooms = room.getConnectedRooms(); It's simple enough to get a list of the immediately connected rooms. But how do we find all the rooms that are connected to those rooms? for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions