Skip to content

Instantly share code, notes, and snippets.

@zachlatta
Created April 14, 2012 22:06
Show Gist options
  • Save zachlatta/2388084 to your computer and use it in GitHub Desktop.
Save zachlatta/2388084 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
// Creates the structure Scenea
struct Scene {
// Constructor that passes the arguments to text and to next which sets scene_text to the value of text and next_scenes to the value of next
Scene(std::string text, std::vector<int> const& next) {
scene_text = text;
next_scenes = next;
}
// Declares scene_text as type string
std::string scene_text;
// Creates the vector next_scenes
std::vector<int> next_scenes;
};
int main() {
// Creates a vector of the type Scene called scenes
std::vector<Scene> scenes;
// Creates an input stream called "in" that reads from "data.scn"
std::ifstream in("data.scn");
while(true) {
// Creates the string scene_text and sets its value to ""
std::string scene_text = "";
// If the user does not enter anything, then break
if (!std::getline(std::cin, scene_text))
break;
// Creates a vector of type int titled "choices"
std::vector<int> choices;
// Creates an integer titled choice and sets it to 0
int choice = 0;
// While the user enters something to be stored in choice, create a new memory cell in the vector choices with the value of choice
while (std::cin >> choice)
choices.push_back(choice);
// Ignore the previous newline characters in the cin buffer 1024 times
std::cin.ignore('\n', 1024); // 1024 can be any big number.
// Add a new memory cell to the vector scenes containing an instance of Scene with the arguements scene_text and choices
scenes.push_back(Scene(scene_text, choices));
}
// Create an integer titled "current"
int current = 0;
// While true != null...
while (true) {
// Create a variable titled "choice" with the value 0
int choice = 0;
// Output the content of the scene with the value of current's member variable scene_text then go to the next line
std::cout << scenes[current].scene_text << std::endl;
// Take in the user's choice and store it in choice
std::cin >> choice;
// Sets current to the value of the vector scene's memory cell with the value of current's member variable next_scenes with the memory cell choice
current = scenes[current].next_scenes[choice];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment