0
votes

In my game there are several class I have written including room, lamp, chest, java, player, key and map. These have all been tested and are correct so now I am writing my adventure class which is the driver for the program. I need to set the players room location to [0][0] and i can't figure out how. here is what i have so far in my room and adventure class.

public class Adventure {

    Scanner in = new Scanner(System.in);
    private Room room;

    public Adventure() {
        Player player = new Player();
        Map map = new Map();
        player.setX(0);
        player.setY(0);
        int x = 0;
        int y = 0;
        map.getRoom(x, y).getDescription(); 
    }
}

public class Room {

    private String description;
    private boolean north;
    private boolean south;
    private boolean east;
    private boolean west;
    private boolean isDark;

    private Lamp theLamp;
    private Key theKey;
    private Chest theChest;

    /**
     * Returns the text description of this room
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns true if the player can go north from this room
     */
    public boolean canGoNorth() {
        return north;
    }

    /**
     * Returns true if the player can go south from this room
     */
    public boolean canGoSouth() {
        return south;
    }

    /**
     * Returns true if the player can go east from this room
     */
    public boolean canGoEast() {
        return east;
    }

    /**
     * Returns true if the player can go west from this room
     */
    public boolean canGoWest() {
        return west;
    }

    /**
     * Returns the lamp object in this room.
     * If no lamp is present, returns null
     */
    public Lamp getLamp() {
        return theLamp;
    }

    /**
     * Sets the lamp variable in this room to null
     */
    public void clearLamp() {
        theLamp = null;
    }

    /**
     * Returns the key object in this room. 
     * If no key is present, returns null
     */
    public Key getKey() {
        return theKey;
    }

    /**
     * Sets the key variable in this room to null
     */
    public void clearKey() {
        theKey = null;
    }

    /**
     * Returns the chest object in this room.
     * If no chest is present, returns null
     */
    public Chest getChest() {
        return theChest;
    }

    /**
     * Returns true if there is no light in this room,
     * veeeeeeeery dangerous!
     */
    public boolean isDark() {
        return isDark;
    }


    /**
     * Hey wassup dawg?  I'ma constructor.  I make the objects round these parts, 
     * sometimes without even trying, knowwhatimssayin?
     * Yall don't haveta worry 'bout me for this'ere game, but look me up in 
     * Chapter 6 sometime. Kay?
     *   
     */
    public Room(String description, boolean north, boolean south, boolean east,
            boolean west, boolean isDark, Lamp theLamp, Key theKey,
            Chest theChest) {
        super();
        this.description = description;
        this.north = north;
        this.south = south;
        this.east = east;
        this.west = west;
        this.isDark = isDark;
        this.theLamp = theLamp;
        this.theKey = theKey;
        this.theChest = theChest;
    }
}

I have to set the room location to 0,0 so that a description in the map class will print out.

1
Create x and y variables in the Room class, including setters and getters maybe? Seems like you did the same thing in the Player class. But maybe I'm not understanding fully what the problem is.Mumfi
What's the Map api? Players and rooms would probably have a location on the Map...plalx
Why don't you add a reference to players in your room class? Something like "playersIn" list, where you add the player object with a setter.Matteo Di Napoli
something like this after you init the map? map.setRoom(0, 0, new Room("This is the players room", false, true, true, false, false, myLamp, myKey));TobiasR.

1 Answers

0
votes

You already have a Room object, so is it really necessary to store the X/Y co-ordinates of each Room? You can create a structure of Room objects which are linked to each other via the North/South/East/West Room objects (if applicable).

You can then have a variable within your player class such as "currentRoom", or "location". Include a function to get and set this location and you can now set and access the current location of the character.

If X/Y is really important to you, I guess in your adventure class you can create a 2-dimensional array of Room objects, and then use X/Y co-ordinates to locate Rooms within this structure (i.e. "x" might refer to the row and "y" refers to the column to locate locations stored in the array).

However it might be easier, less wasteful, and more extensible to create Room objects as you go and link them to other room objects via exit/Room pointers.

public class Player {
    private Room location = null;
    public void setLocation(Room newLocation) {
        location = newLocation;
    }
}

public class Room {
    private Room NorthExit = null;

    public Room getNorthExit() {
        return NorthExit;
    }

    public setNorthExit(Room newRoom) {
        NorthExit = newRoom;
    }
}


// In main somewhere...
Room room1 = new Room();
Room room2 = new Room();
room1.setNorthExit(room2);

Player player1 = new Player();
player1.setLocation(room1);

You can see in the code above we create and link two rooms, if we are in the first room the user can set room1 to their location, and access all the information about room1 from their location object. We can also access any neighbouring rooms from our location object, through Location objects/references for each exit.