I've been working on a C++ maze assignment but I've become stuck -- below is the code I have so far. I'm trying to get it to print the unsolved maze (read from a txt file) and then the solved maze.
Any input on what I need to do to get it to print the solved maze would be appreciated...thanks.
#include<stdio.h>
#include<iostream>
#include <cstdlib>
#include<ctime>
#define FALSE 0
#define TRUE 1
usingnamespace std;
constint SIZE=12;
class Maze {
private:
char M[SIZE][SIZE]; // array of random numbers
int size; // size of the array
public:
Maze(); // constructor
void read_maze();
int maze_traverse(int row, int col);
void display_maze();
};
int main() {
int i, j;
Maze M;
M.display_maze();
M.maze_traverse(2,0);
M.display_maze();
}
// constructor
Maze::Maze() {
read_maze();
}
// constructor
void Maze::read_maze() {
int i, j;
FILE* ifp;
ifp=fopen("maze.txt", "r");
for(i=0; i< SIZE; i++)
for(j=0; j< SIZE; j++)
fscanf(ifp, " %c ", &M[i][j]);
}
int Maze::maze_traverse(int row, int col) {
// return TRUE if at the end of the maze
if (M[row][col] == 'E') return TRUE;
// return FALSE if not at the end of the maze
if (M[row][col] > SIZE-1 || M[row][col] == 'S') return FALSE;
// mark + for part of the path
M[row][col] = '+';
// return TRUE if the path goes to the North
if (M[row-1][col] == TRUE) return TRUE;
// return TRUE if the path goes to the South
if (M[row+1][col] == TRUE) return TRUE;
// return TRUE if the path goes to the East
if (M[row][col+1] == TRUE) return TRUE;
// return TRUE if the path goes to the West
if (M[row][col-1] == TRUE) return TRUE;
// mark x for the wrong path
M[row][col] = 'x';
return FALSE;
}
void Maze::display_maze(){
int i, j;
for(i=0; i<SIZE; i++) {
for(j=0; j< SIZE; j++)
printf("%c ", M[i][j]);
cout << endl;
}
cout << endl;
}
/*
// Pause the console so it doesn't close (when running in Windows)
cout << endl << endl << endl << endl << endl;
system("pause");
*/
You will need a lot more code if you want to know if a move is valid and where that path leads.
First check if a move is valid by comparing it the '.' char. If you want to know which branch they are taking you could insert a special char at each branch so when they reach it you can determine which path they will take.