Advertisement
Guest User

Game Class

a guest
Nov 20th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 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 2008.03.30
  16. */
  17.  
  18. public class Game
  19. {
  20. private Parser parser;
  21. private Room currentRoom;
  22. private Item key;
  23.  
  24.  
  25. /**
  26. * Create the game and initialise its internal map.
  27. */
  28. public Game()
  29. {
  30. createRooms();
  31. parser = new Parser();
  32.  
  33. }
  34.  
  35. /**
  36. * Create all the rooms and link their exits together.
  37. */
  38. private void createRooms()
  39. {
  40. Room outside, theatre, pub, lab, office;
  41.  
  42. // create the rooms
  43. outside = new Room("outside the main entrance of the university");
  44. theatre = new Room("in a lecture theatre");
  45. pub = new Room("in the campus pub");
  46. lab = new Room("in a computing lab");
  47. office = new Room("in the computing admin office");
  48.  
  49. // initialise room exits
  50. outside.setExit("east", theatre);
  51. outside.setExit("south", lab);
  52. outside.setExit("west", pub);
  53. theatre.setExit("west", outside);
  54. pub.setExit("east", outside);
  55. lab.setExit("north", outside);
  56. lab.setExit("east", office);
  57. office.setExit("west", lab);
  58.  
  59. //initialise items in rooms
  60. office.addItemToRoom(new Item("Airlock key", "You have picked up Airlock key", true));
  61.  
  62. currentRoom = outside; // start game outside
  63. }
  64.  
  65. /**
  66. * Main play routine. Loops until end of play.
  67. */
  68. public void play()
  69. {
  70. printWelcome();
  71.  
  72. // Enter the main command loop. Here we repeatedly read commands and
  73. // execute them until the game is over.
  74.  
  75. boolean finished = false;
  76. while (! finished) {
  77. Command command = parser.getCommand();
  78. finished = processCommand(command);
  79. }
  80. System.out.println("Thank you for playing. Good bye.");
  81. }
  82.  
  83. /**
  84. * Print out the opening message for the player.
  85. */
  86. private void printWelcome()
  87. {
  88. System.out.println();
  89. System.out.println("Welcome to the World of Zuul!");
  90. System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  91. System.out.println("Type '" + CommandWord.HELP + "' if you need help.");
  92. System.out.println();
  93. System.out.println(currentRoom.getLongDescription());
  94. }
  95.  
  96. /**
  97. * Given a command, process (that is: execute) the command.
  98. * @param command The command to be processed.
  99. * @return true If the command ends the game, false otherwise.
  100. */
  101. private boolean processCommand(Command command)
  102. {
  103. boolean wantToQuit = false;
  104.  
  105. CommandWord commandWord = command.getCommandWord();
  106.  
  107. if(commandWord == CommandWord.UNKNOWN) {
  108. System.out.println("I don't know what you mean...");
  109. return false;
  110. }
  111.  
  112. if (commandWord == CommandWord.HELP) {
  113. printHelp();
  114. }
  115. else if (commandWord == CommandWord.GO) {
  116. goRoom(command);
  117. }
  118. else if (commandWord == CommandWord.QUIT) {
  119. wantToQuit = quit(command);
  120. }
  121.  
  122. if (commandWord == CommandWord.TAKE) {
  123. //
  124. }
  125.  
  126. else {
  127. return wantToQuit;
  128. }
  129. }
  130.  
  131.  
  132.  
  133. // implementations of user commands:
  134.  
  135. /**
  136. * Print out some help information.
  137. * Here we print some stupid, cryptic message and a list of the
  138. * command words.
  139. */
  140. private void printHelp()
  141. {
  142. System.out.println("You are lost. You are alone. You wander");
  143. System.out.println("around at the university.");
  144. System.out.println();
  145. System.out.println("Your command words are:");
  146. parser.showCommands();
  147. }
  148.  
  149. /**
  150. * Try to go to one direction. If there is an exit, enter the new
  151. * room, otherwise print an error message.
  152. */
  153. private void goRoom(Command command)
  154. {
  155. if(!command.hasSecondWord()) {
  156. // if there is no second word, we don't know where to go...
  157. System.out.println("Go where?");
  158. return;
  159. }
  160.  
  161. String direction = command.getSecondWord();
  162.  
  163. // Try to leave current room.
  164. Room nextRoom = currentRoom.getExit(direction);
  165.  
  166. if (nextRoom == null) {
  167. System.out.println("There is no door!");
  168. }
  169. else {
  170. currentRoom = nextRoom;
  171. System.out.println(currentRoom.getLongDescription());
  172. }
  173. }
  174.  
  175. /**
  176. * Try to go to one direction. If there is an exit, enter the new
  177. * room, otherwise print an error message.
  178. */
  179. private void takeItem(Command command)
  180. {
  181. if(!command.hasSecondWord()) {
  182. System.out.println("What would you like me to take?");
  183. return;
  184.  
  185. }
  186. String name = command.getSecondWord();
  187.  
  188.  
  189. boolean isItHere = false;
  190. for (int k = 0; k>currentRoom.roomItems.size();k++)
  191. { Item tmp = currentRoom.roomItems.get(k);
  192. if(tmp.getName().equals(Item.getName()))
  193. {isItHere = true;
  194. }
  195. }
  196. }
  197.  
  198. /**
  199. * "Quit" was entered. Check the rest of the command to see
  200. * whether we really quit the game.
  201. * @return true, if this command quits the game, false otherwise.
  202. */
  203. private boolean quit(Command command)
  204. {
  205. if(command.hasSecondWord()) {
  206. System.out.println("Quit what?");
  207. return false;
  208. }
  209. else {
  210. return true; // signal that we want to quit
  211. }
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement