HELP! Mouse maze

I am currently working on a group project ,and we are stumped on how to implement a mouse that can move through the maze we have coded. The goal is to create a mouse that can traverse the maze and find the cheese with senses like sight and smell. The instructions say that we must use stacks.
For this lab, you should work in groups of two or three students!
Using the Stack class, write a complete program for a mouse to find at least one piece of cheese in a
maze with possible backtracking for dead-ends given the starting position of the mouse and a block
of cheese (or blocks of cheese) which, of course, doesn't move.


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
#include <cstring>
#include <string>
#include <array>
#include <sstream>
#include<iostream>
#include<fstream>
#include<ostream>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
//Michael Geter
//Data Structures
//Lab 5
// Professor Whitston
using namespace std;

enum Direction { DOWN, RIGHT, UP, LEFT };
const int ROWS = 200, COLS = 200;

void mazeTraversal(char[][COLS], const int, const int, int, int, int);
void mazeGenerator(char[][COLS], int *, int *);
void printMaze(const char[][COLS]);
bool validMove(const char[][COLS], int, int);
bool coordsAreEdge(int, int);
const char mouse = 'M';
const char cat = 'C';

using namespace std;

int main()
{
	char maze[ROWS][COLS];
	int xStart, yStart, x, y;



	for (int loop = 0; loop < ROWS; ++loop)
	for (int loop2 = 0; loop2 < COLS; ++loop2)
		maze[loop][loop2] = '|';

	mazeGenerator(maze, &xStart, &yStart);

	x = xStart;  // starting row
	y = yStart;  // starting col

	mazeTraversal(maze, xStart, yStart, x, y, RIGHT);
	return 0;
}

// Assume that there is exactly 1 entrance and exactly 1 exit to the maze.
void mazeTraversal(char maze[][COLS], const int xCoord, const int yCoord,
	int row, int col, int direction)
{
	static bool flag = false;   // starting position flag

	maze[row][col] = 'x';  // insert x at current location
	printMaze(maze);

	if (coordsAreEdge(row, col) && row != xCoord && col != yCoord) {
		cout << endl << "Maze successfully exited!\n\n";
		return;   // maze is complete
	}
	else if (row == xCoord && col == yCoord && flag) {
		cout << "\nArrived back at the starting location.\n\n";
		return;
	}
	else {
		flag = true;

		for (int move = direction, count = 0; count < 4;
			++count, ++move, move %= 4)
			switch (move) {
			case DOWN:
				if (validMove(maze, row + 1, col)) { // move down
					mazeTraversal(maze, xCoord, yCoord, row + 1, col, LEFT);
					return;
				}
				break;
			case RIGHT:
				if (validMove(maze, row, col + 1)) { // move right
					mazeTraversal(maze, xCoord, yCoord, row, col + 1, DOWN);
					return;
				}
				break;
			case UP:
				if (validMove(maze, row - 1, col)) { // move up
					mazeTraversal(maze, xCoord, yCoord, row - 1, col, RIGHT);
					return;
				}
				break;
			case LEFT:
				if (validMove(maze, row, col - 1)) { // move left
					mazeTraversal(maze, xCoord, yCoord, row, col - 1, UP);
					return;
				}
				break;
		}
	}
}

bool validMove(const char maze[][COLS], int r, int c)
{
	return (r >= 0 && r <= ROWS - 1 && c >= 0 && c <= COLS - 1 &&
		maze[r][c] != '#');  // a valid move
}

bool coordsAreEdge(int x, int y)
{
	if ((x == 0 || x == ROWS - 1) && (y >= 0 && y <= COLS - 1))
		return true;
	else if ((y == 0 || y == COLS - 1) && (x >= 0 && x <= ROWS - 1))
		return true;
	else
		return false;
}

void printMaze(const char maze[][COLS])
{
	for (int x = 0; x < ROWS; ++x) {

		for (int y = 0; y < COLS; ++y)
			cout << maze[x][y] << ' ';

		cout << '\n';
	}

	cout << "\nHit return to see next move";
	cin.get();
}

void mazeGenerator(char maze[][COLS], int *xPtr, int *yPtr)
{
	int a, x, y, entry, exit;

	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() % (ROWS - 2);
		*yPtr = COLS - 1;
		maze[*xPtr][*yPtr] = '.';
	}
	else {
		*xPtr = ROWS - 1;
		*yPtr = 1 + rand() % (COLS - 2);
		maze[*xPtr][*yPtr] = '.';
	}

	// Determine exit location
	if (exit == 0) {
		a = 1 + rand() % (ROWS - 2);
		maze[a][0] = '.';
	}
	else if (exit == 1) {
		a = 1 + rand() % (COLS - 2);
		maze[0][a] = '.';
	}
	else if (exit == 2) {
		a = 1 + rand() % (ROWS - 2);
		maze[a][COLS - 1] = '.';
	}
	else {
		a = 1 + rand() % (COLS - 2);
		maze[ROWS - 1][a] = '.';
	}

	for (int loop = 1; loop < (ROWS - 2) * (COLS - 2); ++loop) {
		x = 1 + rand() % (ROWS - 2);    // add dots to maze
		y = 1 + rand() % (COLS - 2);
		maze[x][y] = '.';
	}
}
I have to get back to work so I will give you the short answer for now. The main point of the stack is to allow backtracking and to make sure you don't go to places you have already been.

If you use a specific order to the direction choice, you can traverse the maze using the stack. You push each choice you make (each spot you visit) onto the stack when it is made. If you run into a dead end, you pop nodes off the stack until you find a choice you haven't made yet. If you don't have a specific node type you are required to use, you can keep a stack of nodes in each node (so that you can keep track of choices already made at each point in the maze). There are probably other ways to do this as well. This is just the first that came off the top of my head.
Topic archived. No new replies allowed.