#include // in Microsoft #include #include "game.h" #include "alien.h" #include "black_hole.h" #include "boulder.h" #include "deer.h" #include "rabbit.h" #include "sitting_duck.h" #include "wolf.h" using namespace std; void game::populate() throw (fatal, exception) { static const size_t xmax = 8; static const char a[][xmax + 1] = { //+ 1 for terminating '\0' "....s.B.", "B.......", "........", "s.......", ".....d.s", "........", ".......B", ".B.s....", "........", "........", "........", "W.......", #if 0 "bbbbbbbb", //a maze of boulders "b......b", "b.bbbb.b", "b..s.b.b", //The 's' is a sitting duck. "bbbbbb.b", "W......b", //The 'W' is a wolf. "bbbbbbbb" #endif }; static const size_t ymax = sizeof a / sizeof a[0]; for (size_t y = 0; y < ymax; ++y) { for (size_t x = 0; x < xmax; ++x) { if (term.in_range(x, y)) { switch (a[y][x]) { //sorry the y comes before the x case '.': break; case 'A': new alien(this, x, y); break; case 'b': new boulder(this, x, y); break; case 'B': new black_hole(this, x, y); break; case 'd': new deer(this, x, y); break; case 's': new sitting_duck(this, x, y); break; case 'W': new wolf(this, x, y); break; default: throw corrupt(a[y][x], x, y); } } } } } void game::play() { for (;; term.wait(250)) { for (master_t::const_iterator it = master.begin(); it != master.end();) { wabbit *const p = *it; const bool alive = p->move(); ++it; if (!alive) { //The wabbit that moved in line 8 blundered into //another wabbit and was eaten. delete p; } if (count('s') <= 0 && count('d') <= 0 || count('W') <= 0) { unsigned y = 0; if (count('s') <= 0 && count('d') <= 0) { term.put(0, y++, "All s and d are dead!"); } if (count('W') <= 0) { term.put(0, y, "Wolf is dead!"); } return; } } } } game::~game() { term.wait(3000); //Delete any remaining wabbit's. for (master_t::const_iterator it = master.begin(); it != master.end();) { wabbit *const p = *it; ++it; delete p; } } wabbit *game::get(unsigned x, unsigned y) const { for (master_t::const_iterator it = master.begin(); it != master.end(); ++it) { wabbit *const p = *it; if (p->x == x && p->y == y) { return p; } } return 0; } game::master_t::size_type game::count(char c) const { master_t::size_type n = 0; for (master_t::const_iterator it = master.begin(); it != master.end(); ++it) { if ((*it)->c == c) { ++n; } } return n; }