#include #include #include using namespace std; enum direction{ north, south, east, west }; const int WORLD_SIZE = 10; const int NUM_SUSPECTS = 3; class point{ public: point(): x(0), y(0) {} void print() const { cout << "(" << x << "," << y << ")"; } void set( int x, int y ) { this -> x = x; this -> y = y; } int getX() const { return x; } int getY() const { return y; } private: int x, y; }; class patrol{ public: patrol() : arrested(0) { location.set(0,0); } int getX() const { return location.getX(); } int getY() const { return location.getY(); } void set( int x, int y ) { location.set(x,y); } void print() const { cout << "patrol car is at: "; location.print(); cout << endl; } void move(); void move( direction d ); void arrest(); bool busy(); private: point location; int arrested; }; void patrol::arrest(){ cout << "arrested a suspect!" << endl; arrested++; } bool patrol::busy(){ if ( arrested < NUM_SUSPECTS ) return true; else return false; } void patrol::move() { direction d; d = static_cast( rand() % 4 ); move(d); } void patrol::move(direction d) { int x = location.getX(); int y = location.getY(); switch(d){ case north: y = (y + 1) % WORLD_SIZE; break; case south: y = y - 1; if (y < 0) y = WORLD_SIZE; break; case east: x = (x + 1) % WORLD_SIZE; break; case west: x = (x - 1) % WORLD_SIZE; if (x < 0) x = WORLD_SIZE; } location.set(x,y); } class suspect{ public: suspect() : caught(false) { location.set((rand() % WORLD_SIZE), (rand() % WORLD_SIZE)); } int getX() const { return location.getX(); } int getY() const { return location.getY(); } void set( int x, int y ) { location.set(x,y); } void jailed() { cout << "jailed!" << endl; caught = true; set(0,0); } void print() const { cout << "suspect at: "; location.print(); cout << endl; } void move(); void move(direction d); bool isCaught() { return caught; } private: bool caught; point location; }; void suspect::move() { direction d; d = static_cast( rand() % 4 ); move(d); } void suspect::move(direction d) { int x = location.getX(); int y = location.getY(); switch(d){ case north: y = (y + 1) % WORLD_SIZE; break; case south: y = y - 1; if (y < 0) y = WORLD_SIZE; break; case east: x = (x + 1) % WORLD_SIZE; break; case west: x = (x - 1) % WORLD_SIZE; if (x < 0) x = WORLD_SIZE; } location.set(x,y); } class world{ public: void print() const { p.print(); for (int i = 0; i != NUM_SUSPECTS; i++ ) s[i].print(); } void set ( int i, int x, int y ) { s[i].set(x,y); } void movepatrol(); void movesuspects(); bool suspectsCaught(); void update(); //int engaged(); private: suspect s[NUM_SUSPECTS]; patrol p; }; void world::movepatrol(){ p.move(); } void world::movesuspects(){ for ( int i = 0; i != NUM_SUSPECTS; i++ ) if ( !s[i].isCaught() ) s[i].move(); } void world::update(){ for ( int i = 0; i != NUM_SUSPECTS; i++ ) if ( s[i].getX() == p.getX() && s[i].getY() == p.getY() && !s[i].isCaught()){ p.arrest(); s[i].jailed(); } } bool world::suspectsCaught() { bool suspects_arrested = true; for ( int i = 0; i != NUM_SUSPECTS; i++ ) if ( !s[i].isCaught() ) suspects_arrested = false; return suspects_arrested; } int main(int argc, char* argv[]){ srand(time(NULL)); world w; while( ! w.suspectsCaught() ) { w.movepatrol(); w.movesuspects(); w.update(); w.print(); } return 0; }