#include #include #include #include "lab.h" using namespace std; Lab::Lab() { loadFile(); buildMaze(); } int Lab::loadFile() { const int version_line=29; char fname[]="lab.dat"; char tmp[version_line]={0}; char tmp2[version_line]={"Labyrinth format version 1.0"}; int x=0; //open file and check for error ifstream inFile(fname); if(!inFile){ cerr << select_error_message(BAD_OPEN) << fname << endl; exit(BAD_OPEN); } //load queue while(inFile.peek() != EOF){ maze_q.push(inFile.get()); } //close file inFile.close(); //check format version for(x=0;x temp_q(maze_q); static bool Found_Exits = false; // There can be only ONE! static bool Found_Minotaur = false; static bool Found_Player = false; // initialize size variables: rows = 0; cols = 0; lvls = 0; // pop() until we get an 'X'. First time in we will skip this step. while(!temp_q.empty() && temp_q.front()=='\n') temp_q.pop(); // Get the # of cols by counting the # of cells in the first line. while(!temp_q.empty() && temp_q.front() != '\n'){ switch(temp_q.front()){ case 'e': //the first line could have an exit if(Found_Exits){ cerr << select_error_message(TOO_MANY_EXITS) << "calcSize()" << endl; exit(TOO_MANY_EXITS); } Found_Exits = true; // we have an exit! // no break statement: fall thru and increment cols case 'X': cols++; //# of cells is the same as # of cols temp_q.pop(); break; default: cerr << select_error_message(INCORRECT_DATA_IN_FILE) << "calcSize()" << endl; exit(INCORRECT_DATA_IN_FILE); } } // Get the # of Rows rows++; // Increment rows to count the first line we just got. do{ while(!temp_q.empty() && temp_q.front() != '\n'){ switch(temp_q.front()){ // check for valid characters. case 'e': if(Found_Exits){ cerr << select_error_message(TOO_MANY_EXITS) << "calcSize()" << endl; exit(TOO_MANY_EXITS); } Found_Exits = true; // we have an exit! temp_q.pop(); break; case 's': if(Found_Player){ cerr << select_error_message(TOO_MANY_PLAYERS) << "calcSize()" << endl; exit(TOO_MANY_PLAYERS); } Found_Player = true; temp_q.pop(); break; case 'm': if(Found_Player){ cerr << select_error_message(TOO_MANY_MINOTAURS) << "calcSize()" << endl; exit(TOO_MANY_MINOTAURS); } Found_Minotaur = true; temp_q.pop(); break; case 'X': case ' ': case 'l': case 'd': case 'u': temp_q.pop(); break; default: cerr << select_error_message(INCORRECT_DATA_IN_FILE) << "calcSize()" << endl; exit(INCORRECT_DATA_IN_FILE); } } rows++; //increment rows. if(!temp_q.empty()) // if temp_q is not empty that means we have a '\n' temp_q.pop(); // pop() the '\n' }while(!temp_q.empty() && temp_q.front() != '\n'); } void Lab::buildMaze() { calcSize(); //get the counts for the maze we are about to build. bool e=false; //exit indicator char l = '\0'; // ladder indicator: u, d, or l bool Wall = false; //switch to denote a wall. size_t r_count=0, c_count=0; // NOW we can build from of maze_q. // pop() until we get an 'X'. First time in we will skip this step. while(!maze_q.empty() && maze_q.front()=='\n') maze_q.pop(); // Allocate the rows Maze = new (Cell**)[rows]; // Allocate the rows r_count=0; do{ c_count = 0; Maze[r_count] = new (Cell*)[cols]; while(!maze_q.empty() && maze_q.front() != '\n'){ switch(maze_q.front()){ case 'X': Wall=true; break; case ' ': case 's':// **** SAVE THESE COORDINATES **** case 'm':// **** SAVE THESE COORDINATES **** break; case 'e': e=true; break; case 'l': case 'd': case 'u': l=maze_q.front(); break; default: cerr << select_error_message(INCORRECT_DATA_IN_FILE) << "buildMaze()" << endl; exit(INCORRECT_DATA_IN_FILE); //hopefully we will not have bad data here. } if(Wall) Maze[r_count][c_count] = NULL; else Maze[r_count][c_count] = new Cell(l, e);//(ladder, exit) maze_q.pop(); e=false; // re-initialize these values. l='\0'; Wall=false; c_count++; } if(!maze_q.empty()) // if maze_q is not empty that means we have a '\n' maze_q.pop(); // pop() the '\n' r_count++; }while(!maze_q.empty() && maze_q.front() != '\n'); //if not empty and not '\n' that means we are still processing the maze. Labyrinth.push_back(Maze); //now check to see if we still have more mazes to build // pop() until we get an 'X'. This will skip anything on those lines. while(!maze_q.empty() && maze_q.front()=='\n') maze_q.pop(); if(!maze_q.empty()) buildMaze(); }