I am having trouble adding an item that will increase a player's energy in my BlueJ game. At the bottom of the createRooms() method in my game class I have added several items to rooms. At that point I tried setting boolean newEnergy equal to true. If the player types "use", they will receive 10 energy if boolean newEnergy is equal to true. I am not sure how I would set boolean newEnergy to true only when the energy item is still in the room.

/**
     * Create all the rooms and link their exits together.
     */
    private void createRooms()
    {
        Room entrance, roomOne, roomTwo, cavern, stream, roomThree, lake, roomFour, roomFive, roomSix, roomSeven, exit;

        // create the rooms
        entrance = new Room("are in the first room of the cave system", "entrance");                        
        roomOne = new Room("are in a dark, empty room", "roomOne");
        roomTwo = new Room("are in a dark room surrounded by bats", "roomTwo");
        cavern = new Room("are in a large cavern within the cave", "cavern");
        stream = new Room("have found a small stream", "stream");
        roomThree = new Room("are in a small empty room", "roomThree");
        lake = new Room("have come across a lake and must cross it. \n" +
                        "There are two ways to cross it. If you choose the wrong \n" +
                        "way you will die", "lake");
        roomFour = new Room("have stumbled upon a large room with four exits", "roomFour");
        roomFive = new Room("are in a room and there is a faint light in the distance", "roomFive");
        roomSix = new Room("appear to be getting closer to the exit", "roomSix");
        roomSeven = new Room("have fallen into a sinkhole", "roomSeven");
        exit = new Room("have found the exit to the cave! Type 'quit' to exit the game or 'north' to restart", "exit");

        // initialise room exits
        entrance.setExit("east", roomOne);
        entrance.setExit("south", roomTwo);
        entrance.setExit("west", stream);

        roomOne.setExit("south", roomTwo);

        roomTwo.setExit("north", entrance);
        roomTwo.setExit("west", stream);

        cavern.setExit("north", roomTwo);
        cavern.setExit("east", roomThree);

        stream.setExit("south", cavern);

        roomThree.setExit("north", roomOne);
        roomThree.setExit("south", lake);

        lake.setExit("north", entrance);
        lake.setExit("west", roomFour);

        roomFour.setExit("north", cavern);
        roomFour.setExit("south", roomFive);
        roomFour.setExit("west", stream);
        roomFour.setExit("east", roomTwo);

        roomFive.setExit("north", entrance);
        roomFive.setExit("south", roomSix);

        roomSix.setExit("west", roomFive);
        roomSix.setExit("east", roomFive);
        roomSix.setExit("south", roomSeven);

        roomSeven.setExit("north", entrance);
        roomSeven.setExit("west", roomSix);
        roomSeven.setExit("east", exit);

        exit.setExit("north", entrance);

        //Add Map
        Item map = new Item("A map of the cave system", "Map", 7);
        entrance.addItem(map);

        //Add Energy
        Item energy = new Item("10 energy. \n" + "Type 'use' to use the energy.", "Energy", 0);
        boolean newEnergy = true;
        roomOne.addItem(energy);
        roomSeven.addItem(energy);

        //Add Bat
        Item bat = new Item("A bat that stole 10 energy from you", "Bat", 0);
        roomTwo.addItem(bat);
        lake.addItem(bat);

        //Add Portal
        Item portal = new Item("A portal", "Portal", 0);
        roomTwo.addItem(portal);


        currentRoom = entrance;  // start game at entrance
    }

The useEnergy() method:

    /**
     * 
     */
    private void useEnergy(Command command)
    {
        if(newEnergy == true)
        {
            currentEnergy = currentEnergy + 10;
        }
        System.out.println("You have: " + currentEnergy);
    }

Line 69 ypu declare a new newEnergy boolean - looks like that may be masking class-level variable of the same name?

Thank you. I fixed it and it works now

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.