World of Zuul

       World of Zuul merupakan sebuah game petualangan, dimana player akan mengetik perintah yang sesuai untuk mencapai pintu keluar. Game ini mengimplementasikan coupling dan cohesion dari Design Class. Program game ini terdiri atas 5 class yaitu CommandWords, Parser, Command, Room, dan Game. 




1. Game
  1. /**
  2. * This class is the main class of the "World of Zuul" application
  3. * "World of Zuul" is a very simple, text based adeventure 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
  11. * others: it creates all
  12. * rooms, creates the parser and starts the game. It also evaluates and
  13. * executes the commands that the parser returns.
  14. *
  15. * @author Sabrina Lydia S
  16. * @version 23/11/20
  17. */
  18.  
  19. public class Game
  20. {
  21. private Parser parser;
  22. private Room currentRoom;
  23.  
  24. /**
  25. * Create the game and initialise its internal map
  26. */
  27. public Game()
  28. {
  29. createRooms();
  30. parser = new Parser();
  31. }
  32.  
  33. /**
  34. * Create all the rooms and link their exits together.
  35. */
  36. private void createRooms()
  37. {
  38. Room outside, theater, pub, lab, office;
  39.  
  40. // Create the rooms
  41. outside = new Room("outside the main entrance of the university");
  42. theater = new Room("in a lecture theater");
  43. pub = new Room("in the campus pub");
  44. lab = new Room("in a computing lab");
  45. office = new Room("in the computing admin office");
  46.  
  47. // initialise room exits
  48. outside.setExits(null, theater, lab, pub);
  49. theater.setExits(null, null, null, outside);
  50. pub.setExits(null, outside, null, null);
  51. lab.setExits(outside, office, null, null);
  52. office.setExits(null, null, null, lab);
  53.  
  54. currentRoom = outside; // start game outside
  55. }
  56.  
  57. /**
  58. * Main play routine. Berulang hingga selesai permainan.
  59. */
  60. public void play()
  61. {
  62. printWelcome();
  63.  
  64. // enter the main command loop. Here we repeatedlu read commands and
  65. // execute the until the game is over.
  66.  
  67. boolean finished = false;
  68. while (! finished) {
  69. Command command = parser.getCommand();
  70. finished = processCommand(command);
  71. }
  72. System.out.println("Thank you for playing. Good bye.");
  73. }
  74.  
  75. /**
  76. * Print out the opening message for the player.
  77. */
  78. private void printWelcome()
  79. {
  80. System.out.println();
  81. System.out.println("Welcome to the World of Zuul!");
  82. System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  83. System.out.println("Type 'help' if you need help.");
  84. System.out.println();
  85. System.out.println("You are " + currentRoom.getDescription());
  86. System.out.print("Exits: ");
  87. if(currentRoom.northExit != null) {
  88. System.out.print("north ");
  89. }
  90. if(currentRoom.eastExit != null) {
  91. System.out.print("east ");
  92. }
  93. if(currentRoom.southExit != null) {
  94. System.out.print("south ");
  95. }
  96. if(currentRoom.westExit != null) {
  97. System.out.print("west ");
  98. }
  99. System.out.println();
  100. }
  101.  
  102. /**
  103. * Given a command, process (that is : execute) the command.
  104. * If this command ends the game, true is returned, otherwise false is
  105. * returned.
  106. * @param command
  107. * @return bool
  108. */
  109. private boolean processCommand(Command command)
  110. {
  111. boolean wantToQuit = false;
  112.  
  113. if(command.isUnknown()) {
  114. System.out.println("I don't know what you mean...");
  115. return false;
  116. }
  117.  
  118. String commandWord = command.getCommandWord();
  119. if (commandWord.equals("help"))
  120. printHelp();
  121. else if (commandWord.equals("go"))
  122. goRoom(command);
  123.  
  124. else if (commandWord.equals("quit"))
  125. wantToQuit = quit(command);
  126.  
  127. return wantToQuit;
  128. }
  129.  
  130. // Implementasi of user commands:
  131.  
  132. /**
  133. * Print out some help information.
  134. * Here we print some stupid, cryptic message and ad list of the
  135. * command words
  136. */
  137. private void printHelp()
  138. {
  139. System.out.println("You are lost. You are alone. You wander");
  140. System.out.println("around at the university.");
  141. System.out.println();
  142. System.out.println("Your command words are:");
  143. System.out.println(" go quit help");
  144. }
  145.  
  146. /**
  147. * Try to go to one direction. If there is an exit, enter
  148. * the new room, otherwise print an error message.
  149. */
  150. private void goRoom(Command command)
  151. {
  152. if(!command.hasSecondWord()) {
  153. // if there is no second word, we don't know where to go ...
  154. System.out.println("Go where?");
  155. return;
  156. }
  157.  
  158. String direction = command.getSecondWord();
  159.  
  160. // Try to leave current room
  161. Room nextRoom = null;
  162. if(direction.equals("north"))
  163. nextRoom = currentRoom.northExit;
  164. if(direction.equals("east"))
  165. nextRoom = currentRoom.eastExit;
  166. if(direction.equals("south"))
  167. nextRoom = currentRoom.southExit;
  168. if(direction.equals("west"))
  169. nextRoom = currentRoom.westExit;
  170.  
  171. if (nextRoom == null) {
  172. System.out.println("There is no door!");
  173. }
  174. else {
  175. currentRoom = nextRoom;
  176. System.out.println("You are " + currentRoom.getDescription());
  177. System.out.print("Exits: ");
  178. if(currentRoom.northExit != null)
  179. System.out.print("north ");
  180. if(currentRoom.eastExit != null)
  181. System.out.print("east ");
  182. if(currentRoom.southExit != null)
  183. System.out.print("south ");
  184. if(currentRoom.westExit != null)
  185. System.out.print("west ");
  186. System.out.println();
  187. }
  188. }
  189.  
  190. /**
  191. * "Quit" was entered. Check the rest of the command to see
  192. * whether we really quit the game. Return true, if this command
  193. * quits the game, false otherwise.
  194. *
  195. * @return bool
  196. */
  197. private boolean quit(Command command)
  198. {
  199. if(command.hasSecondWord()) {
  200. System.out.println("Quit what?");
  201. return false;
  202. }
  203. else {
  204. return true; // signal that we want to quit
  205. }
  206. }
  207. }

