objectivecpp.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* Copyright © 2015 Hein-Pieter van Braam <hp@tmm.cx>
  2. * This work is free. You can redistribute it and/or modify it under the
  3. * terms of the Do What The Fuck You Want To Public License, Version 2,
  4. * as published by Sam Hocevar. See the COPYING file for more details.
  5. *
  6. * Text copied from http://www.firthworks.com/roger/cloak/
  7. */
  8. #include <iostream>
  9. #include <vector>
  10. #include <cstdlib>
  11. #include <cstddef>
  12. #include <readline/readline.h>
  13. #include <readline/history.h>
  14. #define BOLD "\033[1m"
  15. #define NO_BOLD "\033[22m"
  16. using namespace std;
  17. class game;
  18. enum direction {
  19. north,
  20. east,
  21. south,
  22. west
  23. };
  24. class thing {
  25. public:
  26. thing(string name, string description);
  27. virtual ~ thing();
  28. virtual string& name();
  29. virtual string& description();
  30. virtual bool can_examine();
  31. virtual string& examine();
  32. private:
  33. string _name;
  34. string _description;
  35. };
  36. thing::thing(string name, string description):
  37. _name(name), _description(description) {
  38. }
  39. thing::~thing() {
  40. }
  41. string& thing::name() {
  42. return _name;
  43. }
  44. string& thing::description() {
  45. return _description;
  46. }
  47. bool thing::can_examine() {
  48. return true;
  49. }
  50. string& thing::examine() {
  51. return _description;
  52. }
  53. class room : public thing {
  54. friend class game;
  55. public:
  56. room(string name, string description);
  57. virtual ~room();
  58. virtual bool can_north();
  59. virtual room* north();
  60. virtual bool can_east();
  61. virtual room* east();
  62. virtual bool can_south();
  63. virtual room* south();
  64. virtual bool can_west();
  65. virtual room* west();
  66. protected:
  67. room* _north;
  68. room* _east;
  69. room* _south;
  70. room* _west;
  71. private:
  72. bool can_go(direction d);
  73. };
  74. room::room(string name, string description):
  75. thing(name, description) {
  76. }
  77. room::~room() {
  78. }
  79. bool room::can_go(direction d) {
  80. bool success;
  81. switch (d) {
  82. case direction::north:
  83. success = _north ? true : false;
  84. break;
  85. case direction::east:
  86. success = _east ? true : false;
  87. break;
  88. case direction::south:
  89. success = _south ? true : false;
  90. break;
  91. case direction::west:
  92. success = _west ? true : false;
  93. break;
  94. }
  95. if(! success)
  96. cout << "You can't go that way." << endl;
  97. return success;
  98. }
  99. bool room::can_north() {
  100. return can_go(direction::north);
  101. }
  102. room* room::north() {
  103. return _north;
  104. }
  105. bool room::can_east() {
  106. return can_go(direction::east);
  107. }
  108. room* room::east() {
  109. return _east;
  110. }
  111. bool room::can_south() {
  112. return can_go(direction::south);
  113. }
  114. room* room::south() {
  115. return _south;
  116. }
  117. bool room::can_west() {
  118. return can_go(direction::west);
  119. }
  120. room* room::west() {
  121. return _west;
  122. }
  123. class foyer : public room {
  124. public:
  125. foyer();
  126. virtual ~foyer();
  127. bool can_north();
  128. };
  129. foyer::foyer():
  130. room("Foyer of the Opera House",
  131. "You are standing in a spacious hall, splendidly decorated in red and gold, with glittering chandeliers overhead. The entrance from the street is to the north, and there are doorways south and west."
  132. ) {
  133. }
  134. foyer::~foyer() {
  135. }
  136. bool foyer::can_north() {
  137. cout << "You've only just arrived, and besides, the weather outside seems to be getting worse." << endl;
  138. return false;
  139. }
  140. class bar : public room {
  141. public:
  142. bar();
  143. ~bar();
  144. };
  145. bar::bar():
  146. room("Foyer Bar",
  147. "The bar, much rougher than you'd have guessed after the opulence of the foyer to the north, is completely empty. There seems to be some sort of message scrawled in the sawdust on the floor."
  148. ) {
  149. }
  150. bar::~bar() {
  151. }
  152. class cloakroom : public room {
  153. public:
  154. cloakroom();
  155. ~cloakroom();
  156. };
  157. cloakroom::cloakroom():
  158. room("The Cloakroom",
  159. "The walls of this small room were clearly once lined with hooks, though now only one remains. The exit is a door to the east."
  160. ) {
  161. }
  162. cloakroom::~cloakroom() {
  163. }
  164. class game {
  165. public:
  166. game();
  167. ~game();
  168. void parse(string input);
  169. private:
  170. room* _room;
  171. vector<thing*> _things;
  172. };
  173. game::game() {
  174. room *f = new foyer;
  175. room *b = new bar;
  176. room *c = new cloakroom;
  177. f->_south = b;
  178. f->_west = c;
  179. b->_north = f;
  180. c->_east = f;
  181. _room = f;
  182. _things.push_back(f);
  183. _things.push_back(b);
  184. _things.push_back(c);
  185. }
  186. game::~game() {
  187. for (auto thing: _things) {
  188. delete thing;
  189. }
  190. }
  191. void game::parse(string input) {
  192. if (input == "look") {
  193. if (_room->can_examine())
  194. cout << _room->examine() << endl;
  195. } else if (input == "north") {
  196. if (_room->can_north())
  197. _room = _room->north();
  198. } else if (input == "east") {
  199. if (_room->can_east())
  200. _room = _room->east();
  201. } else if (input == "south") {
  202. if (_room->can_south())
  203. _room = _room->south();
  204. } else if (input == "west") {
  205. if (_room->can_west())
  206. _room = _room->west();
  207. } else {
  208. cout << "I don't know how to \"" << input << "\"" << endl;
  209. }
  210. }
  211. int main() {
  212. char *i;
  213. const char *prompt = "> ";
  214. cout << "Hurrying through the rainswept November night, you're glad to see the bright lights of the Opera House. It's surprising that there aren't more people about but, hey, what do you expect in a cheap demo game...?" << endl;
  215. game game;
  216. for (;;) {
  217. cout << endl;
  218. i = readline(prompt);
  219. if (!i)
  220. break;
  221. add_history(i);
  222. game.parse(string(i));
  223. free(i);
  224. }
  225. cout << endl;
  226. return 0;
  227. }