ace

characterinfo1

ace
Sep 16th, 2010
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.85 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Random;
  3. import java.util.Collection;
  4.  
  5.  
  6. /**
  7. * This class is the main class of the "World of Zuul" application.
  8. * "World of Zuul" is a very simple, text based adventure game. Users
  9. * can walk around some scenery. That's all. It should really be extended
  10. * to make it more interesting!
  11. *
  12. * To play this game, create an instance of this class and call the "play"
  13. * method.
  14. *
  15. * This main class creates and initialises all the others: it creates all
  16. * rooms, creates the parser and starts the game. It also evaluates and
  17. * executes the commands that the parser returns.
  18. *
  19. * @author Michael Kolling and David J. Barnes
  20. * @version 1.0 (February 2002)
  21. */
  22.  
  23. public class Game
  24. {
  25. private Parser parser;
  26.  
  27. private String lastDirection;
  28. private HashMap<String,Room> rooms;
  29. private Player player;
  30. private int numberOfMoves;
  31. private Room lastRoom;
  32. private Random tPort;
  33. private HashMap<String,Character> characters;
  34. /**
  35. * Create the game and initialise its internal map.
  36. */
  37. public Game()
  38. {
  39.  
  40. parser = new Parser();
  41. rooms = new HashMap<String, Room>();
  42. characters = new HashMap<String, Character>();
  43. player = new Player();
  44. createRooms();
  45. numberOfMoves = 0;
  46. tPort = new Random();
  47.  
  48.  
  49.  
  50. }
  51.  
  52. /**
  53. * Create all the rooms and link their exits together.
  54. */
  55. private void createRooms()
  56. {
  57. Room outside, galleria, theatre, pub, store, office, closet;
  58.  
  59. // create the rooms
  60. outside = new Room("outside the main entrance of the mall");
  61. galleria = new Room("in the mall galleria");
  62. theatre = new Room("in a movie theatre");
  63. pub = new Room("in the pub");
  64. store = new Room("in a store");
  65. office = new Room("in the mall office");
  66. closet = new Room ("in a closet");
  67. //initialise roommap
  68. rooms.put("outside", outside);
  69. rooms.put("theatre", theatre);
  70. rooms.put("pub", pub);
  71. rooms.put("office", office);
  72. rooms.put("galleria", galleria);
  73. rooms.put ("store", store);
  74. rooms.put("closet", closet);
  75.  
  76. // initialise room exits
  77. outside.setExit("east", pub);
  78. outside.setExit("south", galleria);
  79. outside.setExit ("west", theatre);
  80. theatre.setExit ("east", outside);
  81. theatre.setExit ("south", office);
  82. pub.setExit("west", outside);
  83. pub.setExit("south", store);
  84. office.setExit("north", theatre);
  85. office.setExit("east", galleria);
  86. galleria.setExit("north", outside);
  87. galleria.setExit("east", store);
  88. galleria.setExit("west", office);
  89. galleria.setExit("south", closet);
  90. store.setExit ("north", pub);
  91. store.setExit("west", galleria);
  92. closet.setExit("north", galleria);
  93.  
  94. createItems();
  95. player.setCurrentRoom(outside); // start game outside
  96. lastDirection = null;
  97. createCharacters();
  98.  
  99.  
  100.  
  101. }
  102.  
  103. private void createItems()
  104. {
  105. Item tickingBomb, popcornMachine, beerTap, beer, key;
  106.  
  107. // create the items
  108. tickingBomb = new Item(1, "a ticking bomb", "bomb");
  109. popcornMachine = new Item(4, "a popcorn machine", "popcornmachine");
  110. key = new Item(0, "a key", "key");
  111. beerTap = new Item (3, "a beer tap", "beertap");
  112. rooms.get("pub").setItems(tickingBomb);
  113. rooms.get("pub").setItems(popcornMachine);
  114. rooms.get("pub").setItems(beerTap);
  115. rooms.get("office").setItems(key);
  116. }
  117.  
  118.  
  119. /* create charcters and populater hashmap characters*/
  120.  
  121. private void createCharacters()
  122. {
  123. Character littleBoy, bartender, shopper, distraughtMother;
  124.  
  125. // create the items
  126. littleBoy = new Character("littleBoy", "tommy");
  127. bartender = new Character("bartender", "michael");
  128. shopper = new Character("shopper", "julie");
  129. distraughtMother = new Character("distraughtmother", "emily");
  130.  
  131. littleBoy.setCurrentRoom(rooms.get("store"));
  132. bartender.setCurrentRoom(rooms.get("pub"));
  133. shopper.setCurrentRoom(rooms.get("store"));
  134. distraughtMother.setCurrentRoom(rooms.get("theatre"));
  135.  
  136. characters.put("littleboy", littleBoy);
  137. characters.put("bartender", bartender);
  138. characters.put("shopper", shopper);
  139. characters.put("distraughtmother", distraughtMother);
  140.  
  141.  
  142. }
  143.  
  144.  
  145. private void printLocationInfo()
  146. {
  147. System.out.println ("You are " + player.getCurrentRoom().getLongDescription() + characterInfo());
  148.  
  149. }
  150.  
  151.  
  152.  
  153. /**
  154. * Main play routine. Loops until end of play.
  155. */
  156. public void play()
  157. {
  158. printWelcome();
  159.  
  160. // Enter the main command loop. Here we repeatedly read commands and
  161. // execute them until the game is over.
  162.  
  163. boolean finished = false;
  164. while (! finished) {
  165. Command command = parser.getCommand();
  166. finished = processCommand(command);
  167. }
  168. System.out.println("Thank you for playing. Good bye.");
  169. }
  170.  
  171. /**
  172. * Print out the opening message for the player.
  173. */
  174. private void printWelcome()
  175. {
  176. System.out.println();
  177. System.out.println("Welcome to the World of Zuul!");
  178. System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  179. System.out.println("Type 'help' if you need help.");
  180. System.out.println();
  181. printLocationInfo();
  182.  
  183. }
  184.  
  185. /**
  186. * Given a command, process (that is: execute) the command.
  187. * If this command ends the game, true is returned, otherwise false is
  188. * returned.
  189. */
  190. private boolean processCommand(Command command)
  191. {
  192. boolean wantToQuit = false;
  193.  
  194. if(numberOfMoves == 20)
  195. {
  196. System.out.println("the bomb has detonated destoying the mall! You have failed.");
  197. return true;
  198. }
  199.  
  200.  
  201. if(command.isUnknown()) {
  202. System.out.println("I don't know what you mean...");
  203. return false;
  204. }
  205.  
  206. String commandWord = command.getCommandWord();
  207. if (commandWord.equals("help"))
  208. printHelp();
  209. else if (commandWord.equals("go"))
  210. goRoom(command);
  211. else if (commandWord.equals("look"))
  212. look();
  213. else if (commandWord.equals("quit"))
  214. wantToQuit = quit(command);
  215. else if (commandWord.equals("defuse"))
  216. wantToQuit = defuse(command);
  217. else if(commandWord.equals ("back"))
  218. back();
  219. else if (commandWord.equals ("take"))
  220. take(command);
  221. else if (commandWord.equals("drop"))
  222. drop (command);
  223. else if (commandWord.equals("inventory"))
  224. inventory();
  225. else if (commandWord.equals("pour"))
  226. pour(command);
  227. else if (commandWord.equals("drink"))
  228. drink(command);
  229. else if (commandWord.equals("rescue"))
  230. rescue(command);
  231. return wantToQuit;
  232.  
  233.  
  234. }
  235.  
  236. // implementations of user commands:
  237.  
  238. /**
  239. * Print out some help information.
  240. * Here we print some stupid, cryptic message and a list of the
  241. * command words.
  242. */
  243. private void printHelp()
  244. {
  245. System.out.println("You are lost. You are alone. ");
  246. System.out.println("You wander around at the mall.");
  247. System.out.println();
  248. System.out.println("Your command words are:");
  249. System.out.println ("" + parser.showCommands());
  250.  
  251. }
  252.  
  253. /**
  254. * Try to go to one direction. If there is an exit, enter
  255. * the new room, otherwise print an error message.
  256. */
  257. private void goRoom(Command command)
  258. {
  259. if(!command.hasSecondWord()) {
  260. // if there is no second word, we don't know where to go...
  261. System.out.println("Go where?");
  262. return;
  263. }
  264.  
  265. String direction = command.getSecondWord();
  266.  
  267. // Try to leave current room.
  268.  
  269. Room nextRoom = player.getCurrentRoom().getExit(direction);
  270.  
  271.  
  272.  
  273. if (nextRoom == null)
  274. {
  275. System.out.println("There is no door!");
  276. }
  277. else if ((nextRoom == rooms.get("office")) && (player.getCurrentRoom() == rooms.get("galleria")))
  278. {
  279. System.out.println ("That door is locked");
  280. printLocationInfo();
  281. }
  282.  
  283. else if (nextRoom == rooms.get("closet"))
  284. {
  285. tPort();
  286. printLocationInfo();
  287. incrementMoves();
  288. }
  289.  
  290. else
  291. {
  292. incrementMoves();
  293. lastDirection = direction;
  294. player.setLastRoom(player.getCurrentRoom());
  295. player.setCurrentRoom(nextRoom);
  296. System.out.println (" end of line");
  297. printLocationInfo();
  298.  
  299. }
  300.  
  301. }
  302.  
  303. private boolean defuse(Command command)
  304. {
  305. if(!command.hasSecondWord())
  306. {
  307. System.out.println("defuse what?");
  308. return false;
  309.  
  310. }
  311.  
  312. if (player.checkInventory().equals ("a ticking bomb"))
  313. {
  314. System.out.println ("you have defused the bomb");
  315. System.out.println ("You have saved the mall! You are commended for bravery.");
  316. return true;
  317. }
  318. else
  319. {
  320. System.out.println("You cannot defuse that!");
  321. return false;
  322. }
  323.  
  324. }
  325.  
  326. private void look()
  327. {
  328. System.out.println ("" + player.getCurrentRoom().getLongDescription());
  329. }
  330.  
  331.  
  332. /**
  333. * "Quit" was entered. Check the rest of the command to see
  334. * whether we really quit the game. Return true, if this command
  335. * quits the game, false otherwise.
  336. */
  337. private boolean quit(Command command)
  338. {
  339. if(command.hasSecondWord()) {
  340. System.out.println("Quit what?");
  341. return false;
  342. }
  343. else
  344. return true; // signal that we want to quit
  345. }
  346.  
  347. private void back()
  348. {
  349. String reverseLastDirection = "";
  350. System.out.println("" + lastDirection);
  351.  
  352. if(lastDirection.equals ("north"))
  353.  
  354. {
  355. reverseLastDirection = "south";
  356.  
  357. }
  358. if(lastDirection.equals("east"))
  359. {
  360. reverseLastDirection = "west";
  361.  
  362. }
  363. if (lastDirection.equals("south"))
  364. {
  365. reverseLastDirection = "north";
  366.  
  367. }
  368. if (lastDirection.equals("west"))
  369. {
  370. reverseLastDirection = "east";
  371.  
  372. }
  373. System.out.println("" + reverseLastDirection);
  374. Command backCommand = new Command ("go", reverseLastDirection);
  375. goRoom(backCommand);
  376. }
  377.  
  378.  
  379. private void take(Command command)
  380. {
  381.  
  382. if(!command.hasSecondWord()) {
  383. // if there is no second word, we don't know what to pick up...
  384. System.out.println("Take what?");
  385. return;
  386. }
  387.  
  388. String itemName = command.getSecondWord();
  389.  
  390. // take item
  391. String result = player.takeItem(itemName);
  392. System.out.println(result);
  393.  
  394.  
  395.  
  396. }
  397.  
  398. private void drop(Command command)
  399. {
  400. if(!command.hasSecondWord())
  401. {
  402. System.out.println ("Drop what?");
  403.  
  404. }
  405. String itemName = command.getSecondWord();
  406. player.dropItem(itemName);
  407. System.out.println ("you have dropped " + itemName);
  408.  
  409. }
  410.  
  411.  
  412. private void inventory()
  413. {
  414.  
  415. System.out.println(player.checkInventory());
  416. }
  417.  
  418. private void pour(Command command)
  419. {
  420. if(!command.hasSecondWord())
  421. {
  422. System.out.println ("Pour what?");
  423.  
  424. }
  425. else
  426. {
  427. if (command.getSecondWord().equals("beer") &&
  428. player.getCurrentRoom().itemTest("beertap") == true)
  429. {
  430. System.out.println ("you pull the beer tap");
  431. Item beer = new Item (1, "a beer", "beer");
  432. player.getCurrentRoom().setItems(beer);
  433. }
  434. else
  435. {
  436. System.out.println("You cannot pour that.");
  437. }
  438. }
  439. }
  440.  
  441. private void drink(Command command)
  442. {
  443. if(!command.hasSecondWord())
  444. {
  445. System.out.println("Drink what?");
  446. }
  447.  
  448. else
  449. {
  450. if (command.getSecondWord().equals("beer") &&
  451. player.getCurrentRoom().itemTest("beer") == true)
  452. {
  453. System.out.println("you drink a beer.");
  454. player.increaseStrength(1);
  455. }
  456. }
  457. }
  458.  
  459. private void incrementMoves()
  460. {
  461. numberOfMoves = numberOfMoves +1;
  462. }
  463.  
  464. private void tPort()
  465. {
  466. int i = tPort.nextInt(6);
  467. System.out.println ("you have been teleported.");
  468. System.out.println ("" + i);
  469. if(i == 0)
  470. {
  471. System.out.println("you have been teleported to the outside.");
  472. player.setCurrentRoom(rooms.get("outside"));
  473. }
  474. else if(i == 1)
  475. {
  476. System.out.println("you have been teleported to the pub.");
  477. player.setCurrentRoom(rooms.get("pub"));
  478. }
  479. else if(i == 2)
  480. {
  481. System.out.println("you have been teleported to the theatre");
  482. player.setCurrentRoom(rooms.get("theatre"));
  483. }
  484. else if(i == 3)
  485. {
  486. System.out.println("you have been teleported to the office.");
  487. player.setCurrentRoom(rooms.get("office"));
  488. }
  489. else if(i == 4)
  490. {
  491. System.out.println("you have been teleported to the store.");
  492. player.setCurrentRoom(rooms.get("store"));
  493. }
  494. else if(i == 5)
  495. {
  496. System.out.println("you have been teleported to the galleria.");
  497. player.setCurrentRoom(rooms.get("galleria"));
  498. }
  499.  
  500. else
  501. {
  502. System.out.println("you have reached the else statement.");
  503. printLocationInfo();
  504. player.setCurrentRoom(rooms.get("closet"));
  505.  
  506. }
  507. }
  508.  
  509. private void rescue(Command command)
  510. {
  511.  
  512. if(!command.hasSecondWord())
  513. {
  514. // if there is no second word, we don't know where to go...
  515. System.out.println("Rescue who?");
  516. return;
  517. }
  518.  
  519. else
  520. {
  521.  
  522. String characterID = command.getSecondWord();
  523.  
  524. if(characters.get(characterID).getCurrentRoom() == null)
  525. {
  526. System.out.println("They are not here!");
  527. }
  528.  
  529.  
  530. else
  531. {
  532. if(characters.get(characterID).getCurrentRoom() == player.getCurrentRoom())
  533. {
  534. characters.get(characterID).setCurrentRoom(rooms.get("outside"));
  535. System.out.println (("you have saved ") + characterID + (" !"));
  536. }
  537.  
  538. else
  539. {
  540. System.out.println("They are not here!");
  541. }
  542. }
  543. }
  544.  
  545. }
  546.  
  547. //returns the values - character objects - from the hashmap of characters
  548.  
  549. private Collection <Character> characterCollection()
  550. {
  551. Collection <Character> characterValues = characters.values();
  552. return characterValues;
  553. //return characters.values();
  554. }
  555.  
  556. private String characterInfo()
  557. {
  558.  
  559. for (Character character : characterCollection())
  560. {
  561. System.out.println("" + character);
  562. System.out.println("" + character.getCurrentRoom());
  563. System.out.println("" + player.getCurrentRoom());
  564. if (character.getCurrentRoom() == player.getCurrentRoom())
  565. {
  566. return ("There is a ") + character.getCharacterDescription();
  567. }
  568.  
  569. else
  570. {
  571. return ("There is no one here.");
  572. }
  573. }
  574. return ("There is nobody here.");
  575.  
  576. }
  577.  
  578.  
  579.  
  580.  
  581. }
Add Comment
Please, Sign In to add comment