2. Parser
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4. import java.lang.String;
  5. /**
  6. * This class is the main class of the "World of Zuul" application.
  7. * "World of Zuul" is a very simple, text based adventure game.
  8. *
  9. * This parser reads user input and tries to interpret it as an "Adventure" command.
  10. * Every time it is called it reads a line from the terminal and
  11. * tries to interpret the line as a two word command. It returns the command
  12. * as an object of class Command.
  13. *
  14. * The parser has a set of known command words. It checks user input against
  15. * the known commands, and if the input is not one of the known commands, it
  16. * returns a command object that is marked as an unknown command.
  17. *
  18. * @author Sabrina Lydia S
  19. * @version 23/11/20
  20. */
  21. public class Parser
  22. {
  23. private CommandWords commands; // holds all valid command words
  24.  
  25. public Parser()
  26. {
  27. commands = new CommandWords();
  28. }
  29.  
  30. public Command getCommand()
  31. {
  32. String inputLine = ""; // will hold the full input line
  33. String word1;
  34. String word2;
  35.  
  36. System.out.print("> "); // print prompt
  37.  
  38. BufferedReader reader = new BufferedReader (new InputStreamReader(System.in));
  39. try {
  40. inputLine = reader.readLine();
  41. }
  42. catch (java.io.IOException exc){
  43. System.out.println ("There was an error during reading: " + exc.getMessage());
  44. }
  45.  
  46. StringTokenizer tokenizer = new StringTokenizer(inputLine);
  47.  
  48. if(tokenizer.hasMoreTokens())
  49. word1 = tokenizer.nextToken(); // get first word
  50. else
  51. word1=null;
  52. if(tokenizer.hasMoreTokens())
  53. word2 = tokenizer.nextToken(); // get second word
  54. else
  55. word2=null;
  56. // note: we just ignore the rest of the input line.
  57.  
  58. // Now check whether this word is known. If so, create a command
  59. // with it. If not, create a "null" command (for unknown command).
  60. if(commands.isCommand(word1))
  61. return new Command(word1, word2);
  62. else
  63. return new Command(null, word2);
  64. }
  65. }

