//Maze.cc
//brian jahns C1999
//cs252
//Maze class implementation file
#include <iostream.h>
#include "Maze.h"
//output stream overloaded operator
ostream& operator << (ostream& os, const Maze& maze)
{
for(int j=0; j<12; j++)
{
for(int i=0; i<12; i++)
os<<maze.map[j][i];
os<<endl;
}
return os;
}
//input stream overloaded operator
istream& operator >> (istream& is, Maze& maze)
{
for(int i=0; i<12; i++)
is.getline(maze.map[i], 13);
return is;
}
Maze::Maze()
{
for(int i; i<12; i++)
for(int j; j<12; j++)
{
map[j][i]='a';
}
}
Maze::~Maze()
{}
//accessors for x and y
//int Maze::col()
//{
// return x;
//}
//int Maze::row()
//{
// return y;
//}
void Maze::cls()
{
for(int i=1; i<=12; i++)
cout<<endl;
}
void Maze::changedir(int i)
{
switch (direction)
{
case (west):
if(i==-1)
direction=south;
else
direction=north;
break;
case (north):
if(i==-1)
direction=west;
else
direction=east;
break;
case (east):
if(i==-1)
direction=north;
else
direction=south;
break;
case (south):
if(i==-1)
direction=east;
else
direction=west;
break;
default:
cout<<"ground control to major tom: invalid int (changedir, case)"<<endl;
}
}
//heading changers that call the private go() movement member function
void Maze::goleft()
{
changedir(-1);
go();
}
void Maze::gostraight()
{
go();
}
void Maze::goright()
{
changedir(1);
go();
}
//private go()
void Maze::go()
{
switch(direction)
{
case (west):
{
col=col-1;
break;
}
case (north):
{
row=row-1;
break;
}
case (east):
{
col=col+1;
break;
}
case (south):
{
row=row+1;
break;
}
default:
cerr<<"ground control to major tom: error, unknown heading! (maze:go)"
<<endl;
}
return;
}
bool Maze::findstart()
{
char c=map[0][0];
int counter=0;
row=0;
col=0;
while(c!='.')
{
c=map[counter][0];
counter++;
}
counter++;
row=counter;
//map[0][counter] is the location of the '.' start
//map[col][row]:: col is the columns and row is the rows
if(row>11)
return false;
else
return true;
}
bool Maze::solve()
{
output();
cout<<endl;
if (col==11)
return true;
switch(direction)
{
case (west):
{
if(map[col][row+1]=='.')
{
goleft();
solve();
break;
}
else if(map[col-1][row]=='.')
{
gostraight();
solve();
break;
}
else if(map[col][row-1]=='.')
{
goright();
solve();
break;
}
else
return false;
break;
}
case (north):
{
if(map[col-1][row]=='.')
{
goleft();
solve();
break;
}
else if(map[col][row-1]=='.')
{
gostraight();
solve();
break;
}
else if(map[col+1][row]=='.')
{
goright();
solve();
break;
}
else
return false;
break;
}
case (east):
{
if(map[col][row-1]=='.')
{
goleft();
solve();
break;
}
else if(map[col+1][row]=='.')
{
gostraight();
solve();
break;
}
else if(map[col][row+1]=='.')
{
goright();
solve();
break;
}
else
return false;
break;
}
case (south):
{
if(map[col+1][row]=='.')
{
goleft();
solve();
break;
}
else if(map[col][row+1]=='.')
{
gostraight();
solve();
break;
}
else if(map[col-1][row]=='.')
{
goright();
solve();
break;
}
else
return false;
break;
}
default:
return false;
}
return true;
}
void Maze::output()
{
map[row][col]='x';
cout<<*this;
}