C++ Maze Program

Discussion in 'C++' started by Roger Douglass, Jun 29, 2003.

  1. 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);
    }
    }
    }
    }
     
    Roger Douglass, Jun 29, 2003
    #1
    1. Advertising

  2. Roger Douglass

    Bruce Guest

    In comp.lang.c++
    "Roger Douglass" <> 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);
    > }
    > }
    >}
    >}
    >
    >
    >
     
    Bruce, Jun 29, 2003
    #2
    1. Advertising

  3. 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
     
    marko djogatovic, Jul 2, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Slattery

    Lost in "Date" maze

    Tim Slattery, Jan 16, 2004, in forum: Java
    Replies:
    7
    Views:
    1,099
    Thomas Schodt
    Jan 19, 2004
  2. Bernard Fields

    Making a maze....

    Bernard Fields, Nov 13, 2003, in forum: Python
    Replies:
    22
    Views:
    1,391
    Andrew Dalke
    Nov 25, 2003
  3. Daniel
    Replies:
    4
    Views:
    378
    Barry Schwarz
    Nov 22, 2004
  4. yb
    Replies:
    3
    Views:
    550
    Victor Bazarov
    Dec 9, 2005
  5. NK

    Rat in a maze problem

    NK, Jun 28, 2007, in forum: C++
    Replies:
    4
    Views:
    5,741
    Fei Liu
    Jun 29, 2007
Loading...

Share This Page