Naufal Daffa Alfa Zain 5025241066 Pemrograman Berorientasi Objek – A
Pada pertemuan kesembilan mata kuliah Pemrograman Berorientasi Objek (PBO A), kami diminta untuk membuat dan menjalankan proyek Game of Zuul menggunakan bahasa Java di platform BlueJ. Program ini merupakan permainan berbasis teks sederhana di mana pemain dapat berpindah-pindah ruangan menggunakan perintah tertentu seperti go, help, dan quit. Melalui proyek ini, mahasiswa dilatih untuk memahami hubungan antarobjek dalam sebuah sistem serta bagaimana setiap kelas saling berinteraksi untuk membentuk alur permainan yang utuh.
Secara garis besar, Game of Zuul terdiri dari beberapa kelas yang merepresentasikan elemen-elemen dalam permainan seperti ruang, perintah, pembacaan input pengguna, dan logika permainan itu sendiri. Ketika dijalankan, program menampilkan deskripsi lokasi pemain, arah keluarnya, serta merespons perintah yang dimasukkan melalui terminal. Pemain bisa berpindah ruangan sesuai arah yang tersedia, meminta bantuan untuk melihat daftar perintah, atau keluar dari permainan dengan mengetik quit.
Melalui latihan ini, kami belajar tentang penerapan prinsip dasar pemrograman berorientasi objek seperti enkapsulasi, komposisi, dan interaksi antarobjek. Setiap kelas memiliki tanggung jawab tersendiri dan saling berhubungan untuk membentuk alur kerja yang utuh. Selain itu, latihan ini juga membantu kami memahami pentingnya desain kelas yang kohesif dan memiliki low coupling agar program mudah dikembangkan dan dipelihara.
Secara keseluruhan, proyek Game of Zuul menjadi sarana pembelajaran yang efektif untuk memperdalam konsep OOP melalui studi kasus permainan sederhana yang interaktif dan aplikatif di BlueJ.
Sumber source code dapat dilihat di Sumber
Source code lengkap
Game
public class Game
{
private Parser parser;
private Room currentRoom;
// Constructor, create game, inisialisasi map (rooms)
public Game() {
createRooms();
parser = new Parser();
}
// Create all rooms, and link exits
private void createRooms() {
Room outside, theater, pub, lab, office;
//create rooms
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 room exit
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);
//set start point
currentRoom = outside;
}
//play routine
public void play() {
printWelcome();
//masuk main command loop.
boolean finished = false;
while(!finished) {
Command command = parser.getCommand();
finished = processCommand(command);
}
System.out.println("Thank you for playing. Good bye.");
}
//print welcome message
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 advanture game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println("You are " + currentRoom.getDescription());
System.out.print("Exits: ");
if(currentRoom.northExit != null) System.out.print("north ");
if(currentRoom.eastExit != null) System.out.print("east ");
if(currentRoom.southExit != null) System.out.print("south ");
if(currentRoom.westExit != null) System.out.print("west ");
System.out.println();
}
private boolean processCommand(Command command) {
boolean wantToQuit = false;
if(command.isUnknown()) {
System.out.println("I'm sorry, 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.");
System.out.println("You wander around at the campus.");
System.out.println();
System.out.println("Your command words are: ");
System.out.println(" go quit help");
}
//try to go one direction
private void goRoom(Command command) {
if(!command.hasSecondWord()) {
System.out.println("Go where?");
System.out.println("'go <direction>'");
return;
}
String direction = command.getSecondWord();
//try to leave current room.
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.print("Exits: ");
if(currentRoom.northExit != null) System.out.print("north ");
if(currentRoom.eastExit != null) System.out.print("east ");
if(currentRoom.southExit != null) System.out.print("south ");
if(currentRoom.westExit != null) System.out.print("west ");
System.out.println();
}
}
private boolean quit(Command command){
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
} else return true;
}
}
Room
public class Room
{
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
public Room(String description)
{
this.description = description;
}
public void setExits(Room north, Room east, Room south, Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = south;
if(west != null)
westExit = west;
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
}
Command
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 isUnknown()
{
return (commandWord == null);
}
public boolean hasSecondWord()
{
return (secondWord != null);
}
}
Parser
import java.util.Scanner;
public class Parser
{
private CommandWords commands; // holds all valid command words
private Scanner reader; // source of command input
public Parser()
{
commands = new CommandWords();
reader = new Scanner(System.in);
}
public Command getCommand()
{
String inputLine; // will hold the full input line
String word1 = null;
String word2 = null;
System.out.print("> "); // print prompt
inputLine = reader.nextLine();
// Find up to two words on the line.
Scanner tokenizer = new Scanner(inputLine);
if(tokenizer.hasNext()) {
word1 = tokenizer.next(); // get first word
if(tokenizer.hasNext()) {
word2 = tokenizer.next(); // get second word
// note: we just ignore the rest of the input line.
}
}
if(commands.isCommand(word1)) {
return new Command(word1, word2);
}
else {
return new Command(null, word2);
}
}
}
CommandWords
public class CommandWords
{
// a constant array that holds all valid command words
private static final String[] validCommands = {
"go", "quit", "help"
};
/**
* Constructor - initialise the command words.
*/
public CommandWords()
{
// nothing to do at the moment...
}
public boolean isCommand(String aString)
{
for(int i = 0; i < validCommands.length; i++) {
if(validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
}
}
No comments:
Post a Comment