diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-11-10 00:34:54 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-11-10 00:34:54 +0100 |
| commit | b92072fc45676f9fe4669905f38a2aa3d6b92c4d (patch) | |
| tree | 93abd1a0090433432febfa6b7cc20163ba5c08cc /labb4/GameState.cpp | |
| parent | 6a72f2f15c9f39b2a851b955b9b87abede8d0257 (diff) | |
| download | tddd86-b92072fc45676f9fe4669905f38a2aa3d6b92c4d.tar.gz | |
vector<Robot *>, bool alive(), correct inheritance
Diffstat (limited to 'labb4/GameState.cpp')
| -rw-r--r-- | labb4/GameState.cpp | 93 |
1 files changed, 61 insertions, 32 deletions
diff --git a/labb4/GameState.cpp b/labb4/GameState.cpp index 581d2f7..1a3aeef 100644 --- a/labb4/GameState.cpp +++ b/labb4/GameState.cpp @@ -11,9 +11,10 @@ GameState::GameState(){} GameState::GameState(int numberOfRobots) { for (int i = 0; i < numberOfRobots; i++) { - Robot robot; - do {robot = Robot();} - while (!isEmpty (robot)); + Robot *robot = new Robot; + while (!isEmpty(*robot)) { + robot->teleport(); + } robots.push_back(robot); } teleportHero(); @@ -21,41 +22,61 @@ GameState::GameState(int numberOfRobots) { void GameState::draw(QGraphicsScene *scene) const { scene->clear(); - for (size_t i = 0; i < robots.size(); ++i) - robots[i].draw(scene); - for (size_t i = 0; i < junks.size(); ++i) - junks[i].draw(scene); + for (int i = 0; i < robots.size(); i++) { + robots[i]->draw(scene); + } hero.draw(scene); } void GameState::teleportHero() { - do hero.teleport(); - while (!isEmpty(hero)); + do { + hero.teleport(); + } while (!isEmpty(hero)); } void GameState::moveRobots() { - for (unsigned int i = 0; i < robots.size(); i++) - robots[i].moveTowards (hero); + for (unsigned int i = 0; i < robots.size(); i++) { + robots[i]->moveTowards(hero); + } } int GameState::countCollisions() { int numberDestroyed = 0; - unsigned int i = 0; - while (i < robots.size()) { - bool hitJunk = junkAt (robots[i]); - bool collision = (countRobotsAt (robots[i]) > 1); - if (hitJunk || collision) { - if (!hitJunk) junks.push_back (Junk(robots[i])); - robots[i] = robots[robots.size()-1]; - robots.pop_back(); - numberDestroyed++; - } else i++; + for (int i = 0; i < robots.size(); i++) { + if (robots[i]->alive()) { + if (countRobotsAt(*robots[i]) > 1 || junkAt(*robots[i])) { + numberDestroyed++; + Junk *junk = new Junk(*robots[i]); + delete robots[i]; + robots[i] = junk; + } + } } + printf("done, %d\n", numberDestroyed); return numberDestroyed; + + //int numberDestroyed = 0; + //unsigned int i = 0; + //while (i < robots.size()) { + // bool hitJunk = junkAt (robots[i]); + // bool collision = (countRobotsAt (robots[i]) > 1); + // if (hitJunk || collision) { + // if (!hitJunk) junks.push_back (Junk(robots[i])); + // robots[i] = robots[robots.size()-1]; + // robots.pop_back(); + // numberDestroyed++; + // } else i++; + //} + //return numberDestroyed; } bool GameState::anyRobotsLeft() const { - return (robots.size() != 0); + for (const auto &robot : robots) { + if (robot->alive()) { + return true; + } + } + return false; } bool GameState::heroDead() const { @@ -63,31 +84,38 @@ bool GameState::heroDead() const { } bool GameState::isSafe(const Unit& unit) const { - for (unsigned int i = 0; i < robots.size(); i++) - if (robots[i].attacks(unit)) return false; - if (junkAt(unit)) return false; - return true; + for (int i = 0; i < robots.size(); i++) { + if (robots[i]->attacks(unit)) { + return false; + } + } + return !junkAt(unit); } void GameState::moveHeroTowards(const Unit& dir) { hero.moveTowards(dir); } -Hero GameState::getHero() const {return hero;} +Hero GameState::getHero() const { + return hero; +} /* * Free of robots and junk only */ bool GameState::isEmpty(const Unit& unit) const { - return (countRobotsAt(unit) == 0 && !junkAt(unit)); + return countRobotsAt(unit) == 0; } /* * Is there junk at unit? */ bool GameState::junkAt(const Unit& unit) const { - for (size_t i = 0; i < junks.size(); ++i) - if (junks[i].at(unit)) return true; + for (const auto &robot : robots) { + if (robot->at(unit) && !robot->alive()) { + return true; + } + } return false; } @@ -96,9 +124,10 @@ bool GameState::junkAt(const Unit& unit) const { */ int GameState::countRobotsAt(const Unit& unit) const { int count = 0; - for (size_t i = 0; i < robots.size(); ++i) { - if (robots[i].at(unit)) + for (const auto &robot : robots) { + if (robot->at(unit) && robot->alive()) { count++; + } } return count; } |
