solving a maze game


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: solving a maze game

  1. #1
    Join Date
    Mar 2006
    Posts
    24

    solving a maze game

    basically, my ques is the following(btw its a long ques )

    my program should read a maze description with 10 rows and 12 columns from the console input. An exampl input will be like this :

    0###########
    00#0000000000#
    #0####0###0#
    #0#00#000#0#
    #0#0####0#0#
    #0#00000#0#
    #0######0#0#
    #0#000##0#0#
    #000#0000#00
    ###########0

    in which '#' represents a wall that you cannot penetrate and '0' represents an empty space that you can move along. For example, the way to input the file to your program will be

    solveAMaze.exe < 3.maz

    Your program should first print out the maze like this:
    +----------+
    | ###########|
    | # #|
    |# #### ### #|
    |# # # # #|
    |# # #### # #|
    |## # #|
    |# ###### # #|
    |# # ## # #|
    |# # # |
    |###########|
    +-----------+

    (Add a border)

    You should compute a path that leads you from the entrance(row 0, column 0) to the exit(row9, column 11). If no such path exists, your program should be able to tell the user that maze has no exit. You can either use recursion or any other methods to solve.
    Your path should not have any duplicated locations, e.g this is not allowed:[0,0] [0,1] [0,0]....
    You do not have to compute the shortest path though.

    Instruction:
    1. You do not need to worry about the input part, it's done for you. Implement the output function printMaze();
    2. Inherit a child class from Maze called MarkableMaze in the files "markableMaze.h" and "markableMaze.cpp". This comtains the function of the class Maze plus abilities to mark a cell in the maze.
    3. Implement the function "solve()" of the class MazeSolver. This function computes the path from the entrance to the exit without any output/printing. You may need to add some constructors or members function to some classes.
    4. Optional - Overload the operator "<<" of ostream in the file mazeSolver.cpp for the class "Pos". You should be able to print out the content of the class "Pos" by
    Pos p;
    cout<<p;
    (Details : gethelp.devx.com)
    5. Implement the function printRoute() to print out the path you computed.

    Error conditions:
    You are not required to handle any error from the input.
    However, you should report to the user if the maze has no way out.

    Sample input/output:
    solveAMaze.exe < 1.maz
    +---------+
    |###########|
    | ###########|
    |# ##########|
    |## #########|
    |### ########|
    |#### #######|
    |##### ######|
    |###### #####|
    |####### ####|
    |######## |
    +----------+
    The path from (0,0) to the exit is:
    [0,0][1,0][1,1][2,1][2,2][3,2][3,3][4,3][4,4][5,4][5,5][6,5][6,6][7,6][7,7][8,7][8,8][9,8][9,9][9,10][9,11]


    P.S Please output "Sorry, the maze has no way to get out to the exit" if no ways exists.

    and i created maze.h file with the following contents:

    #define MAZENOOFROW 10
    #define MAZENOOFCOL 12

    #define WALL 0
    #define EMPTY 1

    class Maze {
    protected:
    int cell_[MAZENOOFROW][MAZENOOFCOL];
    public:
    void readMaze();
    void printMaze();
    bool isWall(int r,int c) {return cell_[r][c]==WALL;};
    };

    plz note: my maze.cpp file has the following

    #include "maze.h"
    #include <string>
    #include <iostream>
    using namespace std;

    void Maze:rintMaze()
    {

    }

    void Maze::readMaze()
    {
    string str;
    int i,j;

    for(i=0;i<MAZENOOFROW;i++)
    {
    cin >> str;
    for(j=0;j<MAZENOOFCOL;j++)
    if(str[j]=='#')
    cell_[i][j] = WALL;
    else
    cell_[i][j] = EMPTY;
    }
    }


    another file markableMaze.cpp file. It has the following contents:

    #include "markableMaze.h"


    MarkableMaze::MarkableMaze()
    {

    }


    markableMaze.h file with the following contents:

    #include "maze.h"

    class MarkableMaze : public Maze {
    protected:
    bool mark_[MAZENOOFROW][MAZENOOFCOL];
    public:
    MarkableMaze();
    void mark(int r,int c);
    void clear(int r,int c);
    void clearAll();
    bool isMark(int r,int c);
    };



    another mazeSolver.h file which contains the following:

    #include "markableMaze.h"
    #include <iostream>
    using namespace std;

    class Pos {
    public:
    int row,col;
    };

    ostream& operator << (ostream& os, const Pos p);

    class MazeSolver {
    protected:
    public:
    bool solve(MarkableMaze&);
    void printRoute();
    };



    mazeSolver.cpp file has this:

    #include "mazeSolver.h"

    bool MazeSolver::solve(MarkableMaze& m)
    {
    }

    void MazeSolver:rintRoute()
    {
    cout << "Sorry, the maze has no way to get out to the exit." << endl;
    cout << "The path from (0,0) to the exit is:" << endl;
    // print out your path here
    }

    ostream& operator << (ostream& os,Pos p)
    {
    // fill in the step 4 in the instruction
    }




    another solveAMaze.cpp file with the following contents

    #include "mazeSolver.h"

    int main()
    {
    MarkableMaze maze1;
    MazeSolver ms;

    maze1.readMaze();
    maze1.printMaze();

    ms.solve(maze1);
    ms.printRoute();

    return 0;
    }


    frankly speaking, i'm jus seeking help with calling these files and implementing them in order to get the expected output.. earnestly nd humbly, i need help with all my .cpp files also if possible..

    THANK YOU ALL IN ADVANCE...

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    To solve a maze, you :

    try to go left, try to go straight, try to go right. This is recursive, and when you have tried all three and fail, it is your base case, pop. The recursive pop backs you up to the last square, where you continue until all fail, pop, ... until you find your way out. Detection of a solution is your problem. remember to track the solution so you can display it somehow. Or just display a dot of where you are each iteration for a fun program.

  3. #3
    Join Date
    Mar 2006
    Posts
    24
    sorry Jonnin
    i am currently doing another problem in c++... hope u can help me with that!
    my Binary Search Tree post.. Can u help me with that?i will get back to this problem in a week's time...
    Thanks

Similar Threads

  1. Game Help (Game to be made in VB)
    By aamsaf in forum VB Classic
    Replies: 0
    Last Post: 09-14-2005, 03:19 PM
  2. Replies: 0
    Last Post: 06-14-2005, 12:39 PM
  3. Replies: 3
    Last Post: 03-09-2005, 12:24 AM
  4. Framework of a complex game...
    By AGuy in forum Java
    Replies: 0
    Last Post: 07-31-2000, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center