MazeReader::MazeReader(std::string file) throw(MazeCreationException) { m_filename = file; m_in.open(m_filename); if(!m_in.good()) { throw(MazeCreationException("\nThe file could not be opened\n")); } m_in>>m_rows; m_in>>m_cols; m_in>>startRow; m_in>>startCol; if(m_rows < 1 || m_cols < 1) { throw(MazeCreationException("\nInvalid size of maze\n")); } if(startCol < 0 || startRow < 0 || startRow >= m_rows || startCol >= m_cols) { throw(MazeCreationException("\nInvalid starting position\n")); } try { readMaze(); } catch(MazeCreationException& e) { std::cout<<e.what()<<std::endl; } m_in.close(); }
void MazeReader::readMaze() throw (MazeCreationException){ std::ifstream myInput(filename); //check for validity of file if(!(myInput.good())) { throw MazeCreationException("Error reading Maze file.\n"); } else { myInput >> m_rows; myInput >> m_columns; myInput >> m_startRow; myInput >> m_startCol; //initialize MazeArray to proper size MazeArray = new char*[m_rows]; //create the rows of arrays for(int k = 0; k< m_rows; k++) { MazeArray[k] = new char[m_columns]; } //populate array with characters for(int i = 0; i < m_rows; i++) { //std::cout << "entered first loop\n"; for(int j = 0; j < m_columns; j++) { //for each index of char array myInput >> MazeArray[i][j]; } } } //check for valid size and starting position if(m_rows <= 0 || m_columns <= 0 || m_startCol < 0 || m_startCol > m_columns || m_startRow > m_rows || m_startRow < 0) { throw MazeCreationException("Error with Maze Size or Starting Position.\n"); } //test output std::cout << "Maze Information: \n" << "# rows: " << m_rows << "\n" << "# cols: " << m_columns << "\n" << "starting row: " << m_startRow << "\n" << "starting column: " << m_startCol << "\n"; }