3. Command
  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.
  4. *
  5. * This class holds information about a command that was issued by the user.
  6. * A command currently consists of two strings: a command word and a second
  7. * word (for example, if the command was "take map", then the two strings
  8. * obviously are "take" and "map").
  9. *
  10. * The way this is used is: Commands are already checked for being valid
  11. * command words. If the user entered an invalid command (a word that is not
  12. * known) then the command word is <null>.
  13. *
  14. * If the command had only one word, then the second word is <null>.
  15. *
  16. * @author Sabrina Lydia S
  17. * @version 23/11/20
  18. */
  19.  
  20. public class Command
  21. {
  22. private String commandWord;
  23. private String secondWord;
  24.  
  25. /**
  26. * Create a command object. First and second word must be supplied, but
  27. * either one (or both) can be null. The command word should be null to
  28. * indicate that this was a command that is not recognised by this game.
  29. * @param String firstWord
  30. * @param String secondWord
  31. */
  32. public Command(String firstWord, String secondWord)
  33. {
  34. commandWord = firstWord;
  35. this.secondWord = secondWord;
  36. }
  37.  
  38. /**
  39. * Return the command word (the first word) of this command. If the
  40. * command was not understood, the result is null.
  41. * @return String commandWord
  42. */
  43. public String getCommandWord()
  44. {
  45. return commandWord;
  46. }
  47.  
  48. /**
  49. * Return the second word of this command. Returns null if there was no
  50. * second word.
  51. * @return String secondWord
  52. */
  53. public String getSecondWord()
  54. {
  55. return secondWord;
  56. }
  57.  
  58. /**
  59. * Return true if this command was not understood.
  60. * @return bool
  61. */
  62. public boolean isUnknown()
  63. {
  64. return (commandWord == null);
  65. }
  66.  
  67. /**
  68. * Return true if the command has a second word.
  69. * @return bool
  70. */
  71. public boolean hasSecondWord()
  72. {
  73. return (secondWord != null);
  74. }
  75. }

4. CommandWords
  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.
  4. *
  5. * This class holds an enumeration of all command words known to the game.
  6. * It is used to recognise commands as they are typed in.
  7. *
  8. * @author Sabrina Lydia S
  9. * @version 23/11/20
  10. */
  11.  
  12. public class CommandWords
  13. {
  14. // a constant array that holds all valid command words
  15. private static final String[] validCommands = {
  16. "go", "quit", "help", "look"
  17. };
  18.  
  19. /**
  20. * Constructor - initialise the command words.
  21. */
  22. public CommandWords()
  23. {
  24. // nothing to do at the moment...
  25. }
  26.  
  27. /**
  28. * Check whether a given String is a valid command word.
  29. * Return true if it is, false if it isn't.
  30. * @return bool
  31. */
  32. public boolean isCommand(String aString)
  33. {
  34. for(int i = 0; i < validCommands.length; i++) {
  35. if(validCommands[i].equals(aString))
  36. return true;
  37. }
  38. //if we get here, the string was not found in the commands
  39. return false;
  40. }
  41. }
5. Room
  1. /**
  2. * Class Room - a room in an adeventure game.
  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.
  6. *
  7. * A "Room" represents one location in the scenery of the game. It is
  8. * connected to other rooms via exits. The exits are labelled north,
  9. * east, south, west. For each direction, the room stores a reference
  10. * to the neighboring room, or null if there is no exit in that direction.
  11. *
  12. * @author Sabrina Lydia S
  13. * @version 23/11/20
  14. */
  15. public class Room
  16. {
  17. public String description;
  18. public Room northExit;
  19. public Room southExit;
  20. public Room eastExit;
  21. public Room westExit;
  22.  
  23. /**
  24. * Create a room described "description". Initially, it has
  25. * no exits. "description" is something like "a kitchen" or
  26. *"an open court yard".
  27. *
  28. * @param String description
  29. */
  30. public Room(String description)
  31. {
  32. this.description = description;
  33. }
  34.  
  35. /**
  36. * Define the exits of this room. Every direction either leads
  37. * to another room or is null (no exit there).
  38. *
  39. * @param north The north exit.
  40. * @param east The east east.
  41. * @param south The south exit.
  42. * @param west The west exit.
  43. */
  44. public void setExits(Room north, Room east, Room south, Room west)
  45. {
  46. if(north != null)
  47. northExit = north;
  48. if(east != null)
  49. eastExit = east;
  50. if(south != null)
  51. southExit = south;
  52. if(west != null)
  53. westExit = west;
  54. }
  55.  
  56. /**
  57. * Return the description of the room (the one that was defined
  58. * in the constructor)
  59. *
  60. * @return String description.
  61. */
  62. public String getDescription()
  63. {
  64. return description;
  65. }
  66.  
  67. }

Output

-thankyou-

Komentar

Postingan populer dari blog ini

ETS PBO

Tugas Simulasi Traffic Light

Membuat Aplikasi Desktop - Tugas 2 PBKK