PBO 9
muhammad rafi budi purnama
5025221307
PBO 9
code : https://github.com/mbahbud/PBO.git
World Of Zuul
Penugasan kali ini kita diminta mengerjakan program "World of Zuul"
"World of Zuul" adalah nama sebuah proyek game berbasis teks klasik yang sering digunakan sebagai aplikasi akhir untuk mengajarkan konsep pemrograman. Game ini merupakan implementasi dari game petualangan teks klasik, di mana pemain menjelajahi peta dengan mengetikkan perintah seperti "go" (pergi) untuk bergerak antar lokasi dan "take" (ambil) untuk mengambil item.
Berikut implmentasi sederhana dari World Of Zuul
Berdasarkan class diagram tersebut terdapat beberapa Class yang memiliki isi kode seperti berikut
GameMain.java
public class GameMain {
public static void main(String[] args) {
Game game = new Game();
game.play();
}
}
Game.java
public class Game {
private Parser parser;
private Room currentRoom;
public Game() {
createRooms();
parser = new Parser();
}
private void createRooms() {
Room outside, hall, kitchen, bedroom;
outside = new Room("outside the main house");
hall = new Room("in the main hall");
kitchen = new Room("in the kitchen");
bedroom = new Room("in the bedroom");
outside.setExit("north", hall);
hall.setExit("south", outside);
hall.setExit("east", kitchen);
hall.setExit("west", bedroom);
kitchen.setExit("west", hall);
bedroom.setExit("east", hall);
currentRoom = outside;
}
public void play() {
printWelcome();
boolean finished = false;
while (!finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Goodbye!");
}
private void printWelcome() {
System.out.println("Welcome to the World of Zuul!");
System.out.println("Type 'help' if you need assistance.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
private boolean processCommand(Command command) {
if (command.isUnknown()) {
System.out.println("I don't know what you mean...");
return false;
}
CommandWord commandWord = command.getCommandWord();
switch (commandWord) {
case HELP:
printHelp();
break;
case GO:
goRoom(command);
break;
case QUIT:
return quit(command);
}
return false;
}
private void printHelp() {
System.out.println("You can use these commands:");
System.out.println(" go <direction>");
System.out.println(" help");
System.out.println(" quit");
}
private void goRoom(Command command) {
if (!command.hasSecondWord()) {
System.out.println("Go where?");
return;
}
String direction = command.getSecondWord();
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("There is no door in that direction!");
} else {
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());
}
}
private boolean quit(Command command) {
if (command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
return true;
}
}
Room.java
import java.util.HashMap;
public class Room {
private String description;
private HashMap<String, Room> exits;
public Room(String description) {
this.description = description;
exits = new HashMap<>();
}
public void setExit(String direction, Room neighbor) {
exits.put(direction, neighbor);
}
public String getShortDescription() {
return description;
}
public String getLongDescription() {
return "You are " + description + ".\n" + getExitString();
}
private String getExitString() {
String returnString = "Exits:";
for (String exit : exits.keySet()) {
returnString += " " + exit;
}
return returnString;
}
public Room getExit(String direction) {
return exits.get(direction);
}
}
Parser.java
import java.util.Scanner;
public class Parser {
private CommandWords commands;
private Scanner scanner;
public Parser() {
commands = new CommandWords();
scanner = new Scanner(System.in);
}
public Command getCommand() {
System.out.print("> ");
String inputLine = scanner.nextLine();
String word1 = null;
String word2 = null;
Scanner tokenizer = new Scanner(inputLine);
if (tokenizer.hasNext())
word1 = tokenizer.next();
if (tokenizer.hasNext())
word2 = tokenizer.next();
return new Command(commands.getCommandWord(word1), word2);
}
public String showCommands() {
return commands.getCommandList();
}
}
CommandWords.java
import java.util.HashMap;
public class CommandWords {
private HashMap<String, CommandWord> validCommands;
public CommandWords() {
validCommands = new HashMap<>();
validCommands.put("go", CommandWord.GO);
validCommands.put("help", CommandWord.HELP);
validCommands.put("quit", CommandWord.QUIT);
}
public CommandWord getCommandWord(String commandWord) {
CommandWord value = validCommands.get(commandWord);
if (value != null) {
return value;
} else {
return CommandWord.UNKNOWN;
}
}
public String getCommandList() {
return "go help quit";
}
}
CommandWord.java
public enum CommandWord {
GO, HELP, QUIT, UNKNOWN;
}
Command.java
public class Command {
private CommandWord commandWord;
private String secondWord;
public Command(CommandWord commandWord, String secondWord) {
this.commandWord = commandWord;
this.secondWord = secondWord;
}
public CommandWord getCommandWord() {
return commandWord;
}
public String getSecondWord() {
return secondWord;
}
public boolean hasSecondWord() {
return secondWord != null;
}
public boolean isUnknown() {
return commandWord == CommandWord.UNKNOWN;
}
}
hasil dari gabungan class terebut adalah seperti berikut
Komentar
Posting Komentar