Skip to main content
added 8 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188

Grouping functionality in functions would be a first step. You could add functions like that:

void printStoryText(std::string storyText) { /* impl */ } int decide(std::string decisionText, const std::vector<std::string>& choices) { /* impl */ } 

Put the coutcout and pausepause statements in the printStoryTextprintStoryText. Decisions should be done with the decide function which accepts a string containing the decision description and a vector of strings with the possible choices. It returns the index of the chosen element.

Add individual functions for each step of your story and call them as result of the decision outcome. Make separate functions for the brick house, the dragon, etc.

Of course this strategy won't be sufficient as your adventure game grows bigger. I would suggest to abstract the game logic into a data structure suitable for that task. A tree would be the best choice I think. The following code samples should illustrate the idea, they are not complete (most importantly they are missing most of the public interface):

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_; } 

You would then build a tree first before actually starting the game logic by creating a root node with the top level story and the following decision. Each choice of a decision has a choice text and a next node which will be selected when chosen. The Decide()Decide() function returns the next node. This will absorb the many if/else statements that you would need otherwise into the tree structure.

Grouping functionality in functions would be a first step. You could add functions like that:

void printStoryText(std::string storyText) { /* impl */ } int decide(std::string decisionText, const std::vector<std::string>& choices) { /* impl */ } 

Put the cout and pause statements in the printStoryText. Decisions should be done with the decide function which accepts a string containing the decision description and a vector of strings with the possible choices. It returns the index of the chosen element.

Add individual functions for each step of your story and call them as result of the decision outcome. Make separate functions for the brick house, the dragon, etc.

Of course this strategy won't be sufficient as your adventure game grows bigger. I would suggest to abstract the game logic into a data structure suitable for that task. A tree would be the best choice I think. The following code samples should illustrate the idea, they are not complete (most importantly they are missing most of the public interface):

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_; } 

You would then build a tree first before actually starting the game logic by creating a root node with the top level story and the following decision. Each choice of a decision has a choice text and a next node which will be selected when chosen. The Decide() function returns the next node. This will absorb the many if/else statements that you would need otherwise into the tree structure.

Grouping functionality in functions would be a first step. You could add functions like that:

void printStoryText(std::string storyText) { /* impl */ } int decide(std::string decisionText, const std::vector<std::string>& choices) { /* impl */ } 

Put the cout and pause statements in the printStoryText. Decisions should be done with the decide function which accepts a string containing the decision description and a vector of strings with the possible choices. It returns the index of the chosen element.

Add individual functions for each step of your story and call them as result of the decision outcome. Make separate functions for the brick house, the dragon, etc.

Of course this strategy won't be sufficient as your adventure game grows bigger. I would suggest to abstract the game logic into a data structure suitable for that task. A tree would be the best choice I think. The following code samples should illustrate the idea, they are not complete (most importantly they are missing most of the public interface):

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_; } 

You would then build a tree first before actually starting the game logic by creating a root node with the top level story and the following decision. Each choice of a decision has a choice text and a next node which will be selected when chosen. The Decide() function returns the next node. This will absorb the many if/else statements that you would need otherwise into the tree structure.

Post Merged (destination) from codereview.stackexchange.com/questions/49846/…
Post Migrated Here from stackoverflow.com (revisions)
Source Link
jasal
jasal

Grouping functionality in functions would be a first step. You could add functions like that:

void printStoryText(std::string storyText) { /* impl */ } int decide(std::string decisionText, const std::vector<std::string>& choices) { /* impl */ } 

Put the cout and pause statements in the printStoryText. Decisions should be done with the decide function which accepts a string containing the decision description and a vector of strings with the possible choices. It returns the index of the chosen element.

Add individual functions for each step of your story and call them as result of the decision outcome. Make separate functions for the brick house, the dragon, etc.

Of course this strategy won't be sufficient as your adventure game grows bigger. I would suggest to abstract the game logic into a data structure suitable for that task. A tree would be the best choice I think. The following code samples should illustrate the idea, they are not complete (most importantly they are missing most of the public interface):

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_; } 

You would then build a tree first before actually starting the game logic by creating a root node with the top level story and the following decision. Each choice of a decision has a choice text and a next node which will be selected when chosen. The Decide() function returns the next node. This will absorb the many if/else statements that you would need otherwise into the tree structure.