C++ Maze Program

R

Roger Douglass

I've been working on this program for school and I just about got it before
I had to turn it in.
Here's my final code on it. I didn't finish
it, but I think I'm pretty close. M is the starting point in the maze and W
the finish. Using recursion the program is to leave a trail of asterisks
(*) showing the solution.

Well, I got stumped on this one. I found something similar to it online,
but not quite what I was looking for.

Here's the code. Maybe someone out there can improve on this.

Roger

P.S. This compiles okay, it just doesn't do what it's supposed to do.

#include <iostream>
using namespace std;

int col = 1;
int row = 2;
const ROWMAX = 11;
const COLMAX = 16;

char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B','B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B','B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'}
};
void printMaze(char[]);
void runMaze(char *, int, int);

int main()
{
char * pt = new char;
pt = &maze[ROWMAX + 1][COLMAX + 1];

cout << "Maze before solution:\n";
printMaze(pt);
cout << "Maze after solution:\n";
runMaze(pt, 1, 2);
delete pt;
return 0;
}
void printMaze(char[])
{
for(int row = 1; row <= ROWMAX; row++)
{
for(int col=1; col <= COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}
void runMaze(char * ptmaze, int row, int col)
{
if(1 <= row && row <= ROWMAX && 1 <= col && col <= COLMAX)
{
if(' ' == maze[row][col])
{
maze[row][col] = '*';
if(row == 1 || row == ROWMAX || col == 1 || col == COLMAX)
printMaze(ptmaze);

else
{
runMaze(ptmaze, row - 1, col);
runMaze(ptmaze, row, col + 1);
runMaze(ptmaze, row + 1, col);
runMaze(ptmaze, row, col - 1);
}
}
}
}
 
M

marko djogatovic

Try this!

#include <iostream>
using namespace std;

int col = 1;
int row = 2;
const int ROWMAX = 11;
const int COLMAX = 16;

char maze[ROWMAX][COLMAX] =
{
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'},
{'B','M',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B',' ','B','B','B',' ','B'},
{'B','W',' ',' ',' ',' ',' ',' ',' ','B',' ','B',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B',' ','B',' ','B'},
{'B',' ',' ',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B',' ','B'},
{'B','B','B','B','B','B','B','B',' ','B','B','B','B','B',' ','B'},
{'B',' ',' ',' ','B',' ',' ',' ',' ','B',' ',' ',' ',' ',' ','B'},
{'B',' ',' ',' ',' ',' ',' ',' ','B','B','B','B',' ','B','B','B'},
{'B',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','B'},
{'B','B','B','B','B','B','B','B','B','B','B','B','B','B','B','B'}
};
void printMaze();
void runMaze(int, int);


void printMaze()
{
for(int row = 0; row < ROWMAX; row++)
{
for(int col=0; col < COLMAX; col++)
cout << maze[row][col];
cout << "\n";
}
}

void runMaze(int row, int col)
{
if( (row>0 && row<ROWMAX) && (col>0 && col<COLMAX)) {
if( maze[row][col] == 'W' ) return;

if( maze[row][col] == ' ') {
maze[row][col]='*';

runMaze(row, col+1);
runMaze(row, col-1);
runMaze(row-1, col);
runMaze(row+1, col);
}
}
}

int main()
{
cout << "Maze before solution:\n";
printMaze();
cout << "Maze after solution:\n";
runMaze(1, 2);
printMaze();
return 0;
}

Marko Djogatovic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top