From dbfbb27044496571db847e7639d1ac5133088a4b Mon Sep 17 00:00:00 2001 From: Love Billenius Date: Wed, 11 Sep 2024 16:30:01 +0200 Subject: [PATCH] One vector --- src/GameState.cpp | 115 ++++++++++++++++++++++------------------------ src/GameState.h | 5 +- src/Junk.cpp | 2 +- src/Junk.h | 3 +- 4 files changed, 58 insertions(+), 67 deletions(-) diff --git a/src/GameState.cpp b/src/GameState.cpp index e783dc4..4cffbfd 100755 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -5,115 +5,108 @@ */ #include "GameState.h" -#include "utilities.h" #include "constants.h" +#include "utilities.h" #include - GameState::GameState(int numberOfRobots) { - for (int i = 0; i < numberOfRobots; i++) { - Robot robot; - while(!isEmpty(robot)){ - robot = Robot(); + for (int i = 0; i < numberOfRobots; i++) { + auto *robot = new Robot(); + while (!isEmpty(*robot)) { + delete robot; + robot = new Robot(); } - robots.push_back(robot); + units.push_back(robot); } teleportHero(); } +GameState::~GameState(){ + for (Unit *unit: units) + delete unit; + units.clear(); +} + void GameState::draw(QGraphicsScene *scene) const { scene->clear(); hero.draw(scene); - for (const Robot& robot: robots) - robot.draw(scene); - for (const Junk& junk: junks) - junk.draw(scene); + for (const Unit *const unit : units) + unit->draw(scene); } void GameState::teleportHero() { - do hero.teleport(); + do + hero.teleport(); while (!isEmpty(hero)); } void GameState::moveRobots() { - for(Robot& robot: robots) - robot.moveTowards(hero.asPoint()); + for (Unit *const unit : units) + unit->moveTowards(hero.asPoint()); } - void GameState::updateCrashes() { - for(unsigned i=0; i < robots.size(); ++i){ - for(unsigned j=0; j < junks.size(); ++j){ - if(robots[i].at(junks[j])){ - robots[i].doCrash(); - } - } - for(unsigned o=i+1; o < robots.size(); ++o){ - if(robots[i].at(robots[o])){ - robots[i].doCrash(); - robots[o].doCrash(); - } + for (unsigned int i = 0; i < units.size(); i++){ + Unit *u1 = units[i]; + + for (unsigned j = i + 1; j < units.size(); ++j) { + Unit *u2 = units[j]; + + if (!u1->at(*u2)) + continue; + + if (u1->isAlive()) + u1->doCrash(); + if (u2->isAlive()) + u2->doCrash(); } } } -int GameState::countToBeJunked()const{ - int numberDestroyed =0; - for(unsigned i=0; i < robots.size(); ++i) - if(robots[i].isToBeJunked()) +int GameState::countToBeJunked() const { + int numberDestroyed = 0; + for (const Unit *const unit: units) + if (unit->isToBeJunked()) numberDestroyed++; return numberDestroyed; } -void GameState::junkTheCrashed(){ - for(unsigned i=0; i < robots.size(); ++i){ - if (robots[i].isToBeJunked()) { - junks.push_back(Junk(robots[i].asPoint())); - robots[i] = robots[robots.size()-1]; - robots.pop_back(); - } +void GameState::junkTheCrashed() { + for (unsigned int i = 0; i < units.size(); i++) { + Unit *unit = units[i]; + if (!unit->isToBeJunked()) + continue; + + auto *junk = new Junk(unit->asPoint()); + delete unit; + units[i] = junk; } } bool GameState::someRobotsAlive() const { - for(unsigned i=0; i < robots.size(); ++i) - if(robots[i].isAlive()) + for (const Unit *const unit : units) + if (unit->isAlive()) // Only robots can return true here return true; return false; } - bool GameState::heroDead() const { - for(const Robot& robot: robots){ - if(hero.at(robot)){ + for (const Unit *const unit : units) + if (hero.at(*unit)) return true; - } - } - for(const Junk& junk: junks){ - if(hero.at(junk)){ - return true; - } - } return false; } +void GameState::moveHeroTowards(const Point &dir) { hero.moveTowards(dir); } -void GameState::moveHeroTowards(const Point& dir) { - hero.moveTowards(dir); -} - -Point GameState::getHeroAsPoint() const {return hero.asPoint();} +Point GameState::getHeroAsPoint() const { return hero.asPoint(); } /* * Free of robots and junk */ -bool GameState::isEmpty(const Unit& unit) const { - for(const Robot& robot: robots) - if(robot.at(unit)) - return false; - for(const Junk& junk: junks) - if(junk.at(unit)) +bool GameState::isEmpty(const Unit &unit) const { + for (const Unit *const iterUnit : units) + if (iterUnit->at(unit)) return false; return true; } - diff --git a/src/GameState.h b/src/GameState.h index ac85881..7b6d21e 100755 --- a/src/GameState.h +++ b/src/GameState.h @@ -25,7 +25,7 @@ public: */ GameState(int numberOfRobots); - ~GameState() = default; + ~GameState(); /* @@ -79,8 +79,7 @@ public: Point getHeroAsPoint () const; private: - std::vector robots; // the robots - std::vector junks; // robots that have turned to junk + std::vector units; Hero hero; // the hero // private helpers diff --git a/src/Junk.cpp b/src/Junk.cpp index 634b33a..cf75b8f 100755 --- a/src/Junk.cpp +++ b/src/Junk.cpp @@ -18,4 +18,4 @@ void Junk::draw(QGraphicsScene *scene) const { bool Junk::isAlive() const { return false; } bool Junk::isToBeJunked() const { return false; } void Junk::doCrash() {} -void Junk::moveTowards(const Point&) const {} +void Junk::moveTowards(const Point &) {} diff --git a/src/Junk.h b/src/Junk.h index 716acc6..4b75fa7 100755 --- a/src/Junk.h +++ b/src/Junk.h @@ -20,8 +20,7 @@ public: bool isAlive() const override; bool isToBeJunked() const override; void doCrash() override; - void moveTowards(const Point&) const; - + void moveTowards(const Point&) override; }; #endif // JUNK_H