#ifndef DIRECTION_HPP
#define DIRECTION_HPP
-enum direction { IDLE, UP, DOWN };
+enum direction { IDLE, UP, DOWN, ALL };
#endif /* DIRECTION_HPP */
void Elevator::stop_at (int floor, enum direction _direction)
{
std::cout << "Adding Stop: Floor=" << floor << " Dir=" << _direction << std::endl;
- //_stops.insert (Stop(floor, _direction));
+
_stops.push_back (Stop(floor, _direction));
}
*/
void Elevator::move ()
{
- std::cout << "move()" << std::endl;
+ /* Check if we are at a stop. If we are:
+ * 1) Open the doors
+ * 2) Clear the call button
+ */
+ if (currently_at_stop ())
+ {
+ std::cout << "At a stop, right now" << std::endl;
+ return;
+ }
+
+ /* Check if we have any more stops to attend to. If we do not, go IDLE */
+ if (_stops.size() == 0)
+ {
+ _direction = IDLE;
+ return;
+ }
+
+ /* Find the best direction to move in, and set our direction to that */
+ if (_direction == IDLE)
+ _direction = find_best_direction ();
+
+ /* Move */
+ switch (_direction)
+ {
+ case UP:
+ _pos += elevator_step;
+ break;
+ case DOWN:
+ _pos -= elevator_step;
+ break;
+ default:
+ throw bad_direction ();
+ break;
+ }
+
+ std::cout << "at position: " << _pos << std::endl;
}
enum direction Elevator::find_best_direction ()
std::vector<Stop>::const_iterator it;
int above, below;
+ /* Find out how many are above and below our current position */
for (above=0, below=0, it = _stops.begin(); it != _stops.end(); it++)
{
-#if 0
- if (_pos.lowerThan (*it))
+ if (_pos < (*it))
above++;
- if (_pos.higherThan (*it))
+ if (_pos > (*it))
below++;
-#endif
}
- std::cout << "above=" << above << " below=" << below << std::endl;
+ /* If we have nothing, then stay IDLE */
+ if (above == 0 && below == 0)
+ return IDLE;
- return UP;
+ /* If they are the same, always go up first.
+ *
+ * This comes from the design meeting on 2007-10-03 */
+ if (above == below)
+ return UP;
+
+ /* Otherwise, go in the direction that has the most stops */
+ return above > below ? UP : DOWN;
}
+bool Elevator::currently_at_stop ()
+{
+ std::vector<Stop>::iterator it;
+ Stop current (_pos, _direction);
+
+ for (it = _stops.begin (); it != _stops.end (); it++)
+ {
+ std::cout << "cas(): current=" << current << " it=" << *it << std::endl;
+ if (*it == current)
+ return true;
+ }
+ return false;
+}
#define ELEVATOR_HPP
#include <iostream>
-//#include <set>
#include <vector>
#include "position.hpp"
#include "direction.hpp"
enum door_status { CLOSED, OPEN };
-const int elevator_step = 0.1;
+const float elevator_step = 0.1;
class bad_direction { };
* PURPOSE: going in the given direction.
*
* REQUIRE: floor is a valid floor
- * REQUIRE: direction is a valid direction
+ * REQUIRE: direction is either UP or DOWN
*
* PROMISE: The elevator will stop at the floor when it gets there
*/
*/
enum direction find_best_direction ();
+ /*
+ * PURPOSE: Figure out if we are currently at a Stop in _stops
+ *
+ * REQUIRE: Nothing
+ *
+ * PROMISE: Return true if we are at a Stop in _stops, false otherwise
+ */
+ bool currently_at_stop ();
+
private:
/* Storage for all of the places that we will be stopping */
- //std::set<Stop> _stops;
std::vector<Stop> _stops;
/* Storage for the current elevator position */
_major += major;
_minor += minor;
+ /* Check for overflow */
+ if (_minor >= 10)
+ {
+ _major += 1;
+ _minor -= 10;
+ }
+
return *this;
}
return false;
}
-#include <iostream>
bool Position::operator> (const Position& rhs)
{
return false;
}
+std::ostream& operator<< (std::ostream& os, Position& rhs)
+{
+ os << "Position: " << rhs._major << "." << rhs._minor;
+ return os;
+}
+
/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */
#ifndef POSITION_HPP
#define POSITION_HPP
+#include <iostream>
+
class Position
{
public:
bool operator< (const Position& rhs);
bool operator> (const Position& rhs);
+ friend std::ostream& operator<< (std::ostream& os, Position& rhs);
+
protected:
int _major;
int _minor;
// Intentionally Left Empty
}
+Stop::Stop (Position pos, enum direction mydirection)
+ : Position (pos)
+ , _direction (mydirection)
+{
+ // Intentionally Left Empty
+}
+
bool Stop::operator== (Stop& rhs)
{
return (Position::operator==(rhs)) && (_direction == rhs._direction);
}
+std::ostream& operator<< (std::ostream& os, Stop& rhs)
+{
+ os << "Stop: " << "help me" << " dir=" << rhs._direction;
+ return os;
+}
+
/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */
*/
Stop (int floor, enum direction mydirection);
+
+ Stop (Position pos, enum direction mydirection);
+
/*
* PURPOSE: Check if this and another Stop object is equivalent
*
* PROMISE: Return true if this and rhs are equivalent, false otherwise
*/
bool operator== (Stop& rhs);
+ friend std::ostream& operator<< (std::ostream& os, Stop& rhs);
private:
#include <iostream>
using namespace std;
-//#include "elevator.hpp"
-#include "stop.hpp"
+#include "elevator.hpp"
int main (int argc, char *argv[])
{
- Stop s1 (1, DOWN);
- Stop s2 (2, DOWN);
+ Elevator e;
- if (s1 > s2)
- cout << "s1 > s2" << endl;
- else if (s1 == s2)
- cout << "s1 == s2" << endl;
- else if (s1 < s2)
- cout << "s1 < s2" << endl;
- else
- cout << "BAD BAD BAD" << endl;
+ e.stop_at (1, DOWN);
+ e.stop_at (2, DOWN);
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
+ e.move ();
return 0;
}