i need some serious help
ok i have a feeling that i am missing something that should be very easy.
What I am doing is I have a txt file that my teacher made which is a maze. it looks something like
e000111010011100
0001100111001001
0110011011001001
1001001011011110
blah blah blah. 0's are places you can move 1's are suppose to be walls.
I am suppose to get a coordinate from the user such as 1 3 and it will check the row and coloumn and if its a 0 then it will put a 'S' for start
*ive got that*
then it is suppose to check all the neighboring positions North South East and West To see if there are 0's if there are then I am suppose to put a '+'
I am suppose to Do that until I find the 'e' or the there are no more places to go. If i find the 'e' then i am suppose to put something like "YAY IM FREE" and if not i am suppose to put "help i am stuck"
alright So I've changed things SEVERAL TIMES! and now it kind of works it just will not go to the next postition.My code looks at follows
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_ITEMS = 100000; //Max. no. of items
const int NROW = 22;
const int NCOL = 22;
typedef char MapType[NROW][NCOL];
struct ItemType{
int row;
int col;
};
class StackType
{
public:
StackType();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType item);
void Pop();
ItemType Top() const;
private:
int top;
ItemType items[MAX_ITEMS];
};
StackType::StackType()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
void StackType::Push(ItemType newItem)
{
if( IsFull())
{
cout<<"Stack is full for Push function!"<<endl;
exit(1);
}
top++;
items[top] = newItem;
}
void StackType::Pop()
{
if(IsEmpty())
{
cout<<"Stack is empty for Pop function!"<<endl;
exit(1);
}
top--;
}
ItemType StackType::Top() const
{
if(IsEmpty())
{
cout<<" Stack is empty for Top function!"<<endl;
exit(1);
}
return items[top];
}
void Read_Inf (ifstream&, MapType);
void Read_Inf(ifstream& inF, MapType M)
{
int i,j;
for(i = 0; i<NROW; i++)
for(j=0; j<NCOL; j++)
M[i][j] = '1';
for(i = 1; i<NROW-1; i++)
for(j=1; j<NCOL-1; j++)
inF>>M[i][j];
}
void DisplayMap(MapType);
void DisplayMap(MapType M)
{
int i, j;
for(i=1; i<NROW-1;i++)
{
for(j=1; j<NCOL-1;j++)
cout<<M[i][j];
cout<<endl;
}
}
int main()
{
ifstream inF;
MapType Maze;
StackType mazestack;
ItemType cpos, npos;
int crow, ccol;
int nrow, ncol;
inF.open("maze.txt");
if (!inF)
{
cout <<"** Can't open input file**"<< endl;
return 1;
}
Read_Inf(inF,Maze);
DisplayMap(Maze); //Display the initial maze map.
// To read the starting point, read its row and col.
do
{
cout<<"Enter the starting location(row, col)-->";
cin >> crow>>ccol;
}while(Maze[crow][ccol] != '0');
// Need complete codes.....
Maze[crow][ccol]='S';
cpos.row=crow;
cpos.col=ccol;
mazestack.Push(cpos);
bool found = false;
while(!mazestack.IsEmpty( ) && ! found)
{
npos=mazestack.Top();
crow=npos.row;
ccol=npos.col;
mazestack.Pop( );
if (Maze[crow][ccol]=='E')
{
found= true;
}
else
{
if (Maze[cpos.row-1][cpos.col]=='0')
{
Maze[cpos.row-1][cpos.col]='+';
npos.row=(cpos.row -1);
npos.col=cpos.col;
mazestack.Push(npos);
}
if (Maze[cpos.row+1][cpos.col]=='0')
{
Maze[cpos.row+1][cpos.col]='+';
npos.row=(cpos.row + 1);
npos.col=cpos.col;
mazestack.Push(npos);
}
if (Maze[cpos.row][cpos.col-1]=='0')
{
if(Maze[cpos.row][cpos.col-1]!='E')
Maze[cpos.row][cpos.col-1]='+';
npos.row=cpos.row;
npos.col=(cpos.col - 1);
mazestack.Push(npos);
}
if (Maze[cpos.row][cpos.col+1]=='0')
{
Maze[cpos.row][cpos.col+1]='+';
npos.row=cpos.row;
npos.col= (cpos.col + 1);
mazestack.Push(npos);
}
}
}
DisplayMap(Maze);
if(found!= true)
cout<<"Help I'm Trapped!"<<endl;
else
cout<<"Yay I escaped!"<<endl;
return 0;
}
Thanks for your time and help!
x-posted in Chixors, and my lj
What I am doing is I have a txt file that my teacher made which is a maze. it looks something like
e000111010011100
0001100111001001
0110011011001001
1001001011011110
blah blah blah. 0's are places you can move 1's are suppose to be walls.
I am suppose to get a coordinate from the user such as 1 3 and it will check the row and coloumn and if its a 0 then it will put a 'S' for start
*ive got that*
then it is suppose to check all the neighboring positions North South East and West To see if there are 0's if there are then I am suppose to put a '+'
I am suppose to Do that until I find the 'e' or the there are no more places to go. If i find the 'e' then i am suppose to put something like "YAY IM FREE" and if not i am suppose to put "help i am stuck"
alright So I've changed things SEVERAL TIMES! and now it kind of works it just will not go to the next postition.My code looks at follows
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_ITEMS = 100000; //Max. no. of items
const int NROW = 22;
const int NCOL = 22;
typedef char MapType[NROW][NCOL];
struct ItemType{
int row;
int col;
};
class StackType
{
public:
StackType();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType item);
void Pop();
ItemType Top() const;
private:
int top;
ItemType items[MAX_ITEMS];
};
StackType::StackType()
{
top = -1;
}
bool StackType::IsEmpty() const
{
return (top == -1);
}
bool StackType::IsFull() const
{
return (top == MAX_ITEMS-1);
}
void StackType::Push(ItemType newItem)
{
if( IsFull())
{
cout<<"Stack is full for Push function!"<<endl;
exit(1);
}
top++;
items[top] = newItem;
}
void StackType::Pop()
{
if(IsEmpty())
{
cout<<"Stack is empty for Pop function!"<<endl;
exit(1);
}
top--;
}
ItemType StackType::Top() const
{
if(IsEmpty())
{
cout<<" Stack is empty for Top function!"<<endl;
exit(1);
}
return items[top];
}
void Read_Inf (ifstream&, MapType);
void Read_Inf(ifstream& inF, MapType M)
{
int i,j;
for(i = 0; i<NROW; i++)
for(j=0; j<NCOL; j++)
M[i][j] = '1';
for(i = 1; i<NROW-1; i++)
for(j=1; j<NCOL-1; j++)
inF>>M[i][j];
}
void DisplayMap(MapType);
void DisplayMap(MapType M)
{
int i, j;
for(i=1; i<NROW-1;i++)
{
for(j=1; j<NCOL-1;j++)
cout<<M[i][j];
cout<<endl;
}
}
int main()
{
ifstream inF;
MapType Maze;
StackType mazestack;
ItemType cpos, npos;
int crow, ccol;
int nrow, ncol;
inF.open("maze.txt");
if (!inF)
{
cout <<"** Can't open input file**"<< endl;
return 1;
}
Read_Inf(inF,Maze);
DisplayMap(Maze); //Display the initial maze map.
// To read the starting point, read its row and col.
do
{
cout<<"Enter the starting location(row, col)-->";
cin >> crow>>ccol;
}while(Maze[crow][ccol] != '0');
// Need complete codes.....
Maze[crow][ccol]='S';
cpos.row=crow;
cpos.col=ccol;
mazestack.Push(cpos);
bool found = false;
while(!mazestack.IsEmpty( ) && ! found)
{
npos=mazestack.Top();
crow=npos.row;
ccol=npos.col;
mazestack.Pop( );
if (Maze[crow][ccol]=='E')
{
found= true;
}
else
{
if (Maze[cpos.row-1][cpos.col]=='0')
{
Maze[cpos.row-1][cpos.col]='+';
npos.row=(cpos.row -1);
npos.col=cpos.col;
mazestack.Push(npos);
}
if (Maze[cpos.row+1][cpos.col]=='0')
{
Maze[cpos.row+1][cpos.col]='+';
npos.row=(cpos.row + 1);
npos.col=cpos.col;
mazestack.Push(npos);
}
if (Maze[cpos.row][cpos.col-1]=='0')
{
if(Maze[cpos.row][cpos.col-1]!='E')
Maze[cpos.row][cpos.col-1]='+';
npos.row=cpos.row;
npos.col=(cpos.col - 1);
mazestack.Push(npos);
}
if (Maze[cpos.row][cpos.col+1]=='0')
{
Maze[cpos.row][cpos.col+1]='+';
npos.row=cpos.row;
npos.col= (cpos.col + 1);
mazestack.Push(npos);
}
}
}
DisplayMap(Maze);
if(found!= true)
cout<<"Help I'm Trapped!"<<endl;
else
cout<<"Yay I escaped!"<<endl;
return 0;
}
Thanks for your time and help!
x-posted in Chixors, and my lj
