Mochamad Kolbi Nuron_5025231183_Tugas PBO 8
Nama : Mochamad Kolbi Nuron
NRP : 5025231183
PBO A tugas ke 8 implementasi world of zuul
1. room.java
Penjelasan:
Room
adalah representasi sebuah ruangan dalam permainan.- Variabel
description
menyimpan teks deskripsi ruangan, seperti "You are in the kitchen." HashMap exits
menyimpan hubungan antar ruangan. Misalnya:north
→ Kitcheneast
→ Bedroom
- Metode utama:
setExit()
: Menambahkan pintu keluar ke ruangan lain.getShortDescription()
: Memberikan deskripsi ruangan.getExitString()
: Memberikan daftar semua pintu keluar dalam format string.getExit()
: Mengembalikan ruangan di arah tertentu.
2. game.java
import java.util.Scanner;
public class game {
private room currentRoom;
public game() {
createRooms();
}
private void createRooms() {
room hall, kitchen, bedroom, garden, study;
hall = new room("You are in the main hall. A grand room with portraits on the walls.");
kitchen = new room("You are in the kitchen. It smells like freshly baked cookies.");
bedroom = new room("You are in the bedroom. The bed looks very comfortable.");
garden = new room("You are in the garden. The flowers are in full bloom.");
study = new room("You are in the study. Books are piled everywhere.");
// Set exits for each room
hall.setExit("north", kitchen);
hall.setExit("east", bedroom);
hall.setExit("west", study);
kitchen.setExit("south", hall);
bedroom.setExit("west", hall);
study.setExit("east", hall);
study.setExit("north", garden);
garden.setExit("south", study);
currentRoom = hall; // Start game in the hall
}
public void play() {
printWelcome();
Scanner scanner = new Scanner(System.in);
boolean finished = false;
while (!finished) {
System.out.print("> ");
String input = scanner.nextLine();
finished = processCommand(input);
}
scanner.close();
}
private void printWelcome() {
System.out.println("Welcome to the World of Zuul!");
System.out.println("Explore the rooms and enjoy the adventure.");
System.out.println("Type 'help' if you need assistance.");
System.out.println();
System.out.println(currentRoom.getShortDescription());
System.out.println("Exits: " + currentRoom.getExitString());
}
private boolean processCommand(String input) {
if (input.equalsIgnoreCase("help")) {
printHelp();
return false;
} else if (input.startsWith("go")) {
String direction = input.substring(3).trim();
goRoom(direction);
return false;
} else if (input.equalsIgnoreCase("look")) {
System.out.println(currentRoom.getShortDescription());
System.out.println("Exits: " + currentRoom.getExitString());
return false;
} else if (input.equalsIgnoreCase("quit")) {
System.out.println("Thank you for playing. Goodbye!");
return true;
} else {
System.out.println("I don't understand that command.");
return false;
}
}
private void printHelp() {
System.out.println("You can use the following commands:");
System.out.println(" go [direction] - Move to another room.");
System.out.println(" look - Look around the current room.");
System.out.println(" help - Show this help message.");
System.out.println(" quit - Exit the game.");
}
private void goRoom(String direction) {
room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no exit in that direction!");
} else {
currentRoom = nextRoom;
System.out.println(currentRoom.getShortDescription());
System.out.println("Exits: " + currentRoom.getExitString());
}
}
}
Game
adalah logika utama permainan.- Metode utama:
createRooms()
: Membuat semua ruangan dan hubungan antar ruangan.play()
: Memulai permainan, menerima input, dan menjalankan perintah.printWelcome()
: Menampilkan pesan sambutan dan informasi ruangan saat ini.processCommand()
: Memproses perintah pemain:help
: Menampilkan daftar perintah.go [direction]
: Berpindah ruangan.look
: Melihat informasi ruangan saat ini.quit
: Mengakhiri permainan.
goRoom()
: Memindahkan pemain ke ruangan yang sesuai arah.
Komentar
Posting Komentar