Guest User

Untitled

a guest
Feb 22nd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.06 KB | None | 0 0
  1. void road() {
  2. int choice2;
  3. int choice3;
  4. int event1;
  5. system("cls");
  6. cout << "You are on a road that heads west and east of your position." << endl;
  7. cout << "Which way will you go?n" << endl;
  8.  
  9. cout << "1. Go West" << endl;
  10. cout << "2. Go East" << endl;
  11. cout << "3. Wait for something to happenn" << endl;
  12.  
  13. cin >> choice2;
  14. cin.ignore();
  15. system("cls");
  16.  
  17. if (choice2 == 1) {
  18. int decision1;
  19. cout << "You travel down the road, about only 100 metres and you encounter " << endl;
  20. cout << "a giant spider with vicious poison coated fangs." << endl;
  21. cout << "its hideous appearance causes your throat to dry and your knees to shake!" << endl;
  22. cout << "What on earth will you do?nn" << endl;
  23. cout << "1. Attempt to attack the spider with you sword." << endl;
  24. cout << "2. Throw your sword in the off chance it might kill it." << endl;
  25. cout << "3. RUN FOR YOUR LIFE!" << endl;
  26.  
  27. cin >> decision1;
  28. cin.ignore();
  29. system("cls");
  30.  
  31. if (decision1 == 1) {
  32. cout << "You viscously swing your sword at the spiders general direction." << endl;
  33. cout << "The swing was so great, your arms jolts out of place, ncreating a surge of pain." << endl;
  34. cout << "Your arm is now broken, and you fall to the ground in pain...." << endl;
  35. cout << "The spider launches 3 metres straight into your body... nWhat on earth is it doing?" << endl;
  36. cout << "Oh My God! The spider is devouring everything...." << endl;
  37. cout << "All that remained was bones of the once mobile adventurer " << name << endl;
  38. system("pause");
  39. gameover();
  40. }
  41. else if (decision1 == 2) {}
  42. else if (decision1 == 3) {}
  43.  
  44.  
  45.  
  46. }
  47. else if (choice2 == 2) {
  48. cout << "After a mile walk, you arrive at an old brick house. " << endl;
  49. cout << "You walk slowly inside." << endl;
  50. cout << "The door slams behind you and the room lightens up." << endl;
  51. cout << "What on earth is going on...?nn" << endl;
  52. system("pause");
  53. cout << "nUnable to open the door, you look around for anything of use." << endl;
  54. cout << "Nothing, not a single piece of furniture." << endl;
  55. cout << "What will you do?n" << endl;
  56. cout << "1. Wait for someone to save you." << endl;
  57. cout << "2. Or Wait for someone to save you." << endl;
  58.  
  59. cin >> choice3;
  60. cin.ignore();
  61. system("cls");
  62. if (choice3 == 1) {
  63. cout << "As you character relaxes his legs and begins to sit" << endl;
  64. cout << "he leans against the wall, only to hit a hidden switch." << endl;
  65. system("pause");
  66. cout << "The floor beneath you disappears, and you start to plummet." << endl;
  67. cout << "What a trap! You bloody stupid fool!nn?" << endl;
  68. cout << "somehow you never reach the bottom....." << endl;
  69. cout << "You die of water deprivation, after only 2 weeks of falling...." << endl;
  70. system("pause");
  71. gameover();
  72. }
  73. else if (choice3 == 2) {
  74. system("cls");
  75. cout << "After only 5 hours..." << endl;
  76. cout << "The doors suddenly swoosh open." << endl;
  77. cout << "You are free! " << endl;
  78. system("pause");
  79. cout << "You take your first steps into what is now moonlight," << endl;
  80. cout << "and then suddenly pass out on the ground...." << endl;
  81. cout << "For some peculiar reason or another, you are now /nback in the creepy dark and gloomy library. You swiftly escape its nostalgia. " << endl;
  82. road();
  83. }
  84.  
  85. }
  86.  
  87. else if (choice2 == 3) {
  88. int random1;
  89. cout << "After at least was seemed to be 4 hours " << endl;
  90. cout << "a dragon swoops down from the heavens...." << endl;
  91. cout << "and lands right in front of you!n" << endl;
  92. system("pause");
  93. system("cls");
  94.  
  95. random1 = rand() % 2; // pseudo random generator
  96. if (random1 == 0) {
  97. int choice4;
  98. cout << "Your character screams at the top of his lungs, " << endl;
  99. cout << "this causes the dragon to immediately to bow in fear..." << endl;
  100. cout << "It turns out dragons are very sensitive to hearing....." << endl;
  101. system("pause");
  102. cout << "nIt seems the dragon is requesting you ride it!n" << endl;
  103. cout << "Will you ride it?n" << endl;
  104. cout << "1. Ride it" << endl;
  105. cout << "2. Or Wait here." << endl;
  106.  
  107. cin >> choice4;
  108. cin.ignore();
  109. system("cls");
  110.  
  111. if (choice4 == 1){
  112. Ending();
  113. }
  114. else if (choice4 == 2){
  115. road();
  116. }
  117.  
  118. }
  119. else if (random1 == 1) {
  120. cout << "Your character screams at the top of his lungs, " << endl;
  121. cout << "eventually your breath gives out and you die because of lack of oxygen." << endl;
  122. system("pause");
  123. gameover();
  124. }
  125.  
  126. }
  127. }
  128.  
  129. class Menu
  130. {
  131. public:
  132. Menu(const string &name, const string &prompt,
  133. const std::vector<std::pair<string, string> > &choices
  134. = std::vector<std::pair<string, string> >{});
  135. virtual ~Menu();
  136. const string& getChoice() const;
  137. bool operator==(const string &name) const;
  138. private:
  139. static const string menuend;
  140. string _name, _prompt;
  141. std::vector<std::pair<string, string> > _choices;
  142. };
  143.  
  144. Menu::Menu(const string &name, const string &prompt,
  145. const std::vector<std::pair<string, string> > &choices)
  146. : _name(name), _prompt(prompt), _choices(choices)
  147. {}
  148.  
  149. bool Menu::operator==(const string &name) const
  150. {
  151. return name==_name;
  152. }
  153.  
  154. const string& Menu::getChoice() const
  155. {
  156. if (_choices.size() == 0) {
  157. cout << _prompt;
  158. return menuend;
  159. }
  160. unsigned choice;
  161. int i;
  162. do {
  163. cout << _prompt;
  164. i = 1;
  165. for (auto ch : _choices)
  166. cout << i++ << ": " << ch.first << 'n';
  167. cin >> choice;
  168. --choice;
  169. } while (choice >= _choices.size());
  170. return _choices[choice].second;
  171. }
  172.  
  173. Menu::~Menu()
  174. {}
  175.  
  176. const string Menu::menuend{"END"};
  177.  
  178. std::vector<Menu> game{
  179. Menu("mainroad",
  180. "You are on a road that heads west and east of your position.n"
  181. "Which way will you go?n", std::vector<std::pair<string,string> >{
  182. {"Go West", "spider"},
  183. {"Go East", "brickhouse"},
  184. {"Wait for something to happen", "dragon"}}),
  185. Menu("spider",
  186. "You travel down the road, about only 100 metres and you encounter n"
  187. "a giant spider with vicious poison coated fangs.n"
  188. "its hideous appearance causes your throat to dry and your knees to shake!n"
  189. "What on earth will you do?nn", std::vector<std::pair<string, string> >{
  190. {"Attempt to attack the spider with your sword.","spiderattack"},
  191. {"Throw your sword in the off chance it might kill it.","throwsword"},
  192. {"RUN FOR YOUR LIFE!", "running"}}),
  193. Menu("spiderattack",
  194. "You viscously swing your sword at the spiders general direction.n"
  195. "The swing was so great, your arms jolts out of place,n"
  196. "creating a surge of pain.n"
  197. "Your arm is now broken, and you fall to the ground in pain....n"
  198. "The spider launches 3 metres straight into your body...n"
  199. "What on earth is it doing?n"
  200. "Oh My God! The spider is devouring everything....n"
  201. "All that remained was bones of the once mobile adventurer.n"),
  202. Menu("brickhouse",
  203. "After a mile walk, you arrive at an old brick house.n"
  204. "You walk slowly inside.n"
  205. "The door slams behind you and the room lightens up.n"
  206. "What on earth is going on...?nn"
  207. "Unable to open the door, you look around for anything of use.n"
  208. "Nothing, not a single piece of furniture.n"
  209. "What will you do?n", std::vector<std::pair<string, string> >{
  210. {"Wait for someone to save you.", "trapdoor"},
  211. {"Or Wait for someone to save you.", "library"}})
  212. };
  213.  
  214. void road() {
  215. auto menu = std::find(game.begin(), game.end(), "mainroad");
  216. while (menu != game.end())
  217. menu = std::find(game.begin(), game.end(), menu->getChoice());
  218. }
  219.  
  220. random1 = rand() % 2;
  221.  
  222. int rand_int(int low, int high)
  223. {
  224. static std::default_random_engine re {};
  225. using Dist = std::uniform_int_distribution<int>;
  226. static Dist uid {};
  227. return uid(re, Dist::param_type{low,high});
  228. }
  229.  
  230. if (choice == 1) {
  231. spider();
  232.  
  233. #include <iostream>
  234.  
  235. struct state;
  236. struct transition;
  237.  
  238. struct transition {
  239. char *text;
  240. struct state *next_state;
  241. };
  242.  
  243. struct state {
  244. char *text;
  245. struct transition transitions[8];
  246. };
  247.  
  248. extern struct state start;
  249. extern struct transition start_transitions[];
  250. extern struct state spider;
  251. extern struct transition spider_transitions[];
  252.  
  253. struct state start = {
  254. "You are on a road that heads west and east of your position.n"
  255. "Which way will you go?",
  256. {
  257. {"Go West", &spider},
  258. {"Go East", NULL},
  259. {"Wait for something to happen", NULL},
  260. { NULL }
  261. }
  262. };
  263.  
  264. struct state spider = {
  265. "You travel down the road, about only 100 metres and you encountern"
  266. "a giant spider with vicious poison coated fangs.n"
  267. "Its hideous appearance causes your throat to dry and your knees to shake!n"
  268. "What on earth will you do?",
  269. {
  270. { "Attempt to attack the spider with your sword.", NULL },
  271. { "Throw your sword in the off chance it might kill it.", NULL },
  272. { "RUN FOR YOUR LIFE!", NULL },
  273. { NULL }
  274. }
  275. };
  276.  
  277.  
  278. int main(void)
  279. {
  280. state *cur = &start;
  281. while (cur) {
  282. // Print text
  283. std::cout << cur->text << std::endl;
  284.  
  285. // List available choices
  286. unsigned trans = 0;
  287. while (cur->transitions[trans].text) {
  288. std::cout << trans << ". " << cur->transitions[trans].text << std::endl;
  289. trans += 1;
  290. }
  291.  
  292. // Read input
  293. unsigned choice;
  294. std::cin >> choice;
  295. std::cin.ignore();
  296.  
  297. // Check input is valid
  298. if (choice < trans) {
  299. // Move to new state
  300. cur = cur->transitions[choice].next_state;
  301. }
  302. }
  303. return 0;
  304. }
  305.  
  306. class Game
  307. {
  308. std::string currentLocation; // Assuming a one player game
  309. // You only need to store that
  310. // players location.
  311.  
  312. std::map<std::string, Location> locations; // All locations in the game
  313. // You can look them up by name.
  314. };
  315.  
  316. class Location
  317. {
  318. std::string description;
  319. std::map<std::string, std::string> linked; // Places linked to from
  320. // here. The action is the key
  321. // the location is the value
  322. // and you can get the location
  323. // details by looking up the
  324. // location in the map.
  325. std::vector<std::string> thingsLyingHere;
  326. };
  327.  
  328. Example:
  329. ========
  330. description: "You are on a road that heads west and east of your position."
  331. linked["Go West"] = "TheCreepyCrossroads";
  332. linked["Go East"] = "TheLongRoad";
  333. linked["Wait for something to happen"] = "Waiting At Start";
  334.  
  335. [road]
  336. You are on a road that heads west and east of your position.
  337. Which way will you go?
  338. #west Go West
  339. #east Go East
  340. #roadwait Wait for something to happen
  341.  
  342. [west]
  343. A spider!!
  344. #fightspider
  345.  
  346. [east]
  347. A dragon!
  348. #endofgame
  349.  
  350. [roadwait]
  351. Very boring.
  352. #road
  353.  
  354. struct Choice {
  355. std::string text;
  356. std::string target;
  357. }
  358.  
  359. struct Encounter {
  360. std::string tag;
  361. std::string message;
  362. std::vector<Choice> choices;
  363. }
  364.  
  365. std::string ReadFile(const std::string& filename) {
  366. /** check online resources */
  367. }
  368.  
  369. Encounter ParseEncounter(const std::string& text) { ... }
  370.  
  371. std::map<std::string,Encounter> ParseEncounters(const std::string& text) {
  372. std::map<std::string,Encounter> encounters;
  373. size_t pos = 0;
  374. while(true) {
  375. // find next [
  376. size_t next = text.find("[");
  377. if(next == std::string::npos) break;
  378. // get the encounter text
  379. std::string enc_str = text.substr(pos,next);
  380. // parse encounter
  381. Encounter enc = ParseEncounter(enc_str);
  382. // store encounter
  383. encounters[enc.tag] = enc;
  384. // next
  385. pos = next;
  386. }
  387. return encounters;
  388. }
  389.  
  390. std::string RunEncounter(const std::map<std::string,Encounter>& encounters, const std::string& tag) {
  391. system("cls");
  392. // get encounter
  393. Encounter node = encounters[tag];
  394. // print message and choices
  395. std::cout << node.message << std::endl;
  396. for(size_t i=0; i<node.choices.size(); i++) {
  397. std::cout << i << ". " << node.choices[i].text << std::endl;
  398. }
  399. // get correct choice from user
  400. int choice = -1;
  401. while(!(0 <= choice && choice < node.choices.size())) {
  402. std::cin >> choice;
  403. }
  404. cin.ignore();
  405. // return choice tag
  406. return node.choices[choice].target;
  407. }
  408.  
  409. int main() {
  410. std::string str = ReadFile("adventure.txt");
  411. std::map<std::string,Encounter> encounters = ParseEncounters(str);
  412. std::string tag = "road";
  413. while(tag != "endofgame") {
  414. tag = RunEncounter(tag);
  415. }
  416. }
  417.  
  418. struct Choice
  419. {
  420. int nextroom;
  421. std::string text;
  422.  
  423. Choice(int n, std::string t) : nextroom(n), text(t) {}
  424. };
  425.  
  426. struct Room
  427. {
  428. const std::string text;
  429. std::vector<Choice> choices;
  430. bool deathroom;
  431.  
  432. Room(std::string t, std::vector<Choice> c, bool d = false)
  433. : text(t), choices(c), deathroom(d) {}
  434. };
  435.  
  436. std::vector<Room> rooms;
  437. Room r1(
  438. "You are in room 1.n",
  439. std::vector<Choice>{ Choice(2, "Option 1"), Choice(3, "Option 2") });
  440. rooms.push_back(r1);
  441.  
  442. int room = 0;
  443. while (rooms[room].deathroom == false)
  444. {
  445. std::cout << rooms[room].text;
  446. room = get_choice(room);
  447. }
  448. std::cout << "Game Over!n";
  449.  
  450. cout << "1. Go West" << endl;
  451. ...
  452.  
  453. if (choice2 == 1) {
  454. go_west();
  455. } else if (choice2 == 2) {
  456. go_east();
  457. } else if (choice2 == 3) {
  458. wait();
  459. }
  460.  
  461. switch (choice2) {
  462.  
  463. case 1:
  464. go_west();
  465. break;
  466. case 2:
  467. go_east();
  468. break;
  469. case 3:
  470. wait();
  471. break;
  472. }
  473.  
  474. #define WEST 1
  475. #define EAST 2
  476. #define WAIT 3
  477.  
  478. if (choice2 == WEST) {
  479. go_west();
  480. } else if (choice2 == EAST) {
  481. go_east();
  482. } else if (choice2 == WAIT) {
  483. wait();
  484. }
  485.  
  486. void(*tbl[4])(void) = {
  487. &invalid_input,
  488. &go_west,
  489. &go_east,
  490. &wait
  491. };
  492.  
  493. cin >> choice2;
  494.  
  495. if (choice2 > 3)
  496. choice2 = 0;
  497.  
  498. tbl[choice2]();
  499.  
  500. void printStoryText(std::string storyText) { /* impl */ }
  501.  
  502. int decide(std::string decisionText,
  503. const std::vector<std::string>& choices) { /* impl */ }
  504.  
  505. struct Choice
  506. {
  507. std::string choice_text;
  508. std::shared_ptr<DecisionNode> next_node;
  509.  
  510. Choice(const std::string& choice, std::shared_ptr<DecisionNode> next)
  511. : choice_text(choice)
  512. , next_node(next)
  513. {}
  514. };
  515.  
  516. class DecisionNode
  517. {
  518. public:
  519. DecisionNode(const std::string& text)
  520. : story_text_(text)
  521. {}
  522.  
  523. void AddChoice(const std::string& text, std::shared_ptr<DecisionNode> nextNode)
  524. {
  525. child_nodes_.push_back(Choice(text, nextNode));
  526. }
  527.  
  528. std::shared_ptr<DecisionNode> Decide() const
  529. {
  530. std::vector<Choice>::const_iterator it = choices_.begin();
  531. for(unsigned int i = 1; it != choices_.end(); ++it, ++i)
  532. {
  533. std::cout << i << ". " << it->choice_text << std::endl;
  534. }
  535.  
  536. unsigned int selected;
  537. std::cin >> selected;
  538.  
  539. return choices_.at(selected);
  540. }
  541.  
  542. private:
  543. std::string story_text_;
  544. std::vector<Choice> choices_;
  545. }
Add Comment
Please, Sign In to add comment