Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ Maze Program

Reply
Thread Tools

C++ Maze Program

 
 
Roger Douglass
Guest
Posts: n/a
 
      06-29-2003
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);
}
}
}
}



 
Reply With Quote
 
 
 
 
Bruce
Guest
Posts: n/a
 
      06-29-2003
In comp.lang.c++
"Roger Douglass" <(E-Mail Removed)> wrote:

There are some maze algorithms in Snippets.

>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);
> }
> }
>}
>}
>
>
>


 
Reply With Quote
 
 
 
 
marko djogatovic
Guest
Posts: n/a
 
      07-02-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Rat in a maze problem NK C++ 4 06-29-2007 03:31 PM
design patterns - Maze examples yb C++ 3 12-09-2005 06:50 PM
Question about storing a maze board(like 2d array) into Tree. Daniel C Programming 4 11-22-2004 12:42 AM
Lost in "Date" maze Tim Slattery Java 7 01-19-2004 01:51 PM
Making a maze.... Bernard Fields Python 22 11-25-2003 03:12 AM



Advertisments