I am trying to add an item to a game that will "float" a player into the correct room. The code below is where the boat is added to a room and the the boolean newBoat is set to true:

        Item boat = new Item(" A boat to help you cross the lake \n", "boat", 10);
        newBoat = true;
        roomThree.addItem(boat);

Below is the method where I am trying to implement the command "float" which if the player has the boat and newBoat is true, currentRoom = roomFour;

    /**
     * 
     */
    private void useBoat(Command command)
    {
        if (newBoat = true) {
            currentRoom = roomFour;
            System.out.println("You have successfully crossed the lake");
        }
        newBoat = false;
        if (newBoat = false) {
            System.out.println("You do not have a boat");
        }
    }

However, it will not set the currentRoom to roomFour but it will print out the line.

Recommended Answers

All 3 Replies

Well...

First, you set newBoat to false on line 10 then try to see if it's false on line 11 - which ot always will be!

Second, on line 11 you have coded an assignment rather than an equality operator

Thank you for the help but after I use 'float', using the command 'look' results in an NPE.

    /**
     * 
     */private void look()
     {
         System.out.println(currentRoom.getLongDescription());
     }

There's only one thing on that line that could cause an NPE and that's currentRoom - you;ll get an NPE when you try to use that when its null to call getLongDescription.
You'll have to look to the rest of your code and maybe add a few print sttements to find out why look() is being called when currentRoom is null

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.