/*
Programmer: Jennifer Leopold
Date: April 6, 2018
File: path.cpp
Purpose: This file contains the definitions of the functions
for the Path class.
*/
#include "path.h"
using namespace std;
Path::Path()
{
m_currentPosition = 0;
for (int i = 0; i < Path::NUM_POSITIONS_IN_PATH; i++)
m_pathPositions[i] = BLANK;
randomlyPositionSpecialEntries(NUM_WAIT_FOR_5_OR_8_ENTRIES,
WAIT_FOR_5_OR_8);
randomlyPositionSpecialEntries(NUM_JUNGLE_ENTRIES, JUNGLE);
randomlyPositionSpecialEntries(NUM_RHINO_ENTRIES, RHINO);
}
void Path::randomlyPositionSpecialEntries
(const int numSpecialEntries, const PathEntry specialEntry)
{
bool unOccupied;
int pos;
for (int i = 1; i <= numSpecialEntries; i++)
{
unOccupied = false;
while (!unOccupied)
{
pos = rand() % Path::NUM_POSITIONS_IN_PATH;
if (m_pathPositions[pos] == BLANK)
unOccupied = true;
}
m_pathPositions[pos] = specialEntry;
}
return;
}
unsigned int Path::getCurrentPosition() const
{
return(m_currentPosition);
}
PathEntry Path::getPathEntryAtPosition(const unsigned int pos)
const
{
if ((pos > 0) && (pos < Path::NUM_POSITIONS_IN_PATH))
return(m_pathPositions[pos]);
else
{
cout << "In Path::getPathEntryAtPosition, invalid array "
<< "position: " << pos << "!\n";
return(BLANK);
}
}
bool Path::incrementCurrentPosition(const unsigned int n)
{
bool success;
if (m_currentPosition + n < NUM_POSITIONS_IN_PATH)
{
m_currentPosition += n;
success = true;
}
else
{
cout << "\nYou cannot walk off the path!\n";
success = false;
}
return(success);
}
ostream& operator << (ostream& outs, const Path& p)
{
for (int i = 0; i < Path::NUM_POSITIONS_IN_PATH; i++)
{
if (p.m_pathPositions[i] != BLANK)
{
outs << i << ": "
<< PATH_ENTRIES[p.m_pathPositions[i]] << endl;
}
}
return(outs);
}