Java-Adventure-Game / Java-Based Adventure Game main / AdventureGameModel / Room.java
Room.java
Raw
package AdventureGameModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.StringJoiner;

/**
 * This class contains the information about a 
 * room in the Adventure Game
 */
public class Room implements Serializable {
    private final String adventureName;
    private int roomNumber; // the number of the room
    private String roomName; // the name of the room
    private String roomDescription; // the description of the room
    private PassageTable motionTable = new PassageTable(); // the passage table for the room
    public ArrayList<AdventureObject> objectsInRoom = new ArrayList<AdventureObject>(); // the list of objects in the room
    private boolean isVisited; // check if room has been visited or not

    /**
     * AdvGameRoom constructor
     *
     * @param roomName: The name of the room
     * @param roomNumber: The number of the room
     * @param roomDescription: The description of the room
     */
    public Room(String roomName, int roomNumber, String roomDescription, String adventureName){
        this.roomName = roomName;
        this.roomNumber = roomNumber;
        this.roomDescription = roomDescription;
        this.adventureName = adventureName;
        this.isVisited = false;
    }

    /**
     * Returns a comma delimited list of every
     * object's description that is in the given room,
     * e.g. "a can of tuna, a beagle, a lamp"
     *
     * @return delimited string of object descriptions
     */
    public String getObjectString() {
        StringJoiner joiner = new StringJoiner(", ");
        for (int i = 0; i < this.objectsInRoom.size(); i++) {
            joiner.add(this.objectsInRoom.get(i).getDescription());
        }
        String want = joiner.toString();
        return want;
    }

    /**
     * Returns a comma delimited list of every
     * move that is possible from the given room,
     * e.g. "DOWN, UP, NORTH, SOUTH"
     *
     * @return delimited string of possible moves
     */
    public String getCommands() {
        StringJoiner joiner = new StringJoiner(", ") ;
        ArrayList<String> all_duplicate_directions = new ArrayList<>();
        for (int i = 0; i < this.motionTable.passageTable.size(); i++) {
            if (!all_duplicate_directions.contains(this.motionTable.passageTable.get(i).getDirection())) {
                all_duplicate_directions.add(this.motionTable.passageTable.get(i).getDirection());
                joiner.add(this.motionTable.passageTable.get(i).getDirection());
            }
        }
        String want = joiner.toString();
        return want;

    }

    /**
     * This method adds a game object to the room
     *
     * @param object to be added to the room
     */
    public void addGameObject(AdventureObject object){
        this.objectsInRoom.add(object);
    }

    /**
     * This method removes a game object from the room
     *
     * @param object to be removed from the room
     */
    public void removeGameObject(AdventureObject object){
        this.objectsInRoom.remove(object);
    }

    /**
     * This method checks if an object is in the room
     *
     * @param objectName Name of the object to be checked
     * @return True if the object is present in the room, False otherwise
     */
    public boolean checkIfObjectInRoom(String objectName){
        for (int i = 0; i<objectsInRoom.size();i++){
            if(this.objectsInRoom.get(i).getName().equals(objectName)) return true;
        }
        return false;
    }

    /**
     * Sets the visit status of the room to true
     */
    public void visit(){
        isVisited = true;
    }

    /**
     * Getter for returning an AdventureObject with a given name
     *
     * @param objectName: Object name to find in the room
     * @return: AdventureObject
     */
    public AdventureObject getObject(String objectName){
        for (int i = 0; i<objectsInRoom.size();i++){
            if(this.objectsInRoom.get(i).getName().equals(objectName)) return this.objectsInRoom.get(i);
        }
        return null;
    }

    /**
     * Getter method for the number attribute
     *
     * @return: number of the room
     */
    public int getRoomNumber(){
        return this.roomNumber;
    }

    /**
     * Getter method for the description attribute
     *
     * @return: description of the room
     */
    public String getRoomDescription(){
        return this.roomDescription.replace("\n", " ");
    }


    /**
     * Getter method for the name attribute
     *
     * @return: name of the room
     */
    public String getRoomName(){
        return this.roomName;
    }


    /**
     * Getter method for the visit attribute
     *
     * @return: visit status of the room
     */
    public boolean getVisited(){
        return this.isVisited;
    }


    /**
     * Getter method for the motionTable attribute
     *
     * @return: motion table of the room
     */
    public PassageTable getMotionTable(){
        return this.motionTable;
    }
}