#include #include #include #include "maze.h" static void PrintUsage() { cout << endl; cout << "Command Summary" << endl; cout << "---------------" << endl; cout << " (h)elp" << endl; cout << " (q)uit" << endl; cout << endl; cout << " (m)aze of size" << endl; cout << " (p)osition in maze" << endl; cout << " (g)oal position" << endl; cout << endl; cout << " (N)orth look" << endl; cout << " (S)outh look" << endl; cout << " (E)ast look" << endl; cout << " (W)est look" << endl; cout << endl; cout << " (n)orth move" << endl; cout << " (s)orth move" << endl; cout << " (e)orth move" << endl; cout << " (w)orth move" << endl; cout << endl; cout << " (j)ump -- must also specify row, col coordinates" << endl; cout << endl; cout << " (v)iew maze" << endl; cout << " (d)ump maze (debug print view)" << endl; cout << endl; } static void PrintPosition(int row, int col) { cout << "(" << row << "," << col << ")"; } static void ProcessLook(Maze& mymaze,char look) { direction dir; status stat; switch (look) { case 'N': dir = north; break; case 'S': dir = south; break; case 'E': dir = east; break; case 'W': dir = west; break; } stat = mymaze.Look(dir); switch (stat) { case POS_BLOCKED: cout << "That direction is blocked" << endl; break; case POS_CLEAR: cout << "That direction is clear" << endl; break; case POS_GOAL: cout << "That direction is clear (and contains the goal)" << endl; break; } } static void ProcessMove(Maze& mymaze,char move) { direction dir; status stat; switch (move) { case 'n': dir = north; break; case 's': dir = south; break; case 'e': dir = east; break; case 'w': dir = west; break; } stat = mymaze.Move(dir); switch (stat) { case POS_BLOCKED: cout << "Cannot move in that direction" << endl; break; case POS_CLEAR: cout << "Moved successfully" << endl; break; case POS_GOAL: cout << "Moved successfully (to the goal)" << endl; break; } } static void ProcessJump(Maze& mymaze) { int row,col; status stat; cin >> row >> col; stat = mymaze.Jump(row,col); switch (stat) { case POS_UNVISITED: cout << "You've never been to "; PrintPosition(row,col); cout << " before" << endl; break; case POS_BLOCKED: cout << "That position contains a wall" << endl; break; case POS_CLEAR: cout << "Successful jump" << endl; break; case POS_GOAL: cout << "Successful jump (to the exit)" << endl; break; } } static void RunMaze(char* progname, char* filename) { Maze mymaze(filename); char command; int row,col; cout << "Type 'h' for help" << endl; do { cout << progname << "> "; cin >> command; switch (command) { case 'h': PrintUsage(); break; case 'q': break; case 'm': mymaze.Size(row,col); cout << "Maze size = " << row << " x " << col << endl; break; case 'p': mymaze.Position(row,col); cout << "Current position = "; PrintPosition(row,col); cout << endl; break; case 'g': mymaze.Goal(row,col); cout << "Goal position = "; PrintPosition(row,col); cout << endl; break; case 'N': case 'S': case 'E': case 'W': ProcessLook(mymaze,command); break; case 'n': case 's': case 'e': case 'w': ProcessMove(mymaze,command); break; case 'j': ProcessJump(mymaze); break; case 'v': cout << endl; mymaze.Print(); cout << endl; break; case 'd': cout << endl; mymaze.Print(0); cout << endl; break; default: cerr << "Unrecognized command: '" << command << "' (" << (int)command << ")" << endl; break; } } while (command != 'q'); } void main(int argc, char* argv[]) { char filename[80]; cout << "Enter maze filename: "; cin >> filename; RunMaze(argv[0],filename); }