/* * Maze1.cpp * CIS 250 * David Klick * 2014-03-12 * * This program creates a maze. * Note: Leave the commented program statements in for later inclusion * in the maze-solving version of this program. * */ #include "Board.h" #include "Cell.h" #include "Direction.h" #include #include #include #include #include #include #include #include using std::boolalpha; using std::cin; using std::cout; using std::stack; using std::vector; void cont(); bool equalsIgnoreCase(const char *s1, const char *s2); bool allCellsVisited(Board& b, int rows, int cols); bool allCellsReachable(Board& b, int rows, int cols); bool allBoundariesCorrect(Board& b, int rows, int cols); int main(int argc, char* argv[]) { int ROWS = 20; int COLS = 20; srand(time(NULL)); // Cell* startCell; // not used in creating maze // Cell* endCell; // not used in creating maze if (argc > 3 || (argc == 2 && equalsIgnoreCase(argv[0], "-h"))) { cout << "Usage: " << argv[0] << " [rows [columns]]"; return 1; } if (argc > 2) COLS = atoi(argv[2]); if (argc > 1) ROWS = atoi(argv[1]); Board b(ROWS, COLS); // ***** ***** // ***** this section must be implemented ***** // ***** ***** cout << "All cells on board visited: " << boolalpha << allCellsVisited(b, ROWS, COLS) << '\n'; cout << "All boundaries correct: " << boolalpha << allBoundariesCorrect(b, ROWS, COLS) << '\n'; cout << "All cells reachable: " << boolalpha << allCellsReachable(b, ROWS, COLS) << '\n'; b.setStartCell(b.getCell(0, rand() % COLS)); b.setEndCell(b.getCell(ROWS-1, rand() % COLS)); cout << b << '\n'; } bool equalsIgnoreCase(const char *s1, const char *s2) { char *p; char *lcs1 = new char(strlen(s1)+1); char *lcs2 = new char(strlen(s2)+1); strcpy(lcs1, s1); strcpy(lcs2, s2); for (p=lcs1; *p!='\0'; p++) *p = tolower(*p); for (p=lcs2; *p!='\0'; p++) *p = tolower(*p); return strcmp(lcs1, lcs2) == 0; } /* * Method used to test a maze to make sure all cells * have been visited. * */ bool allCellsVisited(Board& b, int rows, int cols) { for (int r=0; risVisited()) return false; } } return true; } /* * Method used to test a maze to make sure all edges * on boundaries exist. * */ bool allBoundariesCorrect(Board& b, int rows, int cols) { for (int r=0; rhasEdge(WEST) || !b.getCell(r,0)->hasWall(WEST) || !b.getCell(r,cols-1)->hasEdge(EAST) || !b.getCell(r,cols-1)->hasWall(EAST)) { cout << b.getCell(r,0) << '\n'; cout << b.getCell(r,cols-1) << '\n'; return false; } } for (int c=0; chasEdge(NORTH) || !b.getCell(0,c)->hasWall(NORTH) || !b.getCell(rows-1,c)->hasEdge(SOUTH) || !b.getCell(rows-1,c)->hasWall(SOUTH)) { cout << '*' << c << "*\n"; return false; } } return true; } /* * Method used to test a maze to make sure all cells * are reachable. * */ bool allCellsReachable(Board& b, int rows, int cols) { int r, c; // Set all cells to a unique value (starting with 1) and unvisit them b.clearVisited(); for (r=0; rsetValue(r*cols+c+1); } } // Now keep reducing connected cells to lowest value. // If all are connected, then all cells should end up as 1. bool changed = true, modify = false; vector lst; int min, v = 0, i; while (changed) { changed = false; for (r=0; rgetValue(); for (i=0; i<(int)lst.size(); i++) { v = b.getAdjacentCell(cell, lst.at(i))->getValue(); if (v < min) { modify = true; min = v; } } // Change values in connected cells if needed if (modify) { changed = true; cell->setValue(min); for (i=0; i<(int)lst.size(); i++) { b.getAdjacentCell(cell, lst.at(i))->setValue(min); } } } } } // Now find out if all cells have value 1 (all connected) for (r=0; rgetValue() != 1) return false; } } return true; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }