Its an assignment I have to submit, we have to make a maze using stack and input text file
but I admit that i'm tottaly lost in this, something wrong with my code (or really alot of mistakes)
please help!

#include <iostream>
#include <fstream>
using namespace std;


struct node
{
int row;
int col;
node*next;
};


class Store
{
private:
node*head;


public:


Store()
{
head=NULL;
}


void PushVi(int i, int j)
{
node*t=new node;
t->row=i;
t->col=j;
t->next=head;
head=t;
}


bool searchvi(int i, int j)
{
node*t;
node*p;
t=head;
p=head;
int ctr1=0;
int ctr2=0;


while (t!= NULL && t->row != i)
{
ctr1++;
t=t->next;
}


while (p!= NULL && p->col != j)
{
ctr2++;
p=p->next;
}


if(ctr1 == ctr2 && ctr1 != 0 && ctr2!=0)
return true;
else
return false;
}
};



class MazeStack
{
private:
node*Top;


public:


MazeStack()
{
Top = NULL;
}


void Push(int i,int j)
{


node*t= new node;
t->row=i;
t->col=j;
t->next=Top;
Top=t;


}


void Pop()
{
node*t;
if(Top != NULL)
{
t=Top;
Top=t->next;
delete t;
}
}


bool IsEmpty()
{
if(Top==NULL)
return true;
else
return false;
}


bool search(int i, int j)
{
node*t;
node*p;
t= Top;
p=Top;
int ctr1=0;
int ctr2=0;


while (t!= NULL && t->row != i)
{
ctr1++;
t=t->next;
}


while (p!= NULL && p->col != j)
{
ctr2++;
p=p->next;
}


if(ctr1 == ctr2)
return true;
else
return false;
}


void strt(int i, int j, char k[30][30])
{
int n=0;
int m=0;
MazeStack g;


while(n < i)
{
m=0;
while(m < j)
{
//  cout<<k[n][m];
if(k[n][m] == '*')
{
cout << "Start Point is at: " << n << "*" << m << endl;
g.Push(n,m);
m=j;
n=i;
}
m++;


}
n++;
//  cout<<endl;
}
if(n==i && m==j)
cout << "There is no Start Point" << endl;
}


void en(int i, int j, char k[30][30])
{
int n=0;
int m=0;


while(n < i)
{
m=0;
while(m < j)
{
if(k[n][m] == 'o')
{
cout << "End Point is at: " << n << "*" << m << endl;
n=i;
m=j;
}
m++;
}
n++;
}
if(n==i && m==j)
cout << "There is no End Point" << endl;
}



void path(int i, int j, char k[30][30])
{
int n=0;
int m=0;
int a;
int b;


Store s;
MazeStack g;


while(n < i)
{
m=0;
while(m < j)
{
if(k[n][m] == '*')
{
a=n;
b=m;
n=i;
m=j;
}
m++;
}
n++;
}
if(a==i && b==j)
cout << "There is no Path" << endl;
else
{
cout << "Path Exist: " << endl;



while(k[a] != 'o')
{



if(k[a-1] == '.')
{
if(s.searchvi(a-1,b))
{
if(k[a][b+1] == '.')
b=b+1;
else
if(k[a+1] == '.')
a=a+1;
else
if(k[a][b-1] == '.')
b=b-1;
else
{
g.Pop();
a=a-1;
}
}
else
{
a=a-1;
g.Push(a,b);
}


s.PushVi(a,b);
}
else
if(k[a][b+1] == '.')
{


if(s.searchvi(a,b+1))
{
if(k[a+1] == '.')
a=a+1;
else
if(k[a][b-1] == '.')
b=b-1;
else
if(k[a-1] == '.')
a=a-1;
else
{
g.Pop();
b=b+1;
}
}
else
{
b=b+1;
g.Push(a,b);
}
s.PushVi(a,b);
}
else if(k[a+1] == '.')
{
if(s.searchvi(a+1,b))
{
if(k[a][b-1] == '.')
b=b-1;
else
if(k[a-1] == '.')
a=a-1;
else
if(k[a][b+1] == '.')
b=b+1;
else
{
g.Pop();
a=a+1;
}
}
else
{
a=a+1;
g.Push(a,b);
}
s.PushVi(a,b);
}
else
if(k[a][b-1] == '.')
{
if(s.searchvi(a,b-1))
{
if(k[a-1] == '.')
a=a-1;
else
if(k[a][b+1] == '.')
b=b+1;
else
if(k[a+1] == '.')
a=a+1;
else
{
g.Pop();
b=b-1;
}
}
else
{
b=b-1;
g.Push(a,b);
}


s.PushVi(a,b);
}


}
}
}
void Print()
{
node*ptr;
ptr=Top;


while(ptr != NULL)
{
cout << ptr->row << "*" << ptr->col << endl;
ptr=ptr->next;
}
}
};



