skip to content
tlam://snippet
cd ..

Dream or nightmare?

Just messing around with C++ as I can't really code anything useful in it...

dream_or_nightmare.cpp · 60 lines · cpp
#include <iostream>#include <vector>#include <cstdlib>#include <ctime>class Dream {protected:    std::string content;    double serenityLevel; // 0 (pure terror) to 1 (absolute peace)public:    Dream(std::string content, double serenity) : content(content), serenityLevel(serenity) {}    virtual void experience() const {        std::cout << "Drifting into a dream... " << content << "\n";    }    bool isNightmare() const {        return serenityLevel < 0.3;    }};class Nightmare : public Dream {private:    std::vector<std::string> fears = {"Falling", "Being chased", "Losing something", "Darkness", "Endless maze"};public:    Nightmare(std::string baseContent) : Dream(baseContent, (std::rand() % 30) / 100.0) {        content += " but something feels off... " + fears[std::rand() % fears.size()];    }    void experience() const override {        std::cout << "Slipping into a nightmare... " << content << "\n";    }};class Mind {public:    void sleep() {        std::srand(std::time(0));        Dream* dream;        if (std::rand() % 2 == 0) {            dream = new Dream("Flying over mountains", 0.8);        } else {            dream = new Nightmare("Walking through a quiet forest");        }        dream->experience();        if (dream->isNightmare()) {            std::cout << "You wake up sweating... it was a nightmare!\n";        } else {            std::cout << "A peaceful dream fades as morning arrives.\n";        }        delete dream;    }};int main() {    Mind myMind;    myMind.sleep();    return 0;}

replies (0)

login to reply.

// no replies yet