#ifndef WABBITH #define WABBITH #include #include "fatal.h" #include "game.h" class wabbit { game *const g; unsigned x, y; const char c; //Decide which direction to move in. virtual void decide(int *dx, int *dy) const = 0; //move calls punish if this wabbit tries to move off the screen, or //bumps into another wabbit that it can neither eat nor be eaten by. virtual void punish() const {} public: class invisible: public fatal { const unsigned x, y; const char c; void print(ostream& ost) const throw () { ost << "can't put invisible char '" << c << "' at (" << x << ", " << y << ")"; } public: invisible(unsigned initial_x, unsigned initial_y, char initial_c): x(initial_x), y(initial_y), c(initial_c) {} }; class fullscreen: public fatal { const unsigned x, y; const char c; void print(ostream& ost) const throw() { ost << "screen is full, can't put '" << c << "' at (" << x << ", " << y << ")"; } public: fullscreen(unsigned initial_x, unsigned initial_y, char initial_c): x(initial_x), y(initial_y), c(initial_c) {} }; class cross_game: public fatal { void print(ostream& ost) const throw() { ost << "different games"; } }; wabbit(game *initial_g, unsigned initial_x, unsigned initial_y, char initial_c); wabbit(const wabbit& another); wabbit& operator=(const wabbit& another); virtual ~wabbit() {g->term.beep(); g->master.remove(this); g->term.put(x, y);} bool move() throw (terminal::unprintable, terminal::out_of_range); //wabbit w1 will eat wabbit w2 if w1.hungry() > w2.bitter(), i.e., //if w1's hunger is stronger than w2's bitterness. virtual int hungry() const = 0; virtual int bitter() const = 0; //Functions that use the private data members of class wabbit. friend wabbit *game::get(unsigned x, unsigned y) const; friend game::master_t::size_type game::count(char c) const; protected: //Functions called only by the member functions of classes derived from //class wabbit. char key() const {return g->term.key();} //called by manual::decide void beep() const {g->term.beep();} //called by manual::punish //called by visionary::decide friend void difference(const wabbit *w1, const wabbit *w2, int *dx, int *dy) throw (cross_game); //called by visionary::decide typedef game::master_t::const_iterator const_iterator; const_iterator begin() const {return g->master.begin();} const_iterator end() const {return g->master.end();} }; #endif