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