Advertisement
Guest User

Command

a guest
Jun 28th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. import java.util.HashMap;
  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 Kolling and David J. Barnes
  17. * @version 1.0 (February 2002)
  18. */
  19.  
  20. class Game
  21. {
  22. private Parser parser;
  23. private Person player;
  24. private Person skeleton;
  25. private Person goblin;
  26. private Person warrior;
  27.  
  28. HashMap<String, Item> items;
  29. //HashMap<String, Room> rooms;
  30.  
  31. int inventorySize;
  32. boolean encounter;
  33.  
  34. /**
  35. * Create the game and initialise its internal map.
  36. */
  37. public Game()
  38. {
  39. player = new Person("Player", 100, 10);
  40. skeleton = new Person("Skeleton", 75, 50);
  41. goblin = new Person("Goblin", 45, 7);
  42. warrior = new Person("Warrior", 90, 6);
  43.  
  44. createRooms();
  45. parser = new Parser();
  46.  
  47. items = new HashMap<String, Item>();
  48. //rooms = new HashMap<String, Room>();
  49.  
  50. encounter = false;
  51. }
  52.  
  53. /**
  54. * Create all the rooms and link their exits together.
  55. * @return
  56. * @return
  57. */
  58.  
  59. private void createRooms()
  60. {
  61. Room dungeon_room1 = new Room("dungeon_room 1");
  62. Room dungeon_room2 = new Room("dungeon_room 2");
  63. Room dungeon_room3 = new Room("dungeon_room 3");
  64. Room dungeon_room4 = new Room("dungeon_room 4");
  65. Room dungeon_room5 = new Room("dungeon_room 5");
  66. Room dungeon_room6 = new Room("dungeon_room 6");
  67. Room dungeon_room7 = new Room("dungeon_room 7");
  68.  
  69. dungeon_room1.setExit("north", dungeon_room2);
  70.  
  71. dungeon_room2.setExit("east", dungeon_room3);
  72. dungeon_room2.setExit("west", dungeon_room4);
  73.  
  74. dungeon_room3.setExit("west", dungeon_room2);
  75. dungeon_room3.setExit("south", dungeon_room7);
  76.  
  77. dungeon_room4.setExit("east", dungeon_room2);
  78. dungeon_room4.setExit("north", dungeon_room5);
  79.  
  80. dungeon_room5.setExit("west", dungeon_room6);
  81. dungeon_room5.setExit("south", dungeon_room4);
  82. dungeon_room6.setExit("east", dungeon_room5);
  83. dungeon_room7.setExit("north", dungeon_room3);
  84.  
  85.  
  86. //Item sword = new Item("sword", 5, 7);
  87. //Item axe = new Item("axe", 8, 9);
  88. //Item healthPotion = new Item("healthpotion", 2, 0);
  89.  
  90. Sword s = new Sword(5);
  91.  
  92. //dungeon_room2.roomInventory.items.put("sword", sword);
  93. //dungeon_room3.roomInventory.items.put("axe", axe);
  94. //dungeon_room4.roomInventory.items.put("healthPotion", healthPotion);
  95.  
  96. player.setCurrentRoom(dungeon_room1); // start game outside
  97. skeleton.setCurrentRoom(dungeon_room7);
  98. goblin.setCurrentRoom(dungeon_room5);
  99. warrior.setCurrentRoom(dungeon_room6);
  100.  
  101. dungeon_room2.roomInventory.showAllItems();
  102. return;
  103. }
  104.  
  105. /**
  106. * Main play routine. Loops until end of play.
  107. */
  108. public void play()
  109. {
  110. printWelcome();
  111.  
  112. // Enter the main command loop. Here we repeatedly read commands and
  113. // execute them until the game is over.
  114.  
  115. boolean finished = false;
  116. while (! finished) {
  117. Command command = parser.getCommand();
  118. finished = processCommand(command);
  119. }
  120. System.out.println("Thank you for playing. Good bye.");
  121. }
  122.  
  123. /**
  124. * Print out the opening message for the player.
  125. */
  126. private void printWelcome()
  127. {
  128. System.out.println();
  129. System.out.println("Welcome to Adventure!");
  130. System.out.println("Adventure is a new, incredibly boring adventure game.");
  131. System.out.println("Type 'help' if you need help.");
  132. System.out.println();
  133. System.out.println(player.getCurrentRoom().getLongDescription());
  134. }
  135.  
  136. /**
  137. * Given a command, process (that is: execute) the command.
  138. * If this command ends the game, true is returned, otherwise false is
  139. * returned.
  140. */
  141. private boolean processCommand(Command command)
  142. {
  143. boolean wantToQuit = false;
  144.  
  145. if(command.isUnknown()) {
  146. System.out.println("I don't know what you mean...");
  147. return false;
  148. }
  149.  
  150. String commandWord = command.getCommandWord();
  151. if (commandWord.equals("help"))
  152. {
  153. printHelp();
  154. } else if (commandWord.equals("go"))
  155. {
  156. gotoRoom(command);
  157. }else if (commandWord.equals("quit"))
  158. {
  159. wantToQuit = quit(command);
  160. }else if (commandWord.equals("search"))
  161. {
  162. searchRoom(command);
  163. }else if (commandWord.equals("take"))
  164. {
  165. // takeItem(command, ///????);
  166. }
  167. return wantToQuit;
  168. }
  169.  
  170. // implementations of user commands:
  171.  
  172. /**
  173. * Print out some help information.
  174. * Here we print some stupid, cryptic message and a list of the
  175. * command words.
  176. */
  177. private void printHelp()
  178. {
  179. System.out.println("You are lost. You are alone. You wander");
  180. System.out.println("around at the university.");
  181. System.out.println();
  182. System.out.println("Your command words are:");
  183. parser.showCommands();
  184. }
  185.  
  186. /**
  187. * Try to go to one direction. If there is an exit, enter the new
  188. * room, otherwise print an error message.
  189. */
  190. private void gotoRoom(Command command)
  191. {
  192. if(!command.hasSecondWord()) {
  193. // if there is no second word, we don't know where to go...
  194. System.out.println("Go where?");
  195. return;
  196. }
  197.  
  198. String direction = command.getSecondWord();
  199.  
  200. // Try to leave current room.
  201. Room nextRoom = player.getCurrentRoom().getExit(direction);
  202.  
  203. if (nextRoom == null)
  204. System.out.println("There is no door!");
  205. else {
  206. player.setCurrentRoom(nextRoom);
  207. System.out.println(player.getCurrentRoom().getLongDescription());
  208. // person.removeHealth(25);
  209. System.out.println("Your current health is: "+ player.getPersonHealth());
  210. }
  211.  
  212. if (player.getPersonHealth() <= 0) {
  213. System.out.println("You died, thanks for playing.");
  214. }
  215.  
  216. if (player.getCurrentRoom().equals(skeleton.getCurrentRoom()))
  217. {
  218. System.out.println("You've encounterd a Skeleton");
  219. skeleton.attack(player);
  220. System.out.println("The skeleton attacked and dealth " + skeleton.getDamage() + " damage.");
  221. System.out.println("Your current health is " + player.getPersonHealth());
  222. }
  223. }
  224. /**
  225. * "Look" was entered. It will display the room description to the player.
  226. *
  227. */
  228.  
  229. private void searchRoom(Command command)
  230. {
  231. if(command.hasSecondWord()) {
  232. System.out.println("Simply type 'look', to look around.");
  233. return;
  234. }
  235. System.out.println(player.getCurrentRoom().getLongDescription());
  236. }
  237.  
  238. private void takeItem(Command command, Item i)
  239. {
  240. if(command.hasSecondWord()) {
  241. System.out.println("Simply type 'look', to look around.");
  242. return;
  243. }
  244.  
  245. if(player.personInventory.inventorySize >= i.weight)
  246. {
  247. // player.personInventory.items.put(i);
  248. // player.getCurrentRoom().remove(i);
  249. }
  250. }
  251. /**
  252. * "Quit" was entered. Check the rest of the command to see
  253. * whether we really quit the game. Return true, if this command
  254. * quits the game, false otherwise.
  255. */
  256. private boolean quit(Command command)
  257. {
  258. if(command.hasSecondWord()) {
  259. System.out.println("Quit what?");
  260. return false;
  261. }
  262. else
  263. return true; // signal that we want to quit
  264. }
  265.  
  266. public static void main(String[] args)
  267. {
  268. Game game = new Game();
  269. game.play();
  270.  
  271. System.out.println();
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement