1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#include <cstdlib>
#include <iomanip>
#include <string>
#include <iostream>
#include <fstream>
#include "Boolean.h"
#include "Stack.h"
#include "Maze.h"
using namespace std;
void Menu(Maze&);
void WalkThroughMaze(Maze&, int, int);
bool CheckExit(int, int);
int main(){
Maze maze;
maze.FillMatrix();
Menu(maze);
return 0;
}
void Menu(Maze &maze){
int start, exit, option;
bool cont = true;
while(cont){
cout<<"Welcome to Mouse in a Maze: \n"<<endl;
cout<<"Please select an option: \n";
cout<<"1) Begin Mouse in Maze game. \n";
cout<<"2) Exit program. \n";
cin>>option;
if (option = 1){
cout<<"Please enter desired starting point for the mouse -> \n";
cin>>start;
cout<<"Now enter desired exit point for the maze -> \n";
cin>>exit;
cout<<endl;
cout<<"Mouse going through maze: \n";
WalkThroughMaze(maze, start, exit);
cont = true;
}
else if (option = 0)
cont = false;
else{
cout<<"Incorrect input for option -- Try again \n";
cont = true;
}
}
}
void WalkThroughMaze (Maze &maze, int start, int exit){
Stack visitStack, backStack;
DATA mouse, next, decision;
bool exitFlag;
decision.row = -1; //Set the decision marker
mouse.row = start;
int k, j, i; //Counters used in walkthrough
visitStack.Push(mouse);
//Check to see if start position is the exit
exitFlag = CheckExit(mouse.row, exit);
while(!exitFlag AND !visitStack.IsEmpty()){
visitStack.StackTop(mouse); //Get top of the list value
if(mouse.row EQ -1){
//If the top of the visitStack is the decision
//marker, delete decision marker and go to that
//position received from backStack
visitStack.Pop(mouse);
backStack.Pop(mouse);
visitStack.Push(mouse);
maze.MarkVisited(mouse.row);
cout<<"*"<<mouse.row<<endl;
if(CheckExit(mouse.row, exit))
exitFlag = true;
}
else{
//Set counters to 0
k = 0;
j = 0;
while(maze.Neighbor(mouse.row, j)){
//Count the number of neighbors and
//push neighbors to backStack
next.row = j;
backStack.Push(next);
k++;
j++;
}
if(k = 1){
//If there is only one neighbor, move mouse
//to next position
mouse = next;
visitStack.Push(mouse);
backStack.Pop(mouse);
maze.MarkVisited(mouse.row);
cout<<mouse.row<<endl;
if(CheckExit(mouse.row, exit))
exitFlag = true;
}
else if (k GT 1){
//If there is more than one neighbor, push -1
//to visitStack
i = 0;
while(i LT k){
visitStack.Push(decision);
i++;
}
}
else{
//Position is not exit nor does it have neighbors
visitStack.Pop(mouse);
cout<<"*"<<mouse.row<<endl;
}
}
}
if(visitStack.IsEmpty()){
//Stack is empty and no exit was found
cout<<"No exit found, reward mouse with cheese anyway\n";
}
else{
//Exit has been found
cout<<"Mouse has found exit at position("<<mouse.row<<").\n";
cout<<"Reward mouse with cheese!\n";
}
}
bool CheckExit(int position, int exit){
bool success;
if (position EQ exit){
success = true;
}
else{
success = false;
}
return success;
}
|