void Maze::loadMaze(char *mazefile) { ifstream mazein; int y = 0; mazein.open(mazefile); if (!mazein) { cout << "Unable to read from " << mazefile << "\n"; exit(0); } while (!mazein.eof()) { string line; Row row(y); getline(mazein, line); row.loadRow(line); rows.insert(row); y++; } rows.rebalance(); mazein.close(); }
void Row::loadRow(const string &s) { int xpos = 0; for (string::const_iterator itr = s.begin(); itr < s.end(); itr++) { Cell *cell = new Cell(xpos, *itr); cells.insert(*cell); xpos++; } }
void read_file(const string &filename, bintree &bt) { ifstream base(filename); if (!base) { cerr << "couldn't read " << filename << endl; exit(1); } string name, line; double lat, lon; while (getline(base, line)) { stringstream sline(line); sline >> lat >> lon >> ws; getline(sline, name); bt.insert(name, lon); } }
bool Maze::findMazePath(int x, int y) { Cell* cell; Row* row = rowAt(y); if (x < 0 || y < 0 || x >= row->size() || y >= rows.size()) { return false; } cell = cellAt(x, y); if (cell->isFinishPoint()) { cell->setInsidePath(true); return true; } else if (!cell->isInsideMaze() || cell->isInsidePath()) { return false; // Recursive case. // Attempt to find a path from each neighbour. // Tentatively mark cell as on path. } else { cell->setInsidePath(true); if (findMazePath(x - 1, y) || findMazePath(x + 1, y) || findMazePath(x, y - 1) || findMazePath(x, y + 1)) { return true; } else { cell->setInsidePath(false); return false; } } }
bool Maze::isValid() { int sumStartPoint = 0; int sumFinishPoint = 0; for (int y = 0; y < rows.size(); y++) { int x = 0; Row* row = rowAt(y); /* Checking errors on cells outside the maze (hasn't met its first wall) */ for (int i = 0; i < row->size() && !cellAt(i, y)->isWall(); i++) { Cell* cell = cellAt(i, y); cell->setInsideMaze(false); //Set cell as outside the maze if (!cell->isValid()) { cout << "Invalid character/s"; cout << endl; return false; } if (cell->isStartPoint()) { cout << "Error - start declared outside of maze\n"; return false; } if (cell->isFinishPoint()) { cout << "Error - finish declared outside of maze\n"; return false; } x++; // Keep x-coordinate moving } /* Checking invalid chars on cells inside the maze and counts on start and finish points */ for (; x < row->size(); x++) { Cell* cell = cellAt(x, y); if (!cell->isWall()) { cell->setInsideMaze(true); //Set cell as inside the maze cell->setInsidePath(false); //Set cell as inside the path } if (!cell->isValid()) { cout << "Invalid character/s" << endl; return false; } else if (cell->isStartPoint()) { cellStartX = x; //Store start cell coordinate cellStartY = y; sumStartPoint++; } else if (cell->isFinishPoint()) { sumFinishPoint++; } } } return (sumStartPointCheck(sumStartPoint) || sumFinishPointCheck(sumFinishPoint))? 1 : 0; }
Row* Maze::rowAt(int y) const { Row row(y); return rows.find(row); }
void Maze::printMaze() const { rows.print(); }
Cell* Row::cellAt(int x) const { Cell cell(x); return cells.find(cell); }
int Row::size() const { return cells.size(); }