Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void road() {
- int choice2;
- int choice3;
- int event1;
- system("cls");
- cout << "You are on a road that heads west and east of your position." << endl;
- cout << "Which way will you go?n" << endl;
- cout << "1. Go West" << endl;
- cout << "2. Go East" << endl;
- cout << "3. Wait for something to happenn" << endl;
- cin >> choice2;
- cin.ignore();
- system("cls");
- if (choice2 == 1) {
- int decision1;
- cout << "You travel down the road, about only 100 metres and you encounter " << endl;
- cout << "a giant spider with vicious poison coated fangs." << endl;
- cout << "its hideous appearance causes your throat to dry and your knees to shake!" << endl;
- cout << "What on earth will you do?nn" << endl;
- cout << "1. Attempt to attack the spider with you sword." << endl;
- cout << "2. Throw your sword in the off chance it might kill it." << endl;
- cout << "3. RUN FOR YOUR LIFE!" << endl;
- cin >> decision1;
- cin.ignore();
- system("cls");
- if (decision1 == 1) {
- cout << "You viscously swing your sword at the spiders general direction." << endl;
- cout << "The swing was so great, your arms jolts out of place, ncreating a surge of pain." << endl;
- cout << "Your arm is now broken, and you fall to the ground in pain...." << endl;
- cout << "The spider launches 3 metres straight into your body... nWhat on earth is it doing?" << endl;
- cout << "Oh My God! The spider is devouring everything...." << endl;
- cout << "All that remained was bones of the once mobile adventurer " << name << endl;
- system("pause");
- gameover();
- }
- else if (decision1 == 2) {}
- else if (decision1 == 3) {}
- }
- else if (choice2 == 2) {
- cout << "After a mile walk, you arrive at an old brick house. " << endl;
- cout << "You walk slowly inside." << endl;
- cout << "The door slams behind you and the room lightens up." << endl;
- cout << "What on earth is going on...?nn" << endl;
- system("pause");
- cout << "nUnable to open the door, you look around for anything of use." << endl;
- cout << "Nothing, not a single piece of furniture." << endl;
- cout << "What will you do?n" << endl;
- cout << "1. Wait for someone to save you." << endl;
- cout << "2. Or Wait for someone to save you." << endl;
- cin >> choice3;
- cin.ignore();
- system("cls");
- if (choice3 == 1) {
- cout << "As you character relaxes his legs and begins to sit" << endl;
- cout << "he leans against the wall, only to hit a hidden switch." << endl;
- system("pause");
- cout << "The floor beneath you disappears, and you start to plummet." << endl;
- cout << "What a trap! You bloody stupid fool!nn?" << endl;
- cout << "somehow you never reach the bottom....." << endl;
- cout << "You die of water deprivation, after only 2 weeks of falling...." << endl;
- system("pause");
- gameover();
- }
- else if (choice3 == 2) {
- system("cls");
- cout << "After only 5 hours..." << endl;
- cout << "The doors suddenly swoosh open." << endl;
- cout << "You are free! " << endl;
- system("pause");
- cout << "You take your first steps into what is now moonlight," << endl;
- cout << "and then suddenly pass out on the ground...." << endl;
- cout << "For some peculiar reason or another, you are now /nback in the creepy dark and gloomy library. You swiftly escape its nostalgia. " << endl;
- road();
- }
- }
- else if (choice2 == 3) {
- int random1;
- cout << "After at least was seemed to be 4 hours " << endl;
- cout << "a dragon swoops down from the heavens...." << endl;
- cout << "and lands right in front of you!n" << endl;
- system("pause");
- system("cls");
- random1 = rand() % 2; // pseudo random generator
- if (random1 == 0) {
- int choice4;
- cout << "Your character screams at the top of his lungs, " << endl;
- cout << "this causes the dragon to immediately to bow in fear..." << endl;
- cout << "It turns out dragons are very sensitive to hearing....." << endl;
- system("pause");
- cout << "nIt seems the dragon is requesting you ride it!n" << endl;
- cout << "Will you ride it?n" << endl;
- cout << "1. Ride it" << endl;
- cout << "2. Or Wait here." << endl;
- cin >> choice4;
- cin.ignore();
- system("cls");
- if (choice4 == 1){
- Ending();
- }
- else if (choice4 == 2){
- road();
- }
- }
- else if (random1 == 1) {
- cout << "Your character screams at the top of his lungs, " << endl;
- cout << "eventually your breath gives out and you die because of lack of oxygen." << endl;
- system("pause");
- gameover();
- }
- }
- }
- class Menu
- {
- public:
- Menu(const string &name, const string &prompt,
- const std::vector<std::pair<string, string> > &choices
- = std::vector<std::pair<string, string> >{});
- virtual ~Menu();
- const string& getChoice() const;
- bool operator==(const string &name) const;
- private:
- static const string menuend;
- string _name, _prompt;
- std::vector<std::pair<string, string> > _choices;
- };
- Menu::Menu(const string &name, const string &prompt,
- const std::vector<std::pair<string, string> > &choices)
- : _name(name), _prompt(prompt), _choices(choices)
- {}
- bool Menu::operator==(const string &name) const
- {
- return name==_name;
- }
- const string& Menu::getChoice() const
- {
- if (_choices.size() == 0) {
- cout << _prompt;
- return menuend;
- }
- unsigned choice;
- int i;
- do {
- cout << _prompt;
- i = 1;
- for (auto ch : _choices)
- cout << i++ << ": " << ch.first << 'n';
- cin >> choice;
- --choice;
- } while (choice >= _choices.size());
- return _choices[choice].second;
- }
- Menu::~Menu()
- {}
- const string Menu::menuend{"END"};
- std::vector<Menu> game{
- Menu("mainroad",
- "You are on a road that heads west and east of your position.n"
- "Which way will you go?n", std::vector<std::pair<string,string> >{
- {"Go West", "spider"},
- {"Go East", "brickhouse"},
- {"Wait for something to happen", "dragon"}}),
- Menu("spider",
- "You travel down the road, about only 100 metres and you encounter n"
- "a giant spider with vicious poison coated fangs.n"
- "its hideous appearance causes your throat to dry and your knees to shake!n"
- "What on earth will you do?nn", std::vector<std::pair<string, string> >{
- {"Attempt to attack the spider with your sword.","spiderattack"},
- {"Throw your sword in the off chance it might kill it.","throwsword"},
- {"RUN FOR YOUR LIFE!", "running"}}),
- Menu("spiderattack",
- "You viscously swing your sword at the spiders general direction.n"
- "The swing was so great, your arms jolts out of place,n"
- "creating a surge of pain.n"
- "Your arm is now broken, and you fall to the ground in pain....n"
- "The spider launches 3 metres straight into your body...n"
- "What on earth is it doing?n"
- "Oh My God! The spider is devouring everything....n"
- "All that remained was bones of the once mobile adventurer.n"),
- Menu("brickhouse",
- "After a mile walk, you arrive at an old brick house.n"
- "You walk slowly inside.n"
- "The door slams behind you and the room lightens up.n"
- "What on earth is going on...?nn"
- "Unable to open the door, you look around for anything of use.n"
- "Nothing, not a single piece of furniture.n"
- "What will you do?n", std::vector<std::pair<string, string> >{
- {"Wait for someone to save you.", "trapdoor"},
- {"Or Wait for someone to save you.", "library"}})
- };
- void road() {
- auto menu = std::find(game.begin(), game.end(), "mainroad");
- while (menu != game.end())
- menu = std::find(game.begin(), game.end(), menu->getChoice());
- }
- random1 = rand() % 2;
- int rand_int(int low, int high)
- {
- static std::default_random_engine re {};
- using Dist = std::uniform_int_distribution<int>;
- static Dist uid {};
- return uid(re, Dist::param_type{low,high});
- }
- if (choice == 1) {
- spider();
- #include <iostream>
- struct state;
- struct transition;
- struct transition {
- char *text;
- struct state *next_state;
- };
- struct state {
- char *text;
- struct transition transitions[8];
- };
- extern struct state start;
- extern struct transition start_transitions[];
- extern struct state spider;
- extern struct transition spider_transitions[];
- struct state start = {
- "You are on a road that heads west and east of your position.n"
- "Which way will you go?",
- {
- {"Go West", &spider},
- {"Go East", NULL},
- {"Wait for something to happen", NULL},
- { NULL }
- }
- };
- struct state spider = {
- "You travel down the road, about only 100 metres and you encountern"
- "a giant spider with vicious poison coated fangs.n"
- "Its hideous appearance causes your throat to dry and your knees to shake!n"
- "What on earth will you do?",
- {
- { "Attempt to attack the spider with your sword.", NULL },
- { "Throw your sword in the off chance it might kill it.", NULL },
- { "RUN FOR YOUR LIFE!", NULL },
- { NULL }
- }
- };
- int main(void)
- {
- state *cur = &start;
- while (cur) {
- // Print text
- std::cout << cur->text << std::endl;
- // List available choices
- unsigned trans = 0;
- while (cur->transitions[trans].text) {
- std::cout << trans << ". " << cur->transitions[trans].text << std::endl;
- trans += 1;
- }
- // Read input
- unsigned choice;
- std::cin >> choice;
- std::cin.ignore();
- // Check input is valid
- if (choice < trans) {
- // Move to new state
- cur = cur->transitions[choice].next_state;
- }
- }
- return 0;
- }
- class Game
- {
- std::string currentLocation; // Assuming a one player game
- // You only need to store that
- // players location.
- std::map<std::string, Location> locations; // All locations in the game
- // You can look them up by name.
- };
- class Location
- {
- std::string description;
- std::map<std::string, std::string> linked; // Places linked to from
- // here. The action is the key
- // the location is the value
- // and you can get the location
- // details by looking up the
- // location in the map.
- std::vector<std::string> thingsLyingHere;
- };
- Example:
- ========
- description: "You are on a road that heads west and east of your position."
- linked["Go West"] = "TheCreepyCrossroads";
- linked["Go East"] = "TheLongRoad";
- linked["Wait for something to happen"] = "Waiting At Start";
- [road]
- You are on a road that heads west and east of your position.
- Which way will you go?
- #west Go West
- #east Go East
- #roadwait Wait for something to happen
- [west]
- A spider!!
- #fightspider
- [east]
- A dragon!
- #endofgame
- [roadwait]
- Very boring.
- #road
- struct Choice {
- std::string text;
- std::string target;
- }
- struct Encounter {
- std::string tag;
- std::string message;
- std::vector<Choice> choices;
- }
- std::string ReadFile(const std::string& filename) {
- /** check online resources */
- }
- Encounter ParseEncounter(const std::string& text) { ... }
- std::map<std::string,Encounter> ParseEncounters(const std::string& text) {
- std::map<std::string,Encounter> encounters;
- size_t pos = 0;
- while(true) {
- // find next [
- size_t next = text.find("[");
- if(next == std::string::npos) break;
- // get the encounter text
- std::string enc_str = text.substr(pos,next);
- // parse encounter
- Encounter enc = ParseEncounter(enc_str);
- // store encounter
- encounters[enc.tag] = enc;
- // next
- pos = next;
- }
- return encounters;
- }
- std::string RunEncounter(const std::map<std::string,Encounter>& encounters, const std::string& tag) {
- system("cls");
- // get encounter
- Encounter node = encounters[tag];
- // print message and choices
- std::cout << node.message << std::endl;
- for(size_t i=0; i<node.choices.size(); i++) {
- std::cout << i << ". " << node.choices[i].text << std::endl;
- }
- // get correct choice from user
- int choice = -1;
- while(!(0 <= choice && choice < node.choices.size())) {
- std::cin >> choice;
- }
- cin.ignore();
- // return choice tag
- return node.choices[choice].target;
- }
- int main() {
- std::string str = ReadFile("adventure.txt");
- std::map<std::string,Encounter> encounters = ParseEncounters(str);
- std::string tag = "road";
- while(tag != "endofgame") {
- tag = RunEncounter(tag);
- }
- }
- struct Choice
- {
- int nextroom;
- std::string text;
- Choice(int n, std::string t) : nextroom(n), text(t) {}
- };
- struct Room
- {
- const std::string text;
- std::vector<Choice> choices;
- bool deathroom;
- Room(std::string t, std::vector<Choice> c, bool d = false)
- : text(t), choices(c), deathroom(d) {}
- };
- std::vector<Room> rooms;
- Room r1(
- "You are in room 1.n",
- std::vector<Choice>{ Choice(2, "Option 1"), Choice(3, "Option 2") });
- rooms.push_back(r1);
- int room = 0;
- while (rooms[room].deathroom == false)
- {
- std::cout << rooms[room].text;
- room = get_choice(room);
- }
- std::cout << "Game Over!n";
- cout << "1. Go West" << endl;
- ...
- if (choice2 == 1) {
- go_west();
- } else if (choice2 == 2) {
- go_east();
- } else if (choice2 == 3) {
- wait();
- }
- switch (choice2) {
- case 1:
- go_west();
- break;
- case 2:
- go_east();
- break;
- case 3:
- wait();
- break;
- }
- #define WEST 1
- #define EAST 2
- #define WAIT 3
- if (choice2 == WEST) {
- go_west();
- } else if (choice2 == EAST) {
- go_east();
- } else if (choice2 == WAIT) {
- wait();
- }
- void(*tbl[4])(void) = {
- &invalid_input,
- &go_west,
- &go_east,
- &wait
- };
- cin >> choice2;
- if (choice2 > 3)
- choice2 = 0;
- tbl[choice2]();
- void printStoryText(std::string storyText) { /* impl */ }
- int decide(std::string decisionText,
- const std::vector<std::string>& choices) { /* impl */ }
- struct Choice
- {
- std::string choice_text;
- std::shared_ptr<DecisionNode> next_node;
- Choice(const std::string& choice, std::shared_ptr<DecisionNode> next)
- : choice_text(choice)
- , next_node(next)
- {}
- };
- class DecisionNode
- {
- public:
- DecisionNode(const std::string& text)
- : story_text_(text)
- {}
- void AddChoice(const std::string& text, std::shared_ptr<DecisionNode> nextNode)
- {
- child_nodes_.push_back(Choice(text, nextNode));
- }
- std::shared_ptr<DecisionNode> Decide() const
- {
- std::vector<Choice>::const_iterator it = choices_.begin();
- for(unsigned int i = 1; it != choices_.end(); ++it, ++i)
- {
- std::cout << i << ". " << it->choice_text << std::endl;
- }
- unsigned int selected;
- std::cin >> selected;
- return choices_.at(selected);
- }
- private:
- std::string story_text_;
- std::vector<Choice> choices_;
- }
Add Comment
Please, Sign In to add comment