Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Merupakan Main Class dari World of zuul
- * versi sederhana untuk sebuah world of zuul
- * pemain bisa bergerak dengan banyak kondisi sehingga menyenangkan
- *
- * untuk memainkan nya dibutuhkan instansi dari class ini yaitu dengan
- * "call" method
- *
- * class ini menginisialisasikan class yang lain
- * semua rooms,membuat class parsers dan mulai game.
- * class ini mengevaluasi dan mengeksekusi perintah dari class parser
- *
- * @author Dwinanda Bagoes Ansori
- * @version 24 November 2020
- */
- public class Game
- {
- private Parser parser;
- private Room currentRoom;
- // membuat game dan petanya
- public Game()
- {
- createRooms();
- parser = new Parser();
- }
- //membuat semua ruangan dan menhubungkan ruangan-ruangan tersebut.
- private void createRooms()
- {
- Room outside, theater, pub, lab, office;
- outside = new Room ("outside the main entrance of the university");
- theater = new Room ("in a lecture theater");
- pub = new Room ("in the campus pub");
- lab = new Room ("in a computing lab");
- office = new Room ("in the computing admin office");
- // inisialisasi arah keluar masing masing room
- outside.setExits(null, theater, lab, pub);
- theater.setExits(null, null, null, outside);
- pub.setExits(null, outside, null, null);
- lab.setExits(outside, office, null, null);
- office.setExits(null, null, null, lab);
- currentRoom = outside; //mulai game nya dari outside
- }
- /**
- * dilakukan gamenya sampai selesai
- */
- public void play()
- {
- printWelcome();
- // masuk ke loop perintah
- // eksekusi sampai game selesai
- boolean finished = false;
- while (!finished)
- {
- Command command = parser.getCommand();
- finished = processCommand(command);
- }
- System.out.println("Thank you for playing. Good Bye");
- }
- /**
- * Pesan awalan.
- */
- private void printWelcome()
- {
- System.out.println();
- System.out.println("Welcome to Adventure");
- System.out.println("Adventure is a new, incredibly boring adventure game");
- System.out.println("Type 'help' if you need help.");
- System.out.println();
- System.out.println("You are " + currentRoom.getDescription());
- System.out.println("Exits: ");
- if (currentRoom.northExit != null)
- System.out.println("north ");
- if (currentRoom.eastExit != null)
- System.out.println("east ");
- if (currentRoom.southExit != null)
- System.out.println("south ");
- if (currentRoom.westExit != null)
- System.out.println("west ");
- }
- /**
- * perintah akan dieksekusi
- * @param command akan diproses.
- * @return true jika menyeleseikan game nya.
- */
- private boolean processCommand(Command command)
- {
- boolean wantToQuit = false;
- if (command.isUnknown())
- {
- System.out.println("I don't know what you mean...");
- return false;
- }
- String commandWord = command.getCommandWord();
- if (commandWord.equals("help"))
- printHelp();
- else if (commandWord.equals("go"))
- goRoom(command);
- else if (commandWord.equals("quit"))
- wantToQuit = quit(command);
- return wantToQuit;
- }
- private void printHelp()
- {
- System.out.println("You are lost. You are alone. You wander");
- System.out.println("around at the university");
- System.out.println();
- System.out.println("Your command words are:");
- System.out.println(" go quit help");
- }
- /**
- * lakukan pergi ke suatu arah jika ada keluar maka pindah tempat
- * jika tidak jadi error
- */
- private void goRoom(Command command)
- {
- if(!command.hasSecondWord())
- {
- // jika tidak ada kata kedua, sehingga jadi sesat
- System.out.println("Go where?");
- return;
- }
- String direction = command.getSecondWord();
- // pindah pindah tempat
- Room nextRoom = null;
- if (direction.equals("north"))
- nextRoom = currentRoom.northExit;
- if (direction.equals("east"))
- nextRoom = currentRoom.eastExit;
- if (direction.equals("south"))
- nextRoom = currentRoom.southExit;
- if (direction.equals("west"))
- nextRoom = currentRoom.westExit;
- if (nextRoom == null)
- System.out.println("There is no door!");
- else
- {
- currentRoom = nextRoom;
- System.out.println("You are " + currentRoom.getDescription());
- System.out.println("Exits: ");
- if (currentRoom.northExit != null)
- System.out.println("north ");
- if (currentRoom.eastExit != null)
- System.out.println("east ");
- if (currentRoom.southExit != null)
- System.out.println("south ");
- if (currentRoom.westExit != null)
- System.out.println("west ");
- }
- }
- /**
- * apabila ada user enter 'quit' maka check semua command untuk melihat apakah kita bisa
- * keluar dari game tersebut
- * @return true jika bisa keluar dan false jika tidak
- */
- private boolean quit(Command command)
- {
- if (command.hasSecondWord())
- {
- System.out.println("Quit where?");
- return false;
- }
- else
- return true;
- }
- }
Add Comment
Please, Sign In to add comment