Compare commits

...

3 Commits

Author SHA1 Message Date
4e7fcd8705 Add move constructor/operator= 2024-09-11 17:29:35 +02:00
dd61f06a18 Abstract Unit class 2024-09-11 17:18:54 +02:00
c9be0f2d2f Fix memory leak in step 7 2024-09-11 17:18:39 +02:00
10 changed files with 75 additions and 15 deletions

View File

@ -22,11 +22,48 @@ GameState::GameState(int numberOfRobots) {
}
GameState::~GameState(){
clearUnits();
}
void GameState::clearUnits(){
for (Unit *unit: units)
delete unit;
units.clear();
}
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());
}
GameState &GameState::operator=(GameState &&other) noexcept {
if (this == &other)
return *this;
clearUnits();
hero = std::move(other.hero);
units = std::move(other.units);
other.units.clear();
return *this;
}
GameState::GameState(GameState &&other) noexcept : units(std::move(other.units)), hero(std::move(other.hero)) {
other.units.clear();
}
void GameState::draw(QGraphicsScene *scene) const {
scene->clear();
hero.draw(scene);

View File

@ -27,6 +27,13 @@ public:
~GameState();
// Copy
GameState &operator=(const GameState &other);
GameState(const GameState &other);
// Move
GameState &operator=(GameState &&other) noexcept;
GameState(GameState &&other) noexcept;
/*
* Clear and redraw entire playing field
@ -84,6 +91,7 @@ private:
// private helpers
bool isEmpty(const Unit& unit) const;
void clearUnits();
};

View File

@ -14,3 +14,10 @@ void Hero::draw(QGraphicsScene *scene) const {
UNIT_WIDTH, UNIT_HEIGHT), Qt::NoPen, QBrush(HERO_COLOR));
}
Unit *Hero::clone() const {
return new Hero(*this);
}
bool Hero::isAlive() const { return true; }
bool Hero::isToBeJunked() const { return false; }
void Hero::doCrash() {}

View File

@ -12,12 +12,15 @@
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;
bool isAlive() const override;
bool isToBeJunked() const override;
void doCrash() override;
};
#endif // HERO_H

View File

@ -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);
}

View File

@ -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.
*/

View File

@ -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);
}

View File

@ -13,7 +13,7 @@ class Robot : public Unit {
bool toBeJunked = false;
public:
Unit* clone() const override;
/*
* did not crash yet
*/

View File

@ -59,7 +59,4 @@ void Unit::checkBounds() {
}
// Default implementation
bool Unit::isAlive() const { return true; }
bool Unit::isToBeJunked() const { return false; }
void Unit::doCrash() {}
void Unit::draw(QGraphicsScene *) const {}

View File

@ -28,6 +28,8 @@ public:
virtual ~Unit(){}
virtual Unit *clone() const = 0;
/*
* Return Point representation of Unit
*/
@ -55,10 +57,10 @@ public:
*/
double distanceTo(const Unit& u) const;
virtual bool isAlive() const;
virtual bool isToBeJunked() const;
virtual void doCrash();
virtual void draw(QGraphicsScene *scene) const;
virtual bool isAlive() const = 0;
virtual bool isToBeJunked() const = 0;
virtual void doCrash() = 0;
virtual void draw(QGraphicsScene *scene) const = 0;
private:
int x; // x position of this unit
int y; // y position of this unit