Commits

Rhys ! committed 6fe6aed Draft

using unique_ptr instead of static method variables for stages

Comments (0)

Files changed (6)

         using std::cout;
         cout << "===HELP===\n";
         cout << "The following commands are supported:\n\n";
+        cout << "drop [item]\n";
+        cout << "hiscore\n";
+        cout << "take [item]\n";
         cout << "quit\n";
-        cout << "hiscore\n";
     }
 };
 

MainMenuStage.hpp

     virtual Status update(ActiveStageManager& smgr) {
         switch (this->input_int()) {
         case 1:
-            smgr.set_next_stage(&this->game.select_adventure());
+            smgr.set_next_stage(this->game.select_adventure().get());
             break;
         case 2:
-            smgr.set_next_stage(&this->game.hall_of_fame());
+            smgr.set_next_stage(this->game.hall_of_fame().get());
             break;
         case 3:
-            smgr.set_next_stage(&this->game.help());
+            smgr.set_next_stage(this->game.help().get());
             break;
         case 4:
-            smgr.set_next_stage(&this->game.about());
+            smgr.set_next_stage(this->game.about().get());
             break;
         case 5:
             return GameStage::Status::FINISH;

SelectAdventureStage.hpp

     virtual Status update(ActiveStageManager& smgr) {
         switch (this->input_int()) {
         case 1:
-            smgr.set_next_stage(&this->game.test_adventure());
+            smgr.set_next_stage(this->game.test_adventure().get());
             return GameStage::Status::FINISH;
         default:
             this->message = "Please select a number listed above.";

TestAdventure.hpp

         if (!this->alive) {
             if (this->hiscore) {
                 auto& hs = this->game.high_score();
-                hs.set_score(10, this->moves, "Test Adventure");
-                smgr.set_next_stage(&hs);
+                hs->set_score(10, this->moves, "Test Adventure");
+                smgr.set_next_stage(hs.get());
             }
 
             return GameStage::Status::FINISH;
 #include "MainMenuStage.hpp"
 
 ZorkishGame::ZorkishGame() {
-    this->smgr.set_next_stage(&this->main_menu());
+    this->smgr.set_next_stage(this->main_menu().get());
 }
 
+ZorkishGame::~ZorkishGame() = default;
+
 ActiveStageManager& ZorkishGame::stage_manager() {
     return this->smgr;
 }
 
-// Use of exit-time destructors is intentional
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wexit-time-destructors"
-
-AboutStage& ZorkishGame::about() {
-    static AboutStage s(*this);
-    return s;
+const std::unique_ptr<AboutStage>& ZorkishGame::about() {
+    if (!this->about_stg) {
+        this->about_stg.reset(new AboutStage(*this));
+    }
+    return this->about_stg;
 }
 
-HallOfFameStage& ZorkishGame::hall_of_fame() {
-    static HallOfFameStage s(*this);
-    return s;
+const std::unique_ptr<HallOfFameStage>& ZorkishGame::hall_of_fame() {
+    if (!this->hall_of_fame_stg) {
+        this->hall_of_fame_stg.reset(new HallOfFameStage(*this));
+    }
+    return this->hall_of_fame_stg;
 }
 
-HelpStage& ZorkishGame::help() {
-    static HelpStage s(*this);
-    return s;
+const std::unique_ptr<HelpStage>& ZorkishGame::help() {
+    if (!this->help_stg) {
+        this->help_stg.reset(new HelpStage(*this));
+    }
+    return this->help_stg;
 }
 
-HighScoreStage& ZorkishGame::high_score() {
-    static HighScoreStage s(*this);
-    return s;
+const std::unique_ptr<HighScoreStage>& ZorkishGame::high_score() {
+    if (!this->high_score_stg) {
+        this->high_score_stg.reset(new HighScoreStage(*this));
+    }
+    return this->high_score_stg;
 }
 
-MainMenuStage& ZorkishGame::main_menu() {
-    static MainMenuStage s(*this);
-    return s;
+const std::unique_ptr<MainMenuStage>& ZorkishGame::main_menu() {
+    if (!this->main_menu_stg) {
+        this->main_menu_stg.reset(new MainMenuStage(*this));
+    }
+    return this->main_menu_stg;
 }
 
-SelectAdventureStage& ZorkishGame::select_adventure() {
-    static SelectAdventureStage s(*this);
-    return s;
+const std::unique_ptr<SelectAdventureStage>& ZorkishGame::select_adventure() {
+    if (!this->select_adv_stg) {
+        this->select_adv_stg.reset(new SelectAdventureStage(*this));
+    }
+    return this->select_adv_stg;
 }
 
-TestAdventure& ZorkishGame::test_adventure() {
-    static TestAdventure s(*this);
-    return s;
+const std::unique_ptr<TestAdventure>& ZorkishGame::test_adventure() {
+    if (!this->test_adv_stg) {
+        this->test_adv_stg.reset(new TestAdventure(*this));
+    }
+    return this->test_adv_stg;
 }
-
-#pragma clang diagnostic pop
 #ifndef ZORKISH_HPP_
 #define ZORKISH_HPP_
 
+#include <memory>
 #include "ActiveStageManager.hpp"
 
 class AboutStage;
 class ZorkishGame {
 private:
     ActiveStageManager smgr;
+    std::unique_ptr<AboutStage> about_stg;
+    std::unique_ptr<HallOfFameStage> hall_of_fame_stg;
+    std::unique_ptr<HelpStage> help_stg;
+    std::unique_ptr<HighScoreStage> high_score_stg;
+    std::unique_ptr<MainMenuStage> main_menu_stg;
+    std::unique_ptr<SelectAdventureStage> select_adv_stg;
+    std::unique_ptr<TestAdventure> test_adv_stg;
 
 public:
     ZorkishGame();
+    ~ZorkishGame();
 
     ActiveStageManager& stage_manager();
 
-    AboutStage& about();
-    HallOfFameStage& hall_of_fame();
-    HelpStage& help();
-    HighScoreStage& high_score();
-    MainMenuStage& main_menu();
-    SelectAdventureStage& select_adventure();
-    TestAdventure& test_adventure();
+    const std::unique_ptr<AboutStage>& about();
+    const std::unique_ptr<HallOfFameStage>& hall_of_fame();
+    const std::unique_ptr<HelpStage>& help();
+    const std::unique_ptr<HighScoreStage>& high_score();
+    const std::unique_ptr<MainMenuStage>& main_menu();
+    const std::unique_ptr<SelectAdventureStage>& select_adventure();
+    const std::unique_ptr<TestAdventure>& test_adventure();
 };
 
 #endif /* ZORKISH_HPP_ */