// Intentionally Left Empty
}
+Elevator::Elevator (int starting_floor)
+ : door_(CLOSED)
+ , direction_(IDLE)
+ , current_position_(starting_floor)
+ , stops_()
+ , ELEVATOR_STEP(0.1)
+{
+ // Intentionally Left Empty
+}
+
bool Elevator::currently_at_stop () const
{
StopList::const_iterator it;
Stop current(current_position_, direction_);
+ /* Calculate the number of Stops above and below our
+ * current position */
+ int stops_above = 0;
+ int stops_below = 0;
+
+ for (it = stops_.begin(); it != stops_.end(); it++)
+ {
+ if (current < *it)
+ ++stops_above;
+
+ if (current > *it)
+ ++stops_below;
+ }
+
+ /* Check if we are at the top. If so, only the position needs to match */
+ if (direction_ == UP && stops_above == 0)
+ {
+ for (it = stops_.begin (); it != stops_.end (); it++)
+ if (it->getPosition() == current_position_)
+ return true;
+ }
+
+ /* Check if we are at the bottom. If so, only the position needs to match */
+ if (direction_ == DOWN && stops_below == 0)
+ {
+ for (it = stops_.begin (); it != stops_.end (); it++)
+ if (it->getPosition() == current_position_)
+ return true;
+ }
+
+ /* Check if we match exactly */
for (it = stops_.begin (); it != stops_.end (); it++)
if (*it == current)
return true;
+ /* No match */
return false;
}
return pos - current_position_;
}
+#include <string>
+static void debug (const std::string& s)
+{
+ std::cout << s << std::endl;
+}
+
void Elevator::move ()
{
static int wait = 0;
*/
if (wait > 0)
{
+ debug ("waiting");
--wait;
return;
}
/* close the door if it is open. This is a requirement to move */
if (door_ != CLOSED)
{
+ debug ("closing door");
wait = 10;
+ door_ = CLOSED;
close_door ();
return;
}
- if (currently_at_stop ())
- {
- wait = 10; // delay around for 10 steps
- stops_.remove (Stop(current_position_, direction_));
- open_door (); // call into the GUI to open the door
- return;
- }
-
/* Calculate the number of Stops above and below our
* current position */
StopList::const_iterator it;
int stops_above = 0;
int stops_below = 0;
- for (it = stops_.begin(); it != stops_.begin(); it++)
+ for (it = stops_.begin(); it != stops_.end(); it++)
{
if (current < *it)
++stops_above;
++stops_below;
}
+ /* Check if we are currently at a stop */
+ if (currently_at_stop ())
+ {
+ std::cout << "At A Stop: " << Stop(current_position_, direction_) << std::endl;
+ std::cout << "above=" << stops_above << " below=" << stops_below << std::endl;
+ wait = 10;
+
+ /* Remove all stops here if we are switching direction */
+ if (stops_above == 0 && direction_ == UP)
+ stops_.remove (Stop(current_position_, ALL));
+
+ if (stops_below == 0 && direction_ == DOWN)
+ stops_.remove (Stop(current_position_, ALL));
+
+ stops_.remove (Stop(current_position_, direction_));
+
+ /* Open the door */
+ door_ = OPEN;
+ open_door ();
+
+ return;
+ }
+
/* Check if we need to change direction */
- if (direction_ == UP && stops_above == 0 && stops_below > 0)
+ if (stops_above == 0 && stops_below > 0 && direction_ == UP)
+ {
+ debug ("1: DOWN");
direction_ = DOWN;
+ }
- if (direction_ == DOWN && stops_below == 0 && stops_above > 0)
+ if (stops_below == 0 && stops_above > 0 && direction_ == DOWN)
+ {
+ debug ("2: UP");
direction_ = UP;
+ }
- if (stops_above == 0 && stops_below == 0)
+ if (stops_above == 0 && stops_below == 0 && direction_ != IDLE)
+ {
+ debug ("3: IDLE");
direction_ = IDLE;
+ }
+
+ if (direction_ == IDLE && stops_above > 0)
+ {
+ debug ("4: UP");
+ direction_ = UP;
+ }
+
+ if (direction_ == IDLE && stops_below > 0)
+ {
+ debug ("5: DOWN");
+ direction_ = DOWN;
+ }
/* Move in the correct direction */
void Elevator::update_position () const
{
- std::cout << "Updating the GUI with our position" << std::endl;
+ std::cout << "Updating the GUI with our position: " << current_position_ << std::endl;
}
void Elevator::open_door () const
{
/* If we do not use the direction to help differentiate, then it is
* possible that an object can be neither less, greater, or equal */
- return ((position_ < rhs.position_) || (direction_ < rhs.direction_));
+ return (position_ < rhs.position_);
}
bool Stop::operator> (const Stop& rhs) const
return direction_;
}
+const Position Stop::getPosition () const
+{
+ return position_;
+}
+
std::ostream& operator<< (std::ostream& os, const Stop& rhs)
{
- os << "Stop(" << rhs.position_ << ", " << rhs.direction_ << ")";
+ os << "Stop(" << rhs.position_ << ", "; // << rhs.direction_ << ")";
+
+ switch (rhs.direction_)
+ {
+ case IDLE:
+ os << "IDLE";
+ break;
+ case UP:
+ os << "UP";
+ break;
+ case DOWN:
+ os << "DOWN";
+ break;
+ case ALL:
+ os << "ALL";
+ break;
+ default:
+ os << "UNHANDLED";
+ break;
+ }
+
+ os << ")";
return os;
}