Advertisement
Guest User

Untitled

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