One vector
This commit is contained in:
		@@ -5,115 +5,108 @@
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "GameState.h"
 | 
			
		||||
#include "utilities.h"
 | 
			
		||||
#include "constants.h"
 | 
			
		||||
#include "utilities.h"
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ public:
 | 
			
		||||
     */
 | 
			
		||||
    GameState(int numberOfRobots);
 | 
			
		||||
 | 
			
		||||
    ~GameState() = default;
 | 
			
		||||
    ~GameState();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
@@ -79,8 +79,7 @@ public:
 | 
			
		||||
    Point getHeroAsPoint () const;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::vector<Robot> robots;  // the robots
 | 
			
		||||
    std::vector<Junk> junks;    // robots that have turned to junk
 | 
			
		||||
    std::vector<Unit*> units;
 | 
			
		||||
    Hero hero;                  // the hero
 | 
			
		||||
 | 
			
		||||
    // private helpers
 | 
			
		||||
 
 | 
			
		||||
@@ -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 &) {}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user