Tugas 8 Implementasi Game of Zuul

Nama : Afel Allaric Absor
NRP   : 5025231140
Kelas  : Pemrograman Berbasis Objek (A)

                                                     Tugas 8 Implementasi Game of Zuul

1). Game.java:

public class Game {

    private Parser parser;

    private boolean finished;

    private Room currentRoom;

    private Room exitRoom;


    public Game() {

        createRooms();

        parser = new Parser();

        finished = false;

    }


    private void createRooms() {

        Room outside = new Room("outside the main entrance of the university");

        Room mainHall = new Room("in the main hall");

        Room hallway = new Room("in the hallway");

        Room cafeteria = new Room("in the cafeteria");

        Room library = new Room("in the library");

        exitRoom = new Room("at the university's exit");

        

        outside.setExit("east", mainHall);


        mainHall.setExit("west", outside);

        mainHall.setExit("north", hallway);

        mainHall.setExit("east", cafeteria);

        mainHall.setExit("south", exitRoom);


        hallway.setExit("south", mainHall);

        hallway.setExit("east", cafeteria);

        hallway.setExit("west", library);


        cafeteria.setExit("west", hallway);

        cafeteria.setExit("south", exitRoom);

        cafeteria.setExit("north", mainHall);


        library.setExit("east", hallway);


        exitRoom.setExit("north", mainHall);


        // Starting point: outside

        currentRoom = outside;

    }


    public void play() {

        printWelcome();


        while (!finished) {

            Command command = parser.getCommand();

            finished = processCommand(command);

        }

        System.out.println("Thank you for playing. Goodbye!");

    }


    private void printWelcome() {

        System.out.println();

        System.out.println("Welcome to the World of Zuul!");

        System.out.println("World of Zuul is a new, incredibly boring adventure game.");

        System.out.println("Type 'help' if you need help.");

        System.out.println();

        System.out.println(currentRoom.getDescription());

        printCurrentExits();

    }


    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(CommandWords.HELP)) {

            printHelp();

        } else if (commandWord.equals(CommandWords.GO)) {

            goRoom(command);

        } else if (commandWord.equals(CommandWords.QUIT)) {

            wantToQuit = true;

        }


        return wantToQuit;

    }


    private void printHelp() {

        System.out.println("You are " + currentRoom.getDescription());

        System.out.println("To find the exit, try using directions like 'go east', 'go west', 'go north', or 'go south'.");

        printCurrentExits();

    }


    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 exit in that direction!");

        } else {

            currentRoom = nextRoom;

            System.out.println("You are now " + currentRoom.getDescription());

            printCurrentExits();


            if (currentRoom == exitRoom) {

                System.out.println("Congrats, you win the game!");

                finished = true;

            }

        }

    }


    private void printCurrentExits() {

        System.out.println("Exits: " + currentRoom.getExits());

    }


    public static void main(String[] args) {

        Game game = new Game();

        game.play();

    }

}

2). CommandWords.java :
public class CommandWords {
    public static final String GO = "go";
    public static final String QUIT = "quit";
    public static final String HELP = "help";

    private static final String[] validCommands = {GO, QUIT, HELP};

    public boolean isCommand(String commandWord) {
        for (String validCommand : validCommands) {
            if (validCommand.equals(commandWord)) {
                return true;
            }
        }
        return false;
    }

    public void showAll() {
        for (String command : validCommands) {
            System.out.print(command + " ");
        }
        System.out.println();
    }
}

3). Parser.java :
import java.util.Scanner;

public class Parser {
    private CommandWords commands;
    private Scanner reader;

    public Parser() {
        commands = new CommandWords();
        reader = new Scanner(System.in);
    }

    public Command getCommand() {
        System.out.print("> ");
        String inputLine = reader.nextLine();
        String word1 = null;
        String word2 = null;

        Scanner tokenizer = new Scanner(inputLine);
        if (tokenizer.hasNext()) {
            word1 = tokenizer.next();
            if (tokenizer.hasNext()) {
                word2 = tokenizer.next();
            }
        }

        if (commands.isCommand(word1)) {
            return new Command(word1, word2);
        } else {
            return new Command(null, word2);
        }
    }
}

4). Command.java :
public class Command {
    private String commandWord;
    private String secondWord;

    public Command(String firstWord, String secondWord) {
        commandWord = firstWord;
        this.secondWord = secondWord;
    }

    public String getCommandWord() {
        return commandWord;
    }

    public String getSecondWord() {
        return secondWord;
    }

    public boolean hasSecondWord() {
        return secondWord != null;
    }

    public boolean isUnknown() {
        return commandWord == null;
    }
}

5). Room.java :
import java.util.HashMap;

public class Room {
    private String description;
    private HashMap<String, Room> exits;

    public Room(String description) {
        this.description = description;
        this.exits = new HashMap<>();
    }

    public String getDescription() {
        return description;
    }

    public void setExit(String direction, Room neighbor) {
        exits.put(direction, neighbor);
    }

    public Room getExit(String direction) {
        return exits.get(direction);
    }

    public String getExits() {
        return String.join(", ", exits.keySet());
    }
}

6). Hasil Akhir :

7). Link repository github berisi source code : https://github.com/afelallaric/Tugas-8-PBO-Implementasi-Game-of-Zuul.git



Komentar

Postingan populer dari blog ini

PWEB 15 Desain Final Project

Tugas 8 Pemrograman Web (E) Membuat Form Register & Login Menggunakan Bootstrap

Tugas PBO 14 GUI & Panel Login