Question: How can I read this file into a 2-D array? A maze is specified by an input file with the following format: Row_count,Column_count Room_name,North_passageway,East_passageway,South_passageway,West_passageway,list_of_items_in_room The

How can I read this file into a 2-D array?

A maze is specified by an input file with the following format:

Row_count,Column_count Room_name,North_passageway,East_passageway,South_passageway,West_passageway,list_of_items_in_room 

The first line of the input file is the number of rows (Row_count) and the number of columns (Column_count) in the maze. The first line is followed by Row_count x Column_count lines with each line representing a different Room in the maze.

Each room has a name, four connected passageways, and a list of items in that room. Each passageway in a room is one of three types:

  1. "-": No passage is allowed
  2. "+": Passage is open
  3. "item_needed_to_open_passageway": Requires the item(s) specified to open the door

The input file for our example maze would look like the following:

2 3 The Start,-,+,red_key,-,axe blue_key The Landing,-,+,-,+, Master Bedroom,-,-,-,+,red_key The Kitchen,red_key,+,-,-, The Hall,-,+,-,+, The Exit!,-,+,-,+, 

Note that the list of items in the room is not comma-separated.

The Maze Class

This class holds the information for the Maze. The Maze is a two-dimensional array of Rooms represented by a Room**. You will need to read the information for the Maze from an input file, assemble the information into Room objects, and add them to the array. In addition to the parameterized constructor and a destructor, this class should have the following methods:

  • LoadMaze ()
    • This private helper function is used to read the file and create the two-dimensional array of Room objects.
  • CreatePassage()
    • This private helper function takes in a string of '-', '+', or an item need to open a door, and returns a pointer to an appropriate Passage object.
  • GetRoom (int row, int col)
    • Takes in a position in terms of a row and col and returns a pointer to the Room object at that location in the Maze.
  • GetNumberRows (), GetNumberCols ()
    • Simple getter methods that return the number of rows and columns in the maze.
C) Maze o rooms: Room** arows: int ocols: int mazeFile: std ifstream LoadMaze(): void CreatePassage std::string type): Passage* o Maze(std::string mazeFile) GetRoom(int row, int col) : Room* o GetNumberRows): int o GetNumberCols() int ~Maze() C) Maze o rooms: Room** arows: int ocols: int mazeFile: std ifstream LoadMaze(): void CreatePassage std::string type): Passage* o Maze(std::string mazeFile) GetRoom(int row, int col) : Room* o GetNumberRows): int o GetNumberCols() int ~Maze()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!