Answered step by step
Verified Expert Solution
Question
1 Approved Answer
public static Maze loadMaze ( final String path ) throws IOException { BufferedReader reader = new BufferedReader ( new FileReader ( path ) ) ;
public static Maze loadMazefinal String path throws IOException
BufferedReader reader new BufferedReadernew FileReaderpath;
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.printlnline;
String words line.split;
System.out.printlnArraystoStringwords;
if wordslength
continue; Invalid line format, skip
String objectName words;
String objectId words;
if objectNameequalsroom
int roomNumber Integer.parseIntobjectId;
Room room new RoomroomNumber;
roomsMap.putroomNumber room;
maze.addRoomroom;
for int i ; i ; i
String side wordsi ;
Direction direction Direction.valuesi;
if sideequalswall
room.setSidedirection new Wall;
else if sidestartsWithd
Door will be set after door parsing
else if objectNameequalsdoor
int roomNumber Integer.parseIntwords;
int roomNumber Integer.parseIntwords;
boolean isOpen wordsequalsopen;
String isOpen words;
Room room roomsMap.getroomNumber;
Room room roomsMap.getroomNumber;
Door door new Doorroom room;
ifisOpenequalsopen
door.setOpentrue;
else
door.setOpenfalse;
doorsMap.putobjectId door;
Set the door on the correct sides of the rooms
for Direction direction : Direction.values
if roomgetSidedirection null
roomsetSidedirection door;
break;
for Direction direction : Direction.values
if roomgetSidedirection null
roomsetSidedirection door;
break;
reader.close;
Set the current room to the first room added if any
if roomsMap.isEmpty
maze.setCurrentRoomroomsMapvaluesiteratornext;
return maze;
This implementation works for small.maze below room wall d wall wall
room d wall d wall
room wall wall wall d
door d close
door d open. Fix the implementation so that it works for large.maze below room wall d wall wall
room d wall d wall
room wall wall wall d
door d close
door d open
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started