zorkish / TestAdventure.hpp

/*
 * TestAdventure.hpp
 *
 *  Created on: 20/08/2012
 *      Author: rhys
 */

#ifndef TESTADVENTURE_HPP_
#define TESTADVENTURE_HPP_

#include <iostream>
#include <boost/algorithm/string/case_conv.hpp>
#include "HighScoreStage.hpp"
#include "Player.hpp"
#include "util.cpp"

class TestAdventure : public ZorkishStage {
private:
    std::string message;
    Inventory world_inv;
    Player player;
    int moves;
    bool alive;
    bool hiscore;

    void pick_up(std::string name) {
        std::stringstream ss;
        auto it = this->world_inv.take(name);
        if (it) {
            ss << "You pick up " << it->short_name() << ".";
            this->player.inv.add(std::move(it));
        } else {
            ss << "There is no " << name << " here.";
        }

        this->message = ss.str();
    }

    void drop(std::string name) {
        std::stringstream ss;
        auto it = this->player.inv.take(name);
        if (it) {
            ss << "You drop " << it->short_name() << ".";
            this->world_inv.add(std::move(it));
        } else {
            ss << "You're not carrying a " << name << ".";
        }

        this->message = ss.str();
    }

    void list_items(const Inventory& inv) {
        if (inv.empty()) {
            this->message = "There is nothing there.";
            return;
        }

        std::stringstream ss;
        inv.for_each([&](const std::unique_ptr<Item>& item)
                {ss << item->short_name() << '\n';});
        this->message = ss.str();
        this->message.resize(this->message.size() - 1);
    }

public:
    TestAdventure(ZorkishGame& game) : ZorkishStage(game) {
        this->cleanup();
    }

    virtual Status update(ActiveStageManager& smgr) {
        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.get());
            }

            return GameStage::Status::FINISH;
        }

        std::string cmd;
        std::string params;
        std::cin >> cmd;
        getline(std::cin, params);
        boost::to_upper(cmd);
        if (params.empty()) {
            if (cmd == "QUIT") {
                this->message = "Your adventure has ended without fame or fortune.";
                this->alive = false;
            } else if (cmd == "HISCORE") {
                this->message = "You have entered the magic word and will now see the \"New High Score\" screen.";
                this->hiscore = true;
                this->alive = false;
            } else if (cmd == "LOOK") {
                this->list_items(this->world_inv);
            } else if (cmd == "INV") {
                this->list_items(this->player.inv);
            } else {
                this->message = "I don't know how to do that.";
            }
        } else {
            if (cmd == "TAKE") {
                this->pick_up(params.substr(1, params.size() - 1));
            } else if (cmd == "DROP") {
                this->drop(params.substr(1, params.size() - 1));
            } else {
                this->message = "I don't know how to do that.";
            }
        }

        this->moves++;
        return GameStage::Status::CONTINUE;
    }

    virtual void draw() {
        using std::cout;
        if (!this->message.empty()) {
            cout << this->message << '\n';
            this->message.clear();
        }
        if (this->alive) {
            cout << ":> ";
        }
    }

    virtual void cleanup() {
        this->message = "Welcome to Zorkish: Void World\nThis world is simple and pointless. Used it to test Zorkish phase 1 spec.";
        this->moves = 0;
        this->alive = true;
        this->hiscore = false;
        this->player = Player();
        this->world_inv = Inventory();
        this->world_inv.add(make_unique<Item>("delicious cake", std::vector<std::string> {"cake"}));
        this->world_inv.add(make_unique<Item>("a rock", std::vector<std::string> {"rock"}));
    }
};

#endif /* TESTADVENTURE_HPP_ */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.