One vector
This commit is contained in:
		@@ -5,102 +5,99 @@
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "GameState.h"
 | 
					#include "GameState.h"
 | 
				
			||||||
#include "utilities.h"
 | 
					 | 
				
			||||||
#include "constants.h"
 | 
					#include "constants.h"
 | 
				
			||||||
 | 
					#include "utilities.h"
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
GameState::GameState(int numberOfRobots) {
 | 
					GameState::GameState(int numberOfRobots) {
 | 
				
			||||||
    for (int i = 0; i < numberOfRobots; i++) {
 | 
					    for (int i = 0; i < numberOfRobots; i++) {
 | 
				
			||||||
        Robot robot;
 | 
					        auto *robot = new Robot();
 | 
				
			||||||
        while(!isEmpty(robot)){
 | 
					        while (!isEmpty(*robot)) {
 | 
				
			||||||
            robot = Robot();
 | 
					            delete robot;
 | 
				
			||||||
 | 
					            robot = new Robot();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        robots.push_back(robot);
 | 
					        units.push_back(robot);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    teleportHero();
 | 
					    teleportHero();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					GameState::~GameState(){
 | 
				
			||||||
 | 
					    for (Unit *unit: units)
 | 
				
			||||||
 | 
					        delete unit;
 | 
				
			||||||
 | 
					    units.clear();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void GameState::draw(QGraphicsScene *scene) const {
 | 
					void GameState::draw(QGraphicsScene *scene) const {
 | 
				
			||||||
    scene->clear();
 | 
					    scene->clear();
 | 
				
			||||||
    hero.draw(scene);
 | 
					    hero.draw(scene);
 | 
				
			||||||
    for (const Robot& robot: robots)
 | 
					    for (const Unit *const unit : units)
 | 
				
			||||||
        robot.draw(scene);
 | 
					        unit->draw(scene);
 | 
				
			||||||
    for (const Junk& junk: junks)
 | 
					 | 
				
			||||||
        junk.draw(scene);
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void GameState::teleportHero() {
 | 
					void GameState::teleportHero() {
 | 
				
			||||||
    do hero.teleport();
 | 
					    do
 | 
				
			||||||
 | 
					        hero.teleport();
 | 
				
			||||||
    while (!isEmpty(hero));
 | 
					    while (!isEmpty(hero));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void GameState::moveRobots() {
 | 
					void GameState::moveRobots() {
 | 
				
			||||||
    for(Robot& robot: robots)
 | 
					    for (Unit *const unit : units)
 | 
				
			||||||
        robot.moveTowards(hero.asPoint());
 | 
					        unit->moveTowards(hero.asPoint());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
void GameState::updateCrashes() {
 | 
					void GameState::updateCrashes() {
 | 
				
			||||||
    for(unsigned i=0; i < robots.size(); ++i){
 | 
					    for (unsigned int i = 0; i < units.size(); i++){
 | 
				
			||||||
        for(unsigned j=0; j < junks.size(); ++j){
 | 
					        Unit *u1 = units[i];
 | 
				
			||||||
            if(robots[i].at(junks[j])){
 | 
					
 | 
				
			||||||
                robots[i].doCrash();
 | 
					        for (unsigned j = i + 1; j < units.size(); ++j) {
 | 
				
			||||||
            }
 | 
					            Unit *u2 = units[j];
 | 
				
			||||||
        }
 | 
					
 | 
				
			||||||
        for(unsigned o=i+1; o < robots.size(); ++o){
 | 
					            if (!u1->at(*u2))
 | 
				
			||||||
            if(robots[i].at(robots[o])){
 | 
					                continue;
 | 
				
			||||||
                robots[i].doCrash();
 | 
					
 | 
				
			||||||
                robots[o].doCrash();
 | 
					            if (u1->isAlive())
 | 
				
			||||||
            }
 | 
					                u1->doCrash();
 | 
				
			||||||
 | 
					            if (u2->isAlive())
 | 
				
			||||||
 | 
					                u2->doCrash();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int GameState::countToBeJunked() const {
 | 
					int GameState::countToBeJunked() const {
 | 
				
			||||||
    int numberDestroyed = 0;
 | 
					    int numberDestroyed = 0;
 | 
				
			||||||
    for(unsigned i=0; i < robots.size(); ++i)
 | 
					    for (const Unit *const unit: units)
 | 
				
			||||||
        if(robots[i].isToBeJunked())
 | 
					        if (unit->isToBeJunked())
 | 
				
			||||||
            numberDestroyed++;
 | 
					            numberDestroyed++;
 | 
				
			||||||
    return numberDestroyed;
 | 
					    return numberDestroyed;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void GameState::junkTheCrashed() {
 | 
					void GameState::junkTheCrashed() {
 | 
				
			||||||
    for(unsigned i=0; i < robots.size(); ++i){
 | 
					    for (unsigned int i = 0; i < units.size(); i++) {
 | 
				
			||||||
        if (robots[i].isToBeJunked()) {
 | 
					        Unit *unit = units[i];
 | 
				
			||||||
            junks.push_back(Junk(robots[i].asPoint()));
 | 
					        if (!unit->isToBeJunked())
 | 
				
			||||||
            robots[i] = robots[robots.size()-1];
 | 
					            continue;
 | 
				
			||||||
            robots.pop_back();
 | 
					
 | 
				
			||||||
        }
 | 
					        auto *junk = new Junk(unit->asPoint());
 | 
				
			||||||
 | 
					        delete unit;
 | 
				
			||||||
 | 
					        units[i] = junk;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool GameState::someRobotsAlive() const {
 | 
					bool GameState::someRobotsAlive() const {
 | 
				
			||||||
    for(unsigned i=0; i < robots.size(); ++i)
 | 
					    for (const Unit *const unit : units)
 | 
				
			||||||
        if(robots[i].isAlive())
 | 
					        if (unit->isAlive()) // Only robots can return true here
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
bool GameState::heroDead() const {
 | 
					bool GameState::heroDead() const {
 | 
				
			||||||
    for(const Robot& robot: robots){
 | 
					    for (const Unit *const unit : units)
 | 
				
			||||||
        if(hero.at(robot)){
 | 
					        if (hero.at(*unit))
 | 
				
			||||||
            return true;
 | 
					            return true;
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    for(const Junk& junk: junks){
 | 
					 | 
				
			||||||
        if(hero.at(junk)){
 | 
					 | 
				
			||||||
            return true;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    return false;
 | 
					    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(); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -108,12 +105,8 @@ Point GameState::getHeroAsPoint() const {return hero.asPoint();}
 | 
				
			|||||||
 * Free of robots and junk
 | 
					 * Free of robots and junk
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
bool GameState::isEmpty(const Unit &unit) const {
 | 
					bool GameState::isEmpty(const Unit &unit) const {
 | 
				
			||||||
    for(const Robot& robot: robots)
 | 
					    for (const Unit *const iterUnit : units)
 | 
				
			||||||
        if(robot.at(unit))
 | 
					        if (iterUnit->at(unit))
 | 
				
			||||||
            return false;
 | 
					 | 
				
			||||||
    for(const Junk& junk: junks)
 | 
					 | 
				
			||||||
        if(junk.at(unit))
 | 
					 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
    return true;
 | 
					    return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -25,7 +25,7 @@ public:
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    GameState(int numberOfRobots);
 | 
					    GameState(int numberOfRobots);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ~GameState() = default;
 | 
					    ~GameState();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /*
 | 
					    /*
 | 
				
			||||||
@@ -79,8 +79,7 @@ public:
 | 
				
			|||||||
    Point getHeroAsPoint () const;
 | 
					    Point getHeroAsPoint () const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    std::vector<Robot> robots;  // the robots
 | 
					    std::vector<Unit*> units;
 | 
				
			||||||
    std::vector<Junk> junks;    // robots that have turned to junk
 | 
					 | 
				
			||||||
    Hero hero;                  // the hero
 | 
					    Hero hero;                  // the hero
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // private helpers
 | 
					    // private helpers
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,4 +18,4 @@ void Junk::draw(QGraphicsScene *scene) const {
 | 
				
			|||||||
bool Junk::isAlive() const { return false; }
 | 
					bool Junk::isAlive() const { return false; }
 | 
				
			||||||
bool Junk::isToBeJunked() const { return false; }
 | 
					bool Junk::isToBeJunked() const { return false; }
 | 
				
			||||||
void Junk::doCrash() {}
 | 
					void Junk::doCrash() {}
 | 
				
			||||||
void Junk::moveTowards(const Point&) const {}
 | 
					void Junk::moveTowards(const Point &) {}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,8 +20,7 @@ public:
 | 
				
			|||||||
    bool isAlive() const override;
 | 
					    bool isAlive() const override;
 | 
				
			||||||
    bool isToBeJunked() const override;
 | 
					    bool isToBeJunked() const override;
 | 
				
			||||||
    void doCrash() override;
 | 
					    void doCrash() override;
 | 
				
			||||||
    void moveTowards(const Point&) const;
 | 
					    void moveTowards(const Point&) override;
 | 
				
			||||||
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif // JUNK_H
 | 
					#endif // JUNK_H
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user