RickyS20_

Untitled

Nov 15th, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. /**
  2.  
  3. * Kelas ini adalah kelas utama dari aplikasi "Dunia Zuul".
  4. * "World of Zuul" adalah game petualangan berbasis teks yang sangat sederhana. Pengguna
  5. * Bisa berjalan di sekitar pemandangan. Itu saja. Ini benar-benar harus diperpanjang
  6. * agar lebih menarik!
  7. *
  8. * Untuk memainkan game ini, buat instance dari kelas ini dan panggil "play"
  9. * metode.
  10. *
  11. * Kelas utama ini membuat dan menginisialisasi semua yang lain: itu menciptakan semua
  12. * ruangan, membuat parser dan memulai permainan. Itu juga mengevaluasi dan
  13. * menjalankan perintah yang dikembalikan parser.
  14. *
  15. * @author Ricky Supriyanto
  16. * @version 1.0 (November 2020)
  17. */
  18.  
  19. class Game
  20. {
  21. private Parser parser;
  22. private Room currentRoom;
  23. /**
  24. * Buat game dan inisialisasi peta internalnya.
  25. */
  26. public Game()
  27. {
  28. createRooms();
  29. parser = new Parser();
  30. }
  31. /**
  32. * Buat semua room dan hubungkan room exit nya.
  33. */
  34. private void createRooms()
  35. {
  36. Room outside, theatre, pub, lab, office;
  37. // Buat room
  38. outside = new Room("outside the main entrance of the university");
  39. theatre = new Room("in a lecture theatre");
  40. pub = new Room("in the campus pub");
  41. lab = new Room("in a computing lab");
  42. office = new Room("in the computing admin office");
  43. // inisialisasi room exits
  44. outside.setExits(null, theatre, lab, pub);
  45. theatre.setExits(null, null, null, outside);
  46. pub.setExits(null, outside, null, null);
  47. lab.setExits(outside, office, null, null);
  48. office.setExits(null, null, null, lab);
  49. currentRoom = outside; // start game outside
  50. }
  51. /**
  52. * Main play routine. Ulangi sampai akhir permainan.
  53. */
  54. public void play()
  55. {
  56. printWelcome();
  57. // Masuk ke loop perintah utama. Di sini kami berulang kali membaca perintah dan
  58. // jalankan sampai permainan selesai.
  59. boolean finished = false;
  60. while (! finished) {
  61. Command command = parser.getCommand();
  62. finished = processCommand(command);
  63. }
  64. System.out.println("Thank you for playing. Good bye.");
  65. }
  66. /**
  67. * Cetak pesan pembuka untuk pemain tersebut.
  68. */
  69. private void printWelcome()
  70. {
  71. System.out.println();
  72. System.out.println("Welcome to Adventure!");
  73. System.out.println("Adventure is a new, incredibly boring adventure game.");
  74. System.out.println("Type 'help' if you need help.");
  75. System.out.println();
  76. System.out.println("You are " + currentRoom.getDescription());
  77. System.out.print("Exits: ");
  78. if(currentRoom.northExit != null)
  79. System.out.print("north ");
  80. if(currentRoom.eastExit != null)
  81. System.out.print("east ");
  82. if(currentRoom.southExit != null)
  83. System.out.print("south ");
  84. if(currentRoom.westExit != null)
  85. System.out.print("west ");
  86. System.out.println();
  87. }
  88. /**
  89. * Diberikan perintah, proses (yaitu: mengeksekusi) perintah.
  90. * Jika perintah ini mengakhiri permainan, true dikembalikan, jika tidak false adalah
  91. * kembali.
  92. */
  93. private boolean processCommand(Command command)
  94. {
  95. boolean wantToQuit = false;
  96. if(command.isUnknown()) {
  97. System.out.println("I don't know what you mean...");
  98. return false;
  99. }
  100. String commandWord = command.getCommandWord();
  101. if (commandWord.equals("help"))
  102. printHelp();
  103. else if (commandWord.equals("go"))
  104. goRoom(command);
  105. else if (commandWord.equals("quit"))
  106. wantToQuit = quit(command);
  107. return wantToQuit;
  108. }
  109. // implementations of user commands:
  110. /**
  111. * Cetak beberapa informasi bantuan.
  112. * Di sini kami mencetak beberapa pesan yang bodoh dan samar serta daftar
  113. * kata perintah.
  114. */
  115. private void printHelp()
  116. {
  117. System.out.println("You are lost. You are alone. You wander");
  118. System.out.println("around at the university.");
  119. System.out.println();
  120. System.out.println("Your command words are:");
  121. System.out.println(" go quit help");
  122. }
  123. /**
  124. * Cobalah untuk pergi ke satu arah. Jika ada jalan keluar, masuk
  125. * ruang baru, jika tidak cetak pesan kesalahan.
  126. */
  127. private void goRoom(Command command)
  128. {
  129. if(!command.hasSecondWord()) {
  130. // if there is no second word, we don't know where to go...
  131. System.out.println("Go where?");
  132. return;
  133. }
  134. String direction = command.getSecondWord();
  135. // Try to leave current room.
  136. Room nextRoom = null;
  137. if(direction.equals("north"))
  138. nextRoom = currentRoom.northExit;
  139. if(direction.equals("east"))
  140. nextRoom = currentRoom.eastExit;
  141. if(direction.equals("south"))
  142. nextRoom = currentRoom.southExit;
  143. if(direction.equals("west"))
  144. nextRoom = currentRoom.westExit;
  145. if (nextRoom == null)
  146. System.out.println("There is no door!");
  147. else {
  148. currentRoom = nextRoom;
  149. System.out.println("You are " +
  150. currentRoom.getDescription());
  151. System.out.print("Exits: ");
  152. if(currentRoom.northExit != null)
  153. System.out.print("north ");
  154. if(currentRoom.eastExit != null)
  155. System.out.print("east ");
  156. if(currentRoom.southExit != null)
  157. System.out.print("south ");
  158. if(currentRoom.westExit != null)
  159. System.out.print("west ");
  160. System.out.println();
  161. }
  162. }
  163. /**
  164. * "Quit" telah dimasukkan. Periksa sisa perintah untuk melihat
  165. * apakah kita benar-benar keluar dari permainan. Kembalikan true, jika perintah ini
  166. * keluar dari permainan, salah jika tidak.
  167. */
  168. private boolean quit(Command command)
  169. {
  170. if(command.hasSecondWord()) {
  171. System.out.println("Quit what?");
  172. return false;
  173. }
  174. else
  175. return true; // signal that we want to quit
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment