I need help with C++ I not able to finish the maze.
My code
#include
#include
#include
class Maze {
private:
int xAxis, yAxis;
char maze[35][35];
int facing;
public:
Maze(std::string filename);
void printMaze();
int MazeTR(int xAxis, int yAxis, int facing);
int getXAxis() { return xAxis; }
int getYAxis() { return yAxis; }
};
Maze::Maze(std::string filename) {
std::ifstream inFile;
inFile.open("C:/Users/hamza/Downloads/Maze/maze.txt");
if (!inFile) {
std::cout << "Unable to open file" << std::endl;
}
inFile >> xAxis;
inFile >> yAxis;
int x, y;
while (inFile >> x >> y) {
inFile >> maze[x][y];
}
inFile.close();
}
void Maze::printMaze() {
for (int x = 0; x < 35; ++x ){
for (int y = 0; y < 35; ++y)
if ((x == xAxis) && (y == yAxis))
std::cout << 'S' << ' ';
else
std::cout << maze[x][y] << ' ';
std::cout << '\n';
}
std::cout << "\nfind your way\n";
std::cin.get();
}
int Maze::MazeTR(int xAxis, int yAxis, int facing) {
int SU = 0;
//this is a mark where we have been
maze[xAxis][yAxis] = '0';
printMaze();
while (SU == 0){
if((xAxis == 4) && (yAxis == 11)){
SU = 1;
}
else if (facing == 0){
if (maze[xAxis][yAxis+1] == 'O'){
//RIGHT
SU = MazeTR(xAxis, yAxis+1, 1);
}
//look up
else if (maze[xAxis-1][yAxis] == 'O'){
//valid move up than
SU = MazeTR(xAxis-1, yAxis, 0);
}
else if (maze[xAxis][yAxis-1] == 'O'){
//Move left
SU = MazeTR(xAxis, yAxis-1, 3);
}
else
return 0;
}
else if (facing == 1){
//check below when facing right
if (maze[xAxis+1][yAxis] == 'O'){
//go dpwm
SU = MazeTR(xAxis+1, yAxis, 2);
}
else if (maze[xAxis][yAxis+1] == 'O'){
//Move right
SU = MazeTR(xAxis, yAxis+1, 1);
}
else if (maze[xAxis-1][yAxis] == 'O'){
//move up
SU = MazeTR(xAxis-1, yAxis, 0);
}
else
return 0;
}
else if (facing == 2){
//check for vailded left move
if (maze[xAxis][yAxis-1] == 'O'){
SU = MazeTR(xAxis, yAxis-1, 3); // Move to the left.
}
else if (maze[xAxis+1][yAxis] == 'O'){
//checking dowm if their is a place go down
SU = MazeTR(xAxis+1, yAxis, 2);
}
else if (maze[xAxis][yAxis+1] == 'O'){
//move right if their space
SU = MazeTR(xAxis, yAxis+1, 1);
}
else
return 0;
}
else if (facing == 3){
//seeing if theirs room above
if (maze[xAxis-1][yAxis] == 'O'){
//Move up
SU = MazeTR(xAxis-1, yAxis, 0);
}
else if (maze[xAxis][yAxis-1] == 'O'){
//Move Left
SU = MazeTR(xAxis, yAxis-1, 3);
}
else if (maze[xAxis+1][yAxis] == 'O'){
//Move Doward
SU = MazeTR(xAxis+1, yAxis, 2);
}
else
return 0;
}
}
// Return value of success.
return SU;
}
int main() {
Maze maze("maze.txt");
std::cout << "Maze created\n";
//when its success it will make it go through the maze
//SU = success
int SU = 0;
// time to move through the maze
SU = maze.MazeTR(maze.getXAxis(), maze.getYAxis(), 1);
if (SU == 1)
std::cout << "The maze has been completed thank you for playing.\n";
else
std::cout << "Error.\n";
return 0;
}
/*
Additional information Instruction
Create a class called "Maze" that includes a recursive call to find a path through a given text maze. The first 2 lines in the text file represent the size of the maze and the the next 2 lines represent the starting location in the maze. A sample text file is attached.
I will test your program on a similar text file. maze.txt Click for more options
++++++++++S++++++++++++++++++++++++
++++++++++OOOOOOOOOOOOOOOOOOOOOOOOO
++++++++++O++++++++++++++++++O+++++
OOOOOOOOOOOOOOOOOOOOOOOOOO+++O++OOE
O+++++++++O++++++++++++++O+++O++O++
OOOOOO++++O++++++++++++++O+++O++O++
O++++O++++OOOOOOOOOOO++++O+OOO++O++
O++++O++++O+++++++++OOOO+O+++O++O++
OOO++OOOO+OOOOOO+++++++++++OOO++OOO
O+O+++++O++++++OOOOOOOOOO++O++++++O
O+OOOO++O++++++O++++++++O+++OOO+++O
O++++O++OOOOOOOO++++++++O+++O+O+++O
OOO++O++++++++++++++++++OOOOO+O+++O
++O++OOOOOOOOOOOOOOOO+++++++++OO++O
OOO+++++++++++++++++OOOOOO++++++++O
O++++++++++++++++++++++++O++OOOOOOO
+++++++++++++++++++++++++O++O++++++
OOOOOOOOOOOOOOOOOOOOOOOOOO++OOOOO++
O++++++++++++++++++++++++O++++++O++
OOOOOOO+++++++++++++++OOOOOOO+++O++
++++++++++++++++++++++O+++++OO++O++
OOOOOOOOOOOOOOOOOOOOOOO++++++O++O++
O++++++++++++++++++++++++++++O+OOOO
OOOO++++++++++++++++++++OOOOOO+O+++
+++OOOOOOOOO+++++++++++++++++++O+++
+++++O+++++OOOOOOOOOO++++++++OOO+++
+O+++OOOOO++++++O++++++++++++O+++++
+OOOOO+++O++++++OOOOOO+++++++O+++++
+++++++++++++++++++++OOOOOOOOO+++++
OOOOOOOOOOOOOOOOOOOOOO+++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++
1.) You will first have to set up tp read in the maze text file.
2.) You must dynamically create a 2-dimensional character array.
3.) Write a recursive method to "find your way" through the maze. (Each recursive call will represent one move in the maze)
*/