#include #include //for exit and EXIT_FAILURE #include // in Microsoft #include "game.h" #include "rabbit.h" #include "wolf.h" using namespace std; game::game(char initial_c) : term(initial_c) { const unsigned xmax = term.xmax(); const unsigned ymax = term.ymax(); struct location { unsigned x, y; }; const location a[] = { {2 * xmax / 3, 1 * ymax / 4}, {2 * xmax / 3, 2 * ymax / 4}, {2 * xmax / 3, 3 * ymax / 4} }; const size_t n = sizeof a / sizeof a[0]; for (const location *p = a; p < a + n; ++p) { new(nothrow) rabbit(this, p->x, p->y); } } game::~game() { term.put(0, 0, "You killed all the rabbits!"); term.wait(3000); } void game::play() { const unsigned xmax = term.xmax(); const unsigned ymax = term.ymax(); wolf w(this, xmax / 3, ymax / 2); for (; !master.empty(); term.wait(250)) { w.move(); for (list::const_iterator it = master.begin(); it != master.end();) { rabbit *const p = *it; ++it; if (!p->move()) { delete p; //calls ~rabbit, which calls master.remove } } } } //Return the address of the rabbit at the given location, //or 0 if there is no rabbit there. rabbit *game::get(unsigned x, unsigned y) const { for (list::const_iterator it = master.begin(); it != master.end(); ++it) { rabbit *const p = *it; if (p->x == x && p->y == y) { return p; } } return 0; }