Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public static Maze loadMaze ( final String path ) throws IOException { BufferedReader reader = new BufferedReader ( new FileReader ( path ) ) ;

public static Maze loadMaze(final String path) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(path));
//List lines = new ArrayList<>();
Maze maze = new Maze();
String line;
Map roomsMap = new HashMap<>();
Map doorsMap = new HashMap<>();
while ((line = reader.readLine())!= null){
System.out.println(line);
String[] words = line.split("");
System.out.println(Arrays.toString(words));
if (words.length <2){
continue; // Invalid line format, skip
}
String objectName = words[0];
String objectId = words[1];
if (objectName.equals("room")){
int roomNumber = Integer.parseInt(objectId);
Room room = new Room(roomNumber);
roomsMap.put(roomNumber, room);
maze.addRoom(room);
for (int i =0; i <4; i++){
String side = words[i +2];
Direction direction = Direction.values()[i];
if (side.equals("wall")){
room.setSide(direction, new Wall());
}
else if (side.startsWith("d")){
// Door will be set after door parsing
}
//
}
} else if (objectName.equals("door")){
int room1Number = Integer.parseInt(words[2]);
int room2Number = Integer.parseInt(words[3]);
//boolean isOpen = words[4].equals("open");
String isOpen = words[4];
Room room1= roomsMap.get(room1Number);
Room room2= roomsMap.get(room2Number);
Door door = new Door(room1, room2);
if(isOpen.equals("open")){
door.setOpen(true);
}else {
door.setOpen(false);
}
doorsMap.put(objectId, door);
// Set the door on the correct sides of the rooms
for (Direction direction : Direction.values()){
if (room1.getSide(direction)== null){
room1.setSide(direction, door);
break;
}
}
for (Direction direction : Direction.values()){
if (room2.getSide(direction)== null){
room2.setSide(direction, door);
break;
}
}
}
}
reader.close();
// Set the current room to the first room added (if any)
if (!roomsMap.isEmpty()){
maze.setCurrentRoom(roomsMap.values().iterator().next());
}
return maze;
}This implementation works for small.maze below room 0 wall d0 wall wall
room 1 d0 wall d1 wall
room 2 wall wall wall d1
door d001 close
door d112 open. Fix the implementation so that it works for large.maze below room 0 wall d0 wall wall
room 1 d0 wall d1 wall
room 2 wall wall wall d1
door d001 close
door d112 open

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_2

Step: 3

blur-text-image_3

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

Oracle 12c SQL

Authors: Joan Casteel

3rd edition

1305251032, 978-1305251038

More Books

Students also viewed these Databases questions