diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-11-10 00:57:06 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-11-10 00:57:06 +0100 |
| commit | fc151ff1e1c862f5b40dc028c293ce083695d78c (patch) | |
| tree | 54e0e3f435f500ff0cbf0088b3a61ef642b2e919 /labb4 | |
| parent | b92072fc45676f9fe4669905f38a2aa3d6b92c4d (diff) | |
| download | tddd86-fc151ff1e1c862f5b40dc028c293ce083695d78c.tar.gz | |
gamestate cc, polymorphic clone
Diffstat (limited to 'labb4')
| -rw-r--r-- | labb4/GameState.cpp | 8 | ||||
| -rw-r--r-- | labb4/GameState.h | 2 | ||||
| -rw-r--r-- | labb4/Junk.cpp | 4 | ||||
| -rw-r--r-- | labb4/Junk.h | 2 | ||||
| -rw-r--r-- | labb4/Robot.cpp | 4 | ||||
| -rw-r--r-- | labb4/Robot.h | 2 | ||||
| -rw-r--r-- | labb4/mainwindow.cpp | 7 |
7 files changed, 24 insertions, 5 deletions
diff --git a/labb4/GameState.cpp b/labb4/GameState.cpp index 1a3aeef..3d7b8b7 100644 --- a/labb4/GameState.cpp +++ b/labb4/GameState.cpp @@ -20,6 +20,13 @@ GameState::GameState(int numberOfRobots) { teleportHero(); } +GameState::GameState(const GameState &other) { + for (const auto &robot : other.robots) { + robots.push_back(robot->clone()); + } + hero = other.hero; +} + void GameState::draw(QGraphicsScene *scene) const { scene->clear(); for (int i = 0; i < robots.size(); i++) { @@ -52,7 +59,6 @@ int GameState::countCollisions() { } } } - printf("done, %d\n", numberDestroyed); return numberDestroyed; //int numberDestroyed = 0; diff --git a/labb4/GameState.h b/labb4/GameState.h index d31758d..7466308 100644 --- a/labb4/GameState.h +++ b/labb4/GameState.h @@ -22,6 +22,8 @@ public: GameState(); GameState(int numberOfRobots); + GameState(const GameState &); + /* * Clear and redraw entire playing field */ diff --git a/labb4/Junk.cpp b/labb4/Junk.cpp index fc0edfe..2276240 100644 --- a/labb4/Junk.cpp +++ b/labb4/Junk.cpp @@ -28,3 +28,7 @@ unsigned int Junk::getCollisionCount() const { bool Junk::alive() const { return false; } + +Junk *Junk::clone() const { + return new Junk(*this); +} diff --git a/labb4/Junk.h b/labb4/Junk.h index 730a766..c520e9f 100644 --- a/labb4/Junk.h +++ b/labb4/Junk.h @@ -26,6 +26,8 @@ public: unsigned int getCollisionCount() const; bool alive() const; + + Junk *clone() const; }; #endif // JUNK_H diff --git a/labb4/Robot.cpp b/labb4/Robot.cpp index f25c101..f4c7fb3 100644 --- a/labb4/Robot.cpp +++ b/labb4/Robot.cpp @@ -25,3 +25,7 @@ unsigned int Robot::getCollisionCount() const { bool Robot::alive() const { return true; } + +Robot *Robot::clone() const { + return new Robot(*this); +} diff --git a/labb4/Robot.h b/labb4/Robot.h index 8b445dc..cf05fab 100644 --- a/labb4/Robot.h +++ b/labb4/Robot.h @@ -19,6 +19,8 @@ public: virtual unsigned int getCollisionCount() const; virtual bool alive() const; + + virtual Robot *clone() const; }; #endif // ROBOT_H diff --git a/labb4/mainwindow.cpp b/labb4/mainwindow.cpp index 1d3f5e0..6fd4422 100644 --- a/labb4/mainwindow.cpp +++ b/labb4/mainwindow.cpp @@ -153,11 +153,10 @@ bool MainWindow::tryMove(Hero hero, const Point& point) { * Process results of viable move */ void MainWindow::processMove(bool waiting) { - // GameState copy=gameState; - // copy.moveRobots(); - // gameState=copy; + GameState copy=gameState; + copy.moveRobots(); + gameState=copy; - gameState.moveRobots(); score += gameState.countCollisions() * (POINTS_PER_ROBOT + (waiting ? WAIT_BONUS : 0)); gameState.draw(scene); |
