summaryrefslogtreecommitdiffstats
path: root/labb4
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2020-11-10 00:34:54 +0100
committerGustav Sörnäs <gustav@sornas.net>2020-11-10 00:34:54 +0100
commitb92072fc45676f9fe4669905f38a2aa3d6b92c4d (patch)
tree93abd1a0090433432febfa6b7cc20163ba5c08cc /labb4
parent6a72f2f15c9f39b2a851b955b9b87abede8d0257 (diff)
downloadtddd86-b92072fc45676f9fe4669905f38a2aa3d6b92c4d.tar.gz
vector<Robot *>, bool alive(), correct inheritance
Diffstat (limited to 'labb4')
-rw-r--r--labb4/GameState.cpp93
-rw-r--r--labb4/GameState.h9
-rw-r--r--labb4/Junk.cpp6
-rw-r--r--labb4/Junk.h4
-rw-r--r--labb4/Robot.cpp8
-rw-r--r--labb4/Robot.h6
6 files changed, 86 insertions, 40 deletions
diff --git a/labb4/GameState.cpp b/labb4/GameState.cpp
index 581d2f7..1a3aeef 100644
--- a/labb4/GameState.cpp
+++ b/labb4/GameState.cpp
@@ -11,9 +11,10 @@ GameState::GameState(){}
GameState::GameState(int numberOfRobots) {
for (int i = 0; i < numberOfRobots; i++) {
- Robot robot;
- do {robot = Robot();}
- while (!isEmpty (robot));
+ Robot *robot = new Robot;
+ while (!isEmpty(*robot)) {
+ robot->teleport();
+ }
robots.push_back(robot);
}
teleportHero();
@@ -21,41 +22,61 @@ GameState::GameState(int numberOfRobots) {
void GameState::draw(QGraphicsScene *scene) const {
scene->clear();
- for (size_t i = 0; i < robots.size(); ++i)
- robots[i].draw(scene);
- for (size_t i = 0; i < junks.size(); ++i)
- junks[i].draw(scene);
+ for (int i = 0; i < robots.size(); i++) {
+ robots[i]->draw(scene);
+ }
hero.draw(scene);
}
void GameState::teleportHero() {
- do hero.teleport();
- while (!isEmpty(hero));
+ do {
+ hero.teleport();
+ } while (!isEmpty(hero));
}
void GameState::moveRobots() {
- for (unsigned int i = 0; i < robots.size(); i++)
- robots[i].moveTowards (hero);
+ for (unsigned int i = 0; i < robots.size(); i++) {
+ robots[i]->moveTowards(hero);
+ }
}
int GameState::countCollisions() {
int numberDestroyed = 0;
- unsigned int i = 0;
- while (i < robots.size()) {
- bool hitJunk = junkAt (robots[i]);
- bool collision = (countRobotsAt (robots[i]) > 1);
- if (hitJunk || collision) {
- if (!hitJunk) junks.push_back (Junk(robots[i]));
- robots[i] = robots[robots.size()-1];
- robots.pop_back();
- numberDestroyed++;
- } else i++;
+ for (int i = 0; i < robots.size(); i++) {
+ if (robots[i]->alive()) {
+ if (countRobotsAt(*robots[i]) > 1 || junkAt(*robots[i])) {
+ numberDestroyed++;
+ Junk *junk = new Junk(*robots[i]);
+ delete robots[i];
+ robots[i] = junk;
+ }
+ }
}
+ printf("done, %d\n", numberDestroyed);
return numberDestroyed;
+
+ //int numberDestroyed = 0;
+ //unsigned int i = 0;
+ //while (i < robots.size()) {
+ // bool hitJunk = junkAt (robots[i]);
+ // bool collision = (countRobotsAt (robots[i]) > 1);
+ // if (hitJunk || collision) {
+ // if (!hitJunk) junks.push_back (Junk(robots[i]));
+ // robots[i] = robots[robots.size()-1];
+ // robots.pop_back();
+ // numberDestroyed++;
+ // } else i++;
+ //}
+ //return numberDestroyed;
}
bool GameState::anyRobotsLeft() const {
- return (robots.size() != 0);
+ for (const auto &robot : robots) {
+ if (robot->alive()) {
+ return true;
+ }
+ }
+ return false;
}
bool GameState::heroDead() const {
@@ -63,31 +84,38 @@ bool GameState::heroDead() const {
}
bool GameState::isSafe(const Unit& unit) const {
- for (unsigned int i = 0; i < robots.size(); i++)
- if (robots[i].attacks(unit)) return false;
- if (junkAt(unit)) return false;
- return true;
+ for (int i = 0; i < robots.size(); i++) {
+ if (robots[i]->attacks(unit)) {
+ return false;
+ }
+ }
+ return !junkAt(unit);
}
void GameState::moveHeroTowards(const Unit& dir) {
hero.moveTowards(dir);
}
-Hero GameState::getHero() const {return hero;}
+Hero GameState::getHero() const {
+ return hero;
+}
/*
* Free of robots and junk only
*/
bool GameState::isEmpty(const Unit& unit) const {
- return (countRobotsAt(unit) == 0 && !junkAt(unit));
+ return countRobotsAt(unit) == 0;
}
/*
* Is there junk at unit?
*/
bool GameState::junkAt(const Unit& unit) const {
- for (size_t i = 0; i < junks.size(); ++i)
- if (junks[i].at(unit)) return true;
+ for (const auto &robot : robots) {
+ if (robot->at(unit) && !robot->alive()) {
+ return true;
+ }
+ }
return false;
}
@@ -96,9 +124,10 @@ bool GameState::junkAt(const Unit& unit) const {
*/
int GameState::countRobotsAt(const Unit& unit) const {
int count = 0;
- for (size_t i = 0; i < robots.size(); ++i) {
- if (robots[i].at(unit))
+ for (const auto &robot : robots) {
+ if (robot->at(unit) && robot->alive()) {
count++;
+ }
}
return count;
}
diff --git a/labb4/GameState.h b/labb4/GameState.h
index 2fe2212..d31758d 100644
--- a/labb4/GameState.h
+++ b/labb4/GameState.h
@@ -25,7 +25,7 @@ public:
/*
* Clear and redraw entire playing field
*/
- void draw(QGraphicsScene* scene) const; // Clear and redraw entire playing field
+ void draw(QGraphicsScene* scene) const; // Clear and redraw entire playing field
/*
* Teleport hero to random location
@@ -37,9 +37,8 @@ public:
*/
void moveRobots();
- /* Count colliding robots
- * Also converts robots to junk while
- * checking collisions
+ /* Count colliding robots.
+ * Also converts robots to junk while checking collisions
*/
int countCollisions ();
@@ -69,7 +68,7 @@ public:
Hero getHero () const;
private:
- std::vector<Robot> robots; // the robots and the junk combined
+ std::vector<Robot *> robots; // the robots and the junk combined
Hero hero; // the hero
// private helpers
diff --git a/labb4/Junk.cpp b/labb4/Junk.cpp
index 8686129..fc0edfe 100644
--- a/labb4/Junk.cpp
+++ b/labb4/Junk.cpp
@@ -15,7 +15,7 @@ void Junk::draw(QGraphicsScene *scene) const {
JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(JUNK_COLOR));
}
-void Junk::moveTowards(const Unit& u) const {}
+void Junk::moveTowards(const Unit& u) {}
bool Junk::attacks(const Unit& u) const {
return false;
@@ -24,3 +24,7 @@ bool Junk::attacks(const Unit& u) const {
unsigned int Junk::getCollisionCount() const {
return 1;
};
+
+bool Junk::alive() const {
+ return false;
+}
diff --git a/labb4/Junk.h b/labb4/Junk.h
index a4d6cca..730a766 100644
--- a/labb4/Junk.h
+++ b/labb4/Junk.h
@@ -19,11 +19,13 @@ public:
*/
void draw(QGraphicsScene* scene) const;
- void moveTowards(const Unit& u) const;
+ void moveTowards(const Unit& u);
bool attacks(const Unit& u) const;
unsigned int getCollisionCount() const;
+
+ bool alive() const;
};
#endif // JUNK_H
diff --git a/labb4/Robot.cpp b/labb4/Robot.cpp
index f7d96e0..f25c101 100644
--- a/labb4/Robot.cpp
+++ b/labb4/Robot.cpp
@@ -14,6 +14,14 @@ void Robot::draw(QGraphicsScene *scene) const {
JUNK_RADIUS, JUNK_RADIUS), QPen(), QBrush(ROBOT_COLOR));
}
+void Robot::moveTowards(const Unit& u) {
+ Unit::moveTowards(u);
+}
+
unsigned int Robot::getCollisionCount() const {
return 1;
};
+
+bool Robot::alive() const {
+ return true;
+}
diff --git a/labb4/Robot.h b/labb4/Robot.h
index 9e2c075..8b445dc 100644
--- a/labb4/Robot.h
+++ b/labb4/Robot.h
@@ -12,9 +12,13 @@
class Robot : public Unit {
public:
Robot();
- void draw(QGraphicsScene* scene) const;
+ virtual void draw(QGraphicsScene* scene) const;
+
+ virtual void moveTowards(const Unit& u);
virtual unsigned int getCollisionCount() const;
+
+ virtual bool alive() const;
};
#endif // ROBOT_H