int main()
{


int x,y;
char z[30][30];
MazeStack w;


ifstream inFile;
inFile.open("maze.txt");


inFile>>x;
inFile>>y;


int a=0;
int b=0;



while (a<x)
{
b=0;
while(b<y)
{
inFile>>z[a];
b++;
}
a++;
}



cout << "The Size of the Maze is " << x << " x " << y << endl;



a=0;
while(a<x)
{
int b=0;
while(b<y)
{
cout << z[a];
b++;
}
a++;
cout << endl;
}



w.strt(a,b,z);
w.en(a,b,z);
w.path(a,b,z);


return 0;
}

Recommended Answers

All 4 Replies

Code tags, formatting, and more specificity in your questions please. What is this supposed to do, what's it actually do, what is the problem, where is the problem, etc. Also, if we need an input file to run this, please post it (i.e. where is "maze.txt"?). Here's your code, formatted and with line numbers. Use code tags to do that:

[code=cplusplus] // paste your code

[/code]

#include <iostream>
#include <fstream>
using namespace std;

struct node
{
    int row;
    int col;
    node*next;
};

class Store
{
private:
    node*head;

public:

    Store()
    {
        head=NULL;
    }

    void PushVi(int i, int j)
    {
        node*t=new node;
        t->row=i;
        t->col=j;
        t->next=head;
        head=t;
    }

    bool searchvi(int i, int j)
    {
        node*t;
        node*p;
        t=head;
        p=head;
        int ctr1=0;
        int ctr2=0;

        while (t!= NULL && t->row != i)
        {
            ctr1++;
            t=t->next;
        }

        while (p!= NULL && p->col != j)
        {
            ctr2++;
            p=p->next;
        }

        if(ctr1 == ctr2 && ctr1 != 0 && ctr2!=0)
            return true;
        else
            return false;
    }
};


class MazeStack
{
private:
    node*Top;

public:

    MazeStack()
    {
        Top = NULL;
    }

    void Push(int i,int j)
    {

        node*t= new node;
        t->row=i;
        t->col=j;
        t->next=Top;
        Top=t;

    }

    void Pop()
    {
        node*t;
        if(Top != NULL)
        {
            t=Top;
            Top=t->next;
            delete t;
        }
    }

    bool IsEmpty()
    {
        if(Top==NULL)
            return true;
        else
            return false;
    }

    bool search(int i, int j)
    {
        node*t;
        node*p;
        t= Top;
        p=Top;
        int ctr1=0;
        int ctr2=0;

        while (t!= NULL && t->row != i)
        {
            ctr1++;
            t=t->next;
        }

        while (p!= NULL && p->col != j)
        {
            ctr2++;
            p=p->next;
        }

        if(ctr1 == ctr2)
            return true;
        else
            return false;
    }

    void strt(int i, int j, char k[30][30])
    {
        int n=0;
        int m=0;
        MazeStack g;

        while(n < i)
        {
            m=0;
            while(m < j)
            {
                // cout<<k[n][m];
                if(k[n][m] == '*')
                {
                    cout << "Start Point is at: " << n << "*" << m << endl;
                    g.Push(n,m);
                    m=j;
                    n=i;
                }
                m++;

            }
            n++;
            // cout<<endl;
        }
        if(n==i && m==j)
            cout << "There is no Start Point" << endl;
    }

    void en(int i, int j, char k[30][30])
    {
        int n=0;
        int m=0;

        while(n < i)
        {
            m=0;
            while(m < j)
            {
                if(k[n][m] == 'o')
                {
                    cout << "End Point is at: " << n << "*" << m << endl;
                    n=i;
                    m=j;
                }
                m++;
            }
            n++;
        }
        if(n==i && m==j)
            cout << "There is no End Point" << endl;
    }


