#include #include #include "wabbit.h" using namespace std; wabbit::wabbit(game *initial_g, unsigned initial_x, unsigned initial_y, char initial_c) : g(initial_g), x(initial_x), y(initial_y), c(initial_c) { if (c == g->term.background()) { throw invisible(x, y, c); } while (g->term.get(x, y) != g->term.background()) { g->term.next(x, y); if (!g->term.in_range(x, y)) { throw fullscreen(x, y, c); } } g->term.put(x, y, c); g->master.push_back(this); } /* delete any other wabbit that got eaten during the move (line 30), but do not delete this wabbit. If this wabbit was eaten during the move, return false (line 34); otherwise return true. */ bool wabbit::move() throw (terminal::unprintable, terminal::out_of_range) { int dx; //uninitialized variables int dy; decide(&dx, &dy); if (dx == 0 && dy == 0) { return true; } const unsigned newx = x + dx; const unsigned newy = y + dy; if (!g->term.in_range(newx, newy)) { punish(); return true; } if (wabbit *const other = g->get(newx, newy)) { const bool I_ate_him = this->hungry() > other->bitter(); const bool he_ate_me = other->hungry() > this->bitter(); if (I_ate_him) { delete other; } if (he_ate_me) { return false; } if (!I_ate_him) { //I bumped into another wabbit, and neither of us was eaten. punish(); return true; } } g->term.put(x, y); x = newx; y = newy; g->term.put(x, y, c); return true; } void difference(const wabbit *w1, const wabbit *w2, int *dx, int *dy) throw (wabbit::cross_game) { if (w1->g != w2->g) { throw wabbit::cross_game(); } *dx = static_cast(w2->x) - static_cast(w1->x); *dy = static_cast(w2->y) - static_cast(w1->y); }