#include #include "visionary.h" using namespace std; static double dist(const wabbit *w1, const wabbit *w2) throw (wabbit::cross_game) { int dx; int dy; difference(w1, w2, &dx, &dy); return sqrt(static_cast(dx * dx + dy * dy)); } inline int signum(int i) throw () {return i < 0 ? -1 : i > 0;} static void step(const wabbit *w1, const wabbit *w2, int *dx, int *dy) throw (wabbit::cross_game) { difference(w1, w2, dx, dy); *dx = signum(*dx); *dy = signum(*dy); } void visionary::decide(int *dx, int *dy) const { static const unsigned radius = 3; //of vision //Move one step away from a wabbit that could eat me. for (const_iterator it = begin(); it != end(); ++it) { const wabbit *const other = *it; if (other != static_cast(this) && dist(this, other) <= radius && other->hungry() > this->bitter()) { step(other, this, dx, dy); return; } } /* Arrive here if there were no enemies within the visual radius. Now see if there's any food I could eat within the visual radius. If so, take one step towards it. */ for (const_iterator it = begin(); it != end(); ++it) { const wabbit *const other = *it; if (other != static_cast(this) && dist(this, other) <= radius && this->hungry() > other->bitter()) { step(this, other, dx, dy); return; } } //Arrive here if there were neither enemies nor food nearby: //lethargic in the absence of stimulation. *dx = *dy = 0; }