Advertisement
Guest User

Untitled

a guest
May 19th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. /**
  2. * This class is the main class of the "World of Zuul" application.
  3. * "World of Zuul" is a very simple, text based adventure game. Users
  4. * can walk around some scenery. That's all. It should really be extended
  5. * to make it more interesting!
  6. *
  7. * To play this game, create an instance of this class and call the "play"
  8. * method.
  9. *
  10. * This main class creates and initialises all the others: it creates all
  11. * rooms, creates the parser and starts the game. It also evaluates and
  12. * executes the commands that the parser returns.
  13. *
  14. * @author Michael Kolling and David J. Barnes
  15. * @version 1.0 (February 2002)
  16. */
  17.  
  18. public class Game
  19. {
  20. private Parser parser;
  21. private Organ currentOrgan;
  22. private int energy;
  23.  
  24.  
  25. /**
  26. * Create the game and initialise its internal map.
  27. */
  28. public Game()
  29. {
  30. createOrgans();
  31. parser = new Parser();
  32. energy = 3;
  33. }
  34.  
  35. /**
  36. * Create all the rooms and link their exits together.
  37. */
  38. private void createOrgans()
  39. {
  40. Organ circulatorySystem, respitorySystem, upperGI, lowerGI, sinuses;
  41.  
  42. // create the rooms
  43. circulatorySystem = new Organ("in the circulatory system");
  44. respitorySystem = new Organ("int the respitory system");
  45. upperGI = new Organ("in the upper GI");
  46. lowerGI = new Organ("in the lower GI");
  47. sinuses = new Organ("in the sinuses");
  48. // set organ exits
  49. circulatorySystem.setExit("anterior", respitorySystem);
  50. circulatorySystem.setExit ("posterior", upperGI);
  51. respitorySystem.setExit ("anterior", sinuses);
  52. respitorySystem.setExit ("posterior", circulatorySystem );
  53. upperGI.setExit ("anterior", circulatorySystem );
  54. upperGI.setExit ("posterior", lowerGI );
  55. lowerGI.setExit ("anterior", upperGI);
  56. sinuses.setExit ("posterior", respitorySystem);
  57. currentOrgan = circulatorySystem; // start game outside
  58. }
  59.  
  60. /**
  61. * Main play routine. Loops until end of play.
  62. */
  63. public void play()
  64. {
  65. printWelcome();
  66.  
  67. // Enter the main command loop. Here we repeatedly read commands and
  68. // execute them until the game is over.
  69.  
  70. boolean finished = false;
  71. while (! finished) {
  72. Command command = parser.getCommand();
  73. finished = processCommand(command);
  74. }
  75. System.out.println("Thank you for playing. Good bye.");
  76. }
  77.  
  78. /**
  79. * Print out the opening message for the player.
  80. */
  81. private void printWelcome()
  82. {
  83. System.out.println();
  84. System.out.println("Welcome to the World of Zuul!");
  85. System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  86. System.out.println("Type 'help' if you need help.");
  87. System.out.println();
  88. printLocationInfo();
  89. }
  90.  
  91. /**
  92. * Given a command, process (that is: execute) the command.
  93. * If this command ends the game, true is returned, otherwise false is
  94. * returned.
  95. */
  96. private boolean processCommand(Command command)
  97. {
  98. boolean wantToQuit = false;
  99.  
  100. if(command.isUnknown()) {
  101. System.out.println("I don't know what you mean...");
  102. return false;
  103. }
  104.  
  105. String commandWord = command.getCommandWord();
  106. if (commandWord.equals("help"))
  107. printHelp();
  108. else if (commandWord.equals("go"))
  109. goOrgan(command);
  110. else if (commandWord.equals("look"))
  111. look();
  112. else if (commandWord.equals("eat"))
  113. eat();
  114. else if (commandWord.equals ("energy"))
  115. energyLevel();
  116. else if (commandWord.equals("quit"))
  117.  
  118. wantToQuit = quit(command);
  119.  
  120. return wantToQuit;
  121. }
  122.  
  123. // a command to look around the room
  124. private void look()
  125. {
  126. System.out.println(currentOrgan.getLongDescription());
  127. }
  128. // implementations of user commands:
  129.  
  130. /**
  131. * Print out some help information.
  132. * Here we print some stupid, cryptic message and a list of the
  133. * command words.
  134. */
  135. private void printHelp()
  136. {
  137. System.out.println("You are lost. You are alone. You wander");
  138. System.out.println("around at the university.");
  139. System.out.println();
  140. System.out.println("Your command words are:");
  141. System.out.println(" go quit help");
  142. }
  143.  
  144. // eat glucose to power up
  145.  
  146. private void eat()
  147. {
  148. if (currentOrgan.getGlucoseLevel() > 0)
  149. {
  150. energy += 1;
  151. currentOrgan.resetGlucoseLevel();
  152. System.out.println ("you have eaten 1 glucose.");
  153. }
  154. else
  155. {
  156. System.out.println("there is nothing to eat.");
  157. }
  158. }
  159.  
  160. private void energyLevel()
  161. {
  162. System.out.println ("energy level " + energy);
  163. }
  164. /**
  165. * Try to go to one direction. If there is an exit, enter
  166. * the new room, otherwise print an error message.
  167. */
  168. private void goOrgan(Command command)
  169. {
  170. if(!command.hasSecondWord())
  171. {
  172. // if there is no second word, we don't know where to go...
  173. System.out.println("Go where?");
  174. return;
  175. }
  176.  
  177. String direction = command.getSecondWord();
  178.  
  179. // Try to leave current room.
  180.  
  181. Organ nextOrgan = currentOrgan.getExit (direction);
  182.  
  183. if (nextOrgan == null)
  184. System.out.println("There is no door!");
  185. else
  186. {
  187. currentOrgan = nextOrgan;
  188. currentOrgan.generateGlucose();
  189. printLocationInfo();
  190.  
  191. }
  192. }
  193.  
  194. /**
  195. * "Quit" was entered. Check the rest of the command to see
  196. * whether we really quit the game. Return true, if this command
  197. * quits the game, false otherwise.
  198. */
  199. private boolean quit(Command command)
  200. {
  201. if(command.hasSecondWord()) {
  202. System.out.println("Quit what?");
  203. return false;
  204. }
  205. else
  206. return true; // signal that we want to quit
  207. }
  208.  
  209. private void printLocationInfo()
  210. {
  211. System.out.println (currentOrgan.getLongDescription());
  212.  
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement