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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
int GetInteger(char* pszPrompt, int nLow, int nHigh);//function declaration
class Coordinate
{
private:
int nRow;
int nCol;
public:
Coordinate(int r=0, int c=0) : nRow(r), nCol(c) { }
void SetPosition(int nR,int nC)
{
nRow = nR;
nCol = nC;
}
int GetRow()
{
return nRow;
}
int GetCol()
{
return nCol;
}
bool operator==(Coordinate& rcRHS)
{
return nRow==rcRHS.nRow && nCol == rcRHS.nCol;
}
};
class HeadOfPathStack;
class PathStack
{
friend class HeadOfPathStack;
private:
Coordinate pLocation;
PathStack* ppsNext;
public:
PathStack()
{
ppsNext = NULL;
}
PathStack(Coordinate& pnewLocation)
{
pLocation = rpNewLocation;
ppsNext = NULL;
}
};
class HeadOfPathStack
{
private:
PathStack* ppsHead;
int nNumberOfLocation;
public:
HeadOfPathStack()
{
ppsHead = NULL;
nNumberOfLocation = 0;
}
~HeadOfPathStack()
{
DeletePathStack();
}
void DeletePathStack();
bool Empty()
{
return nNumberOfLocation == 0 ? true:false;
}
bool Full();
bool Pop(Coordinate& rpNewLocation);
bool Push(Coordinate pNewLocation);
bool Top(Coordinate& rpNewLocation);
};
void HeadOfPathStack::DeletePathStack()
{
while(ppsHead != NULL)
{
PathStack* ppsToDelete = ppsHead;
ppsHead = ppsHead->ppsNext;
delete ppsToDelete;
}
nNumberOfLocation = 0;
}
bool HeadOfPathStack::Full()
{
PathStack* ppsTemp;
ppsTemp = new PathStack();
if(ppsTemp != NULL)
{
delete ppsTemp;
return false;
}
return true;
}
bool HeadOfPathStack::Pop(Coordinate& rpNewLocation)
{
if(!Empty())
{
PathStack* ppsCurrSpot = ppsHead;
rpNewLocation = ppsCurrSpot->pLocation;
ppsHead = ppsHead->ppsNext;
nNumberOfLocation--;
delete ppsCurrSpot;
return true;
}
else
{
return false;
}
}
bool HeadOfPathStack::Push(Coordinate pNewLocation)
{
if(!Full()) // if stack not full
{
PathStack* ppsCurrSpot = new PathStack(pNewLocation); //set new stack location
ppsCurrSpot->ppsNext = ppsHead; //connect last location to new one
ppsHead = ppsCurrSpot; //set new head of the stack
nNumberOfLocation++; //increment location on the stack
return true;
}
else //if stack is full
{
std::cout << "Stack full!" << std::endl;
return false;
}
}
bool HeadOfPathStack::Top(Coordinate& rpNewLocation)
{
if(!Empty()) //if stack not empty
{
rpNewLocation = ppsHead->pLocation; //get top location
return true;
}
else
{
return false;
}
}
class Maze
{
private:
int nCols, nRows;
int** papanMaze;
HeadOfPathStack hpsPath; //stack to hold mouse path locations
HeadOfPathStack hpsBranch; //stack to hold locations of intersections
public:
Maze(): nCols(0),nRows(0) {} //constructor
~Maze(); // destructor
void FindPath();
void DisplayMaze();
};
Maze::~Maze()
{
for(int i = 0; i<nRows; i++)
{
delete [] papanMaze[i]; //delete rows
}
delete [] papanMaze; //delete array of pointers to rows(arrays of int)
}
void Maze(char maze[][Cols], int *xPtr, int *yPtr)
{
int a, x, y, entry;
do
{
entry = rand() % 4;
exit = rand() % 4;
}
while (entry == exit);
// Determine entry position
if (entry == 0)
{
*xPtr = 1 + rand() % (ROWS - 2); // avoid corners
*yPtr = 0;
maze[*xPtr][*yPtr] = '.';
}
else if (entry == 1)
{
*xPtr = 0;
*yPtr = 1 + rand() % (COLS - 2);
maze[*xPtr][*yPtr] = '.';
}
else if (entry == 2)
{
*xPtr = 1 + rand() % (nRows - 2);
*yPtr = nCols - 1;
maze[*xPtr][*yPtr] = '.';
}
else
{
*xPtr = nRows - 1;
*yPtr = 1 + rand() % (nCols - 2);
maze[*xPtr][*yPtr] = '.';
}
}
|