diff --git a/src/GameState.cpp b/src/GameState.cpp index 4cffbfd..e727d49 100755 --- a/src/GameState.cpp +++ b/src/GameState.cpp @@ -22,6 +22,10 @@ GameState::GameState(int numberOfRobots) { } GameState::~GameState(){ + clearUnits(); +} + +void GameState::clearUnits(){ for (Unit *unit: units) delete unit; units.clear(); @@ -63,6 +67,23 @@ void GameState::updateCrashes() { } } +GameState &GameState::operator=(const GameState &other) { + if (this == &other) + return *this; + + clearUnits(); + for (const Unit *const unit : other.units) + units.push_back(unit->clone()); + hero = other.hero; + return *this; +} + +GameState::GameState(const GameState &other) : hero(other.hero) { + clearUnits(); + for (const Unit *const unit : other.units) + units.push_back(unit->clone()); +} + int GameState::countToBeJunked() const { int numberDestroyed = 0; for (const Unit *const unit: units) diff --git a/src/GameState.h b/src/GameState.h index 7b6d21e..f4a76f9 100755 --- a/src/GameState.h +++ b/src/GameState.h @@ -27,7 +27,8 @@ public: ~GameState(); - + GameState &operator=(const GameState &other); + GameState(const GameState &other); /* * Clear and redraw entire playing field */ @@ -84,6 +85,7 @@ private: // private helpers bool isEmpty(const Unit& unit) const; + void clearUnits(); }; diff --git a/src/Hero.cpp b/src/Hero.cpp index 311b024..8feda57 100755 --- a/src/Hero.cpp +++ b/src/Hero.cpp @@ -14,3 +14,6 @@ void Hero::draw(QGraphicsScene *scene) const { UNIT_WIDTH, UNIT_HEIGHT), Qt::NoPen, QBrush(HERO_COLOR)); } +Unit *Hero::clone() const { + return new Hero(*this); +} diff --git a/src/Hero.h b/src/Hero.h index 26c8b45..5db8360 100755 --- a/src/Hero.h +++ b/src/Hero.h @@ -12,11 +12,12 @@ class Hero : public Unit { public: + Unit *clone() const override; /* * Draws this hero onto the given QGraphicsScene. */ - void draw(QGraphicsScene *scene) const; + void draw(QGraphicsScene *scene) const override; }; diff --git a/src/Junk.cpp b/src/Junk.cpp index cf75b8f..087d36f 100755 --- a/src/Junk.cpp +++ b/src/Junk.cpp @@ -19,3 +19,7 @@ bool Junk::isAlive() const { return false; } bool Junk::isToBeJunked() const { return false; } void Junk::doCrash() {} void Junk::moveTowards(const Point &) {} + +Unit *Junk::clone() const { + return new Junk(*this); +} diff --git a/src/Junk.h b/src/Junk.h index 4b75fa7..4025f1e 100755 --- a/src/Junk.h +++ b/src/Junk.h @@ -12,7 +12,7 @@ class Junk : public Unit { public: Junk(const Point& p): Unit(p){} - + Unit *clone() const override; /* * Draws this junk onto the given QGraphicsScene. */ diff --git a/src/Robot.cpp b/src/Robot.cpp index 0b76830..920d9aa 100755 --- a/src/Robot.cpp +++ b/src/Robot.cpp @@ -25,4 +25,6 @@ void Robot::draw(QGraphicsScene *scene) const { JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(ROBOT_COLOR)); } - +Unit *Robot::clone() const{ + return new Robot(*this); +} diff --git a/src/Robot.h b/src/Robot.h index 2ce8b63..42ae0bf 100755 --- a/src/Robot.h +++ b/src/Robot.h @@ -13,7 +13,7 @@ class Robot : public Unit { bool toBeJunked = false; public: - + Unit* clone() const override; /* * did not crash yet */ diff --git a/src/Unit.h b/src/Unit.h index ce53e82..d40dbed 100755 --- a/src/Unit.h +++ b/src/Unit.h @@ -28,6 +28,8 @@ public: virtual ~Unit(){} + virtual Unit *clone() const = 0; + /* * Return Point representation of Unit */