My project requires I use the right-hand method of solving a maze. I have to use a stack to keep track of and eventually print out the path to the exit. I know the way I am trying to keep track of each point is wrong because using mazeArray[xCoord][yCoord] is only going to give me the values at that position in the 2d array. So I feel like I need to use a struct that contains a x position and y position, and maybe instead of using another ADT I could use a bool within each struct to mark if the position was visited or not. I guess I am having a brain fart on how to implement said struct within my project. Any help would be greatly appreciated. Below is all code I have written.
#ifndef MAZE_H
#define MAZE_H
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
using namespace std;
enum dir {E,S,W,N};
#define MAZE_SIZE 12
class Maze
{
private:
int xCoord;
int yCoord;
int direction;
bool visited;
bool exitFound;
char mazeArray[MAZE_SIZE][MAZE_SIZE];
char currentPos;
stack<char> path;
stack<char> visited;
queue<char> choices;
public:
Maze();
void solveMaze();
void turnRight();
void turnLeft();
void explore();
void printMaze();
void printPath();
};
#endif
#include "maze.h"
Maze::Maze()
{
exitFound = false;
direction = E;
//read file variable
ifstream loadFile;
//Open maze txt file
loadFile.open("maze.txt");
//check to see if the file was opened
if(loadFile.is_open()){
//A loop that created the maze
for (int x = 0; x < 12; x++){
for(int y = 0; y < 12; y++){
if ((y == 0 || y == 11)||(x == 0 || x == 11))
mazeArray[x][y] = '1';
//replace 0 with integers from txt
else
loadFile >> mazeArray[x][y];
}
}
//closing the text file
loadFile.close();
}
else{
cout << "File not found\n";
}
cout << "Please select your starting location\n"
<< "x-coordinate: ";
cin >> xCoord;
cout << "y-coordinate: ";
cin >> yCoord;
currentPos = mazeArray[xCoord][yCoord];
path.push(currentPos);
visited.push(currentPos);
}
void Maze::explore()
{
if(direction == E){
//Examining North
if(mazeArray[xCoord][yCoord-1] = '0')
path.push(mazeArray[xCoord][yCoord-1]);
//Examining East
if(mazeArray[xCoord+1][yCoord] = '0')
path.push(mazeArray[xCoord+1][yCoord]);
//Examining South
if(mazeArray[xCoord][yCoord+1] = '0')
path.push(mazeArray[xCoord][yCoord+1]);
}
else if(direction == S){
//Examining East
if(mazeArray[xCoord+1][yCoord] = '0')
path.push(mazeArray[xCoord+1][yCoord]);
//Examining South
if(mazeArray[xCoord][yCoord+1] = '0')
path.push(mazeArray[xCoord][yCoord+1]);
//Examining West
if(mazeArray[xCoord-1][yCoord] = '0')
path.push(mazeArray[xCoord-1][yCoord]);
//if right hand rule carries you forward
//yCoord = (yCoord + 1);
}
else if(direction == W){
//Examining South
if(mazeArray[xCoord][yCoord+1] = '0')
path.pursh(mazeArray[xCoord][yCoord+1]);
//Examining West
if(mazeArray[xCoord-1][yCoord] = '0')
path.push(mazeArray[xCoord-1][yCoord]);
//Examining North
if(mazeArray[xCoord][yCoord-1] = '0')
path.push(mazeArray[xCoord][yCoord-1]);
}
else if(direction == N){
//Examining West
if(mazeArray[xCoord-1][yCoord] = '0')
path.push(mazeArray[xCoord-1][yCoord]);
//Examining North
if(mazeArray[xCoord][yCoord-1] = '0')
path.push(mazeArray[xCoord][yCoord-1]);
//Examining East
if(mazeArray[xCoord+1][yCoord] = '0')
path.push(mazeArray[xCoord+1][yCoord]);
//Traveling East
if (mazeArray[xCoord+1][yCoord]='0'){
xCoord+=1;
currentPos = mazeArray[xCoord][yCoord];
visited.push(currentPos);
direction=E;
explore();
}
//Continuing North
else if(mazeArray[xCoord][yCoord-1]=='0'){
yCoord-=1;
currentPos=mazeArray[xCoord][yCoord];
visited.push(currentPos);
direction=N;
explore();
}
//Travelling West
else if(mazeArray[xCoord-1][yCoord]=='0'){
xCoord-=1;
currentPos = mazeArray[xCoord][yCoord];
visited.push(currentPos);
direction==W;
explore();
}
//If have to go back the way came
//This definately does not work
else{
//loop through visited stack until empty
for(visited.top(); visited.empty(); visited.pop())
path.pop();
}
}
}
void Maze::printMaze()
{
for (int j = 0; j < 12; j++) {
for (int i = 0; i < 12; i++) {
cout << mazeArray[i][j];
}
cout << endl;
}
}
void Maze::printPath()
{
}