Tuesday, December 8, 2009

Finding a Path - Creature Class Definition

/*
Title: Creature 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 "TannCreature.h"
using namespace std;

//Function definitions

/* Default Constructor
Sets the creature to (0,0) and makes the maze pointer point to NULL
*/
TannCreature::TannCreature()
{
position_.y = 0; position_.x = 0;
maze_ = NULL;
}

/* Explicit value constructor
Purpose: This functions will take a maze as a parameter and set the position
of the creature to the start of the maze
Rec./Ref: an instance of the maze class
*/
TannCreature::TannCreature(TannMaze &maze)
{
maze_ = &maze;
position_ = maze.getStart();
}

/* set function
Purpose: this function will do the same as the explicit value constructor, but
will allow it to be done after initialization
*/
void TannCreature::setMaze(TannMaze &maze)
{
maze_ = &maze;
position_ = maze.getStart();
}

/* findExit function
Purpose: This function will begin a set of recursive calls that will take the
creature from the start to the end if possible. It will return a bool
representing success or failure.
Return: bool representing success or failure
*/
bool TannCreature::findExit()
{
//Book problem had a way to have start and end at same point but this program
//doesn't, so it does not make a check to see if the start is the end
if(goNorth())
return true;
else if(goWest())
return true;
else if(goEast())
return true;
else
return false;
}

/* movement functions
Purpose: These functions will move the creature north, west, south, or east,
and return a bool representing success or failure. failure is when
the move north is blocked by a wall or is not inside the maze
If the creature can move in a specified direction, it marks the new
space as path...if it has to backtrack, it marks the space as 'visited'
Return: A bool representing success or failure
*/
//move North function
bool TannCreature::goNorth()
{
if(position_.y - 1 > 0
&& maze_->checkCoord(position_.x, position_.y - 1) != WALL
&& maze_->checkCoord(position_.x, position_.y - 1) != VISITED)
{
position_.y--;
if(maze_->checkCoord(position_.x, position_.y) == EXIT)
return true;
else
{
maze_->mark(position_, PATH);
if(goNorth())
return true;
else if(goWest())
return true;
else if(goEast())
return true;
else
{
maze_->mark(position_, VISITED);
position_.y++;
return false;
}
}
}
else
return false;
}

//move West function
bool TannCreature::goWest()
{
if(position_.x - 1 > 0
&& maze_->checkCoord(position_.x - 1, position_.y) != WALL
&& maze_->checkCoord(position_.x - 1, position_.y) != VISITED)
{
position_.x--;
if(maze_->checkCoord(position_.x, position_.y) == EXIT)
return true;
else
{
maze_->mark(position_, PATH);
if(goNorth())
return true;
else if(goWest())
return true;
else if(goSouth())
return true;
else
{
maze_->mark(position_, VISITED);
position_.x++;
return false;
}
}
}
else
return false;
}

//move East Function
bool TannCreature::goEast()
{
if(position_.x + 1 <>getWidth()
&& maze_->checkCoord(position_.x + 1, position_.y) != WALL
&& maze_->checkCoord(position_.x + 1, position_.y) != VISITED)
{
position_.x++;
if(maze_->checkCoord(position_.x, position_.y) == EXIT)
return true;
else
{
maze_->mark(position_, PATH);
if(goNorth())
return true;
else if(goEast())
return true;
else if(goSouth())
return true;
else
{
maze_->mark(position_, VISITED);
position_.x--;
return false;
}
}
}
else
return false;
}

//move South Function
bool TannCreature::goSouth()
{
if(position_.y + 1 <>getHeight()
&& maze_->checkCoord(position_.x, position_.y + 1) != WALL
&& maze_->checkCoord(position_.x, position_.y + 1) != VISITED)
{
position_.y++;
if(maze_->checkCoord(position_.x, position_.y) == EXIT)
return true;
else
{
maze_->mark(position_, PATH);
if(goWest())
return true;
else if(goEast())
return true;
else if(goSouth())
return true;
else
{
maze_->mark(position_, VISITED);
position_.y--;
return false;
}
}
}
else
return false;
}

No comments:

Post a Comment