Tuesday, December 8, 2009

Finding a Path Algorithm - Maze Class Definition

/*
Title: Maze Class Function Definitions
Author: Daniel J. Tanner
Class: CS 2420 - Data Abstraction & Problem Solving w/ C++
Date: October 17th, 2005 (latest update)
*/

#include
#include
#include
#include
#include
#include
#include "TannMaze.h"
using namespace std;

//Maze Class Definitions
//Destructor
TannMaze::~TannMaze()
{
delete [] maze;
}

//Constructors
/* Default
Purpose : This constructor will simply create an instance of the Maze
class
and have the 2-d array pointer point to null
Rec./Ref.: N/A
*/
TannMaze::TannMaze()
{
try
{
mazeHeight_ = 5; mazeWidth_ = 5;
start_.x = 2; start_.y = 4;
end_.x = 2; end_.y = 0;
generateMaze();
}
catch(MazeError error)
{throw(error);}
}

/* Explicit Value Constructor
Purpose: To accept from the user a height, a width, and a start and an end
and then to redirect this information to the generateMaze function
Rec.: height, width, start coord and end coord
Call: function generateMaze(height, width, start, end)
*/
TannMaze::TannMaze(int width, int height, Coord &start, Coord &end)
{
cout << "using explicit input constructor to generate maze..." << endl;
try
{
generateMaze(width, height, start, end);
}
catch(MazeError error){throw error;}
}

/* Filename Constructor
Purpose: This constructor will redirect the program to the generateMaze
function that deals with files directly
Rec/Ref: the filename of the file that will be opened and read from
Send: that same filename to the appropriate maze generator function
*/
TannMaze::TannMaze(string filename)
{
try{
generateMaze(filename);
}
catch(MazeError error)
{
throw(error);
}
}

//Maze generation functions
/* Explicit Value Maze Generator
This function will set all the different variables at once and then redirect
the program to the default generateMaze function
*/
void TannMaze::generateMaze(int width, int height, Coord &start, Coord &end)
{
mazeHeight_ = height; mazeWidth_ = width; start_ = start; end_ = end;
try{generateMaze();}
catch(MazeError error)
{ throw(error);}
}

/* Explicit Value (from file) Maze Generator
This function will set all the variables based on a file as described by the
problem definition in the book
*/
void TannMaze::generateMaze(string filename)
{
fstream fileIn(filename.c_str());
if(!fileIn)
throw MazeError("Error opening file " + filename);
int height, width;
Coord start, end;
fileIn >> width >> height >> end.x >> end.y >> start.x >> start.y;
mazeHeight_ = height; mazeWidth_ = width; start_ = start; end_ = end;
try{generateMaze();}
catch(MazeError error)
{ throw(error);}
}



/* Default Maze Generator
This is THE main maze generator function
Purpose: To generate a 2-dimensional array-based maze made up of ascii
characters, and then randomly fill it with walls and open spaces
along with a specific startpoint and a specific endpoint.
Precondition: The height, width, start, and end variables must have already
been assigned to the class' variables for this to work
correctly
it is therefore only able to be called by member functions which
already assign those properties.
*/
void TannMaze::generateMaze()
{
//create the 2d array, throwing an error if there's not enough memory
try
{
maze = new mazeSquare*[mazeWidth_];
for(int index = 0; index < mazeWidth_; index++)
maze[index] = new mazeSquare[mazeHeight_];
}
catch(bad_alloc error) {throw MazeError("Error allocating memory");}

//set up random number generator to be based off current time
srand(unsigned(time(NULL)));

//fill the maze with squares of either WALL or CLEAR
for(int heightCount = 0; heightCount < mazeHeight_; heightCount++)
{
for(int widthCount = 0; widthCount < mazeWidth_; widthCount++)
{
switch(rand() % 2)
{
case 0:
maze[widthCount][heightCount] = WALL;
break;
case 1:
maze[widthCount][heightCount] = CLEAR;
break;
}
}
}

//set the start and end
maze[start_.x][start_.y] = START;
maze[end_.x][end_.y] = EXIT;
}

void TannMaze::displayMaze()const
{
for(int hIndex = 0; hIndex < mazeHeight_; hIndex++)
{
for(int wIndex = 0; wIndex < mazeWidth_; wIndex++)
{
switch(maze[wIndex][hIndex])
{
case WALL:
cout << 'X';
break;
case CLEAR:
cout << ' ';
break;
case EXIT:
cout << 'E';
break;
case START:
cout << 'S';
break;
case PATH:
cout << '@';
break;
case VISITED:
cout << '*';
break;
default:
break;
}
}
cout << endl;
}
}

/* Mark Function
Purpose: This function will change the status of one maze square to visited or to path
Rec.: the location of the square as a coord, and the new status
*/
void TannMaze::mark(const Coord &sqLoc, enum mazeSquare demarcation)
{
maze[sqLoc.x][sqLoc.y] = demarcation;
}

No comments:

Post a Comment