    void path(int i, int j, char k[30][30])
    {
        int n=0;
        int m=0;
        int a;
        int b;

        Store s;
        MazeStack g;

        while(n < i)
        {
            m=0;
            while(m < j)
            {
                if(k[n][m] == '*')
                {
                    a=n;
                    b=m;
                    n=i;
                    m=j;
                }
                m++;
            }
            n++;
        }
        if(a==i && b==j)
            cout << "There is no Path" << endl;
        else
        {
            cout << "Path Exist: " << endl;


            while(k[a][b] != 'o')
            {


                if(k[a-1][b] == '.')
                {
                    if(s.searchvi(a-1,b))
                    {
                        if(k[a][b+1] == '.')
                            b=b+1;
                        else
                            if(k[a+1][b] == '.')
                                a=a+1;
                            else
                                if(k[a][b-1] == '.')
                                    b=b-1;
                                else
                                {
                                    g.Pop();
                                    a=a-1;
                                }
                    }
                    else
                    {
                        a=a-1;
                        g.Push(a,b);
                    }

                    s.PushVi(a,b);
                }
                else
                    if(k[a][b+1] == '.')
                    {

                        if(s.searchvi(a,b+1))
                        {
                            if(k[a+1][b] == '.')
                                a=a+1;
                            else
                                if(k[a][b-1] == '.')
                                    b=b-1;
                                else
                                    if(k[a-1][b] == '.')
                                        a=a-1;
                                    else
                                    {
                                        g.Pop();
                                        b=b+1;
                                    }
                        }
                        else
                        {
                            b=b+1;
                            g.Push(a,b);
                        }
                        s.PushVi(a,b);
                    }
                    else if(k[a+1][b] == '.')
                    {
                        if(s.searchvi(a+1,b))
                        {
                            if(k[a][b-1] == '.')
                                b=b-1;
                            else
                                if(k[a-1][b] == '.')
                                    a=a-1;
                                else
                                    if(k[a][b+1] == '.')
                                        b=b+1;
                                    else
                                    {
                                        g.Pop();
                                        a=a+1;
                                    }
                        }
                        else
                        {
                            a=a+1;
                            g.Push(a,b);
                        }
                        s.PushVi(a,b);
                    }
                    else
                        if(k[a][b-1] == '.')
                        {
                            if(s.searchvi(a,b-1))
                            {
                                if(k[a-1][b] == '.')
                                    a=a-1;
                                else
                                    if(k[a][b+1] == '.')
                                        b=b+1;
                                    else
                                        if(k[a+1][b] == '.')
                                            a=a+1;
                                        else
                                        {
                                            g.Pop();
                                            b=b-1;
                                        }
                            }
                            else
                            {
                                b=b-1;
                                g.Push(a,b);
                            }

                            s.PushVi(a,b);
                        }

            }
        }
    }
    void Print()
    {
        node*ptr;
        ptr=Top;

        while(ptr != NULL)
        {
            cout << ptr->row << "*" << ptr->col << endl;
            ptr=ptr->next;
        }
    }
};


int main()
{

    int x,y;
    char z[30][30];
    MazeStack w;



    ifstream inFile;
    inFile.open("maze.txt");

    inFile>>x;
    inFile>>y;

    int a=0;
    int b=0;


    while (a<x)
    {
        b=0;
        while(b<y)
        {
            inFile>>z[a][b];
            b++;
        }
        a++;
    }




    cout << "The Size of the Maze is " << x << " x " << y << endl;


    a=0;
    while(a<x)
    {
        int b=0;
        while(b<y)
        {
            cout << z[a][b];
            b++;
        }
        a++;
        cout << endl;
    }


    w.strt(a,b,z);
    w.en(a,b,z);
    w.path(a,b,z);



    return 0;
}

Oh thanks
sorry this is my first time, didn't know how to make the code more readable and things...

well, the input file will contains two numbers that tells the size of the maze, and the maze itself, the maze itself has 4 signs
"*" for the start point
"o" for the target, or end the point
"#" for the walls
"." for the empty space to move on
We should walk through the dots only to find the path from the "*" to the "o"

the program will read the size and the maze from the input file using two dimintional array?, and it should do the following:
1-Gives the location of the start point ( that is from line 131 till 158)
2-Gives the location of the end point ( that is from line 160 till 182)
3-Tell if there exist a path, if there exist then it will give us all the locations of the dots that lead to from the start point to the end point. ( this path thing is my main problem, the very complicated function for the path is from 185 till 330)

the start point, end point, and the path should be in a stack, so in the end they will be printed using the function print (from line 331 till 342)

and my problem in the path function is that, after I pass one point how to make it known as passed? I tried to make another stack and push all the visited points with the main stack that will be printed in the end, so whenver the "if" statement works and find a dot in that "path" function, it will go and search if this dot is already exist in the "store" stack thingy, so if it exists, the main stack MazeStack will pop it, if the next location also exist, the main stack will keep on poping till it reachs for a point that is not stored in the Store stack, then the main stack will push the new location.
the while loop will keep on going as long as the current location does not qual to the exit point 'o', and also there is another condition I forgot to put in the while loop, is if the locations reached their maximum size and the exit point still not found then "There is no path exist"...


In the attached file there is a simple example of how the input file will look like, and the output won't be in another text file, it will be in the same normal black screen of the program and should look like this:

Start point is at: 1*5
End point is at: 2*1
Path exists:
1*5
2*5
2*4
2*3
1*3
1*2
1*1
2*1.

> didn't know how to make the code more readable and things...
It's only mentioned at least 5 times in the things you should have read before posting.

Nevermind I solved it myself :/

It's only mentioned at least 5 times in the things you should have read before posting.

Yeah sorry I just read it, I was in a big hurry when I posted this right after I registered.

Sorry and thanks for trying to help, VernonDozier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.