#include #include //for exit function #include "wolf.h" using namespace std; wolf::wolf(const terminal& initial_t, unsigned initial_x, unsigned initial_y) { t = &initial_t; x = initial_x; y = initial_y; c = 'W'; //Copy lines 13-35 of the above rabbit.C here, //changing the word "rabbit" to "wolf". } //Return false if this wolf ate another animal, true otherwise. bool wolf::move() { struct keystroke { char c; int dx; //horizontal difference int dy; //vertical difference }; static const keystroke a[] = { {'h', -1, 0}, //left {'j', 0, 1}, //down {'k', 0, -1}, //up {'l', 1, 0} //right }; static const size_t n = sizeof a / sizeof a[0]; if (const char k = t->key()) { for (const keystroke *p = a; p < a + n; ++p) { if (k == p->c) { const unsigned newx = x + p->dx; const unsigned newy = y + p->dy; if (!t->in_range(newx, newy)) { break; //Go to line 57. } const bool I_ate_him = t->get(newx, newy) != t->background(); t->put(x, y); //Erase this wolf from its old location. x = newx; y = newy; t->put(x, y, c); //Redraw this wolf at its new location. return !I_ate_him; } } //Punish user who pressed an illegal key or tried to move off screen. t->beep(); } return true; }