Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi so I am making a text adventure game and for some reason, nothing happens. I know what the problem is I just do not know how to fix it. I used the debugger in my IDE and it seems like the methods are not finding the health potions called "health". I know they are being added to the inventory, however. I have two classes that are relevant:

I have one called PlayerStats that deals with the health of the player and using the health potions:

package TextAdvenureGame;

import java.util.ArrayList;

class PlayerStats {
    private int maxHealth = 30;
    private int health;
    private int baseDamage;
    private Item equippedWeapon;
    private String location;
    private ArrayList<Item> inventory;

    public PlayerStats(String location) {
        this.baseDamage = 2;
        this.health = maxHealth;
        this.inventory = new ArrayList<Item>();
        this.setLocation(location);
    }
    
//This here sees if the player has any health potions
    public boolean hasHealthPotion() {
        for (Item item : inventory) {
            if (item.getName().equalsIgnoreCase("health")) {
             return true;
            }
           }
		return false;
      }

//This should make the player use the health potion when it is called.
    public void useHealthPotion() {
        boolean hasHealthPotion = false;
        for (Item item : inventory) {
            if (item.getName().equalsIgnoreCase("health")) {
                hasHealthPotion = true;
                System.out.println("Using health potion.");
                health += 5;
                if (health > maxHealth) {
                    health = maxHealth;
                }
                break;
            }
        }
        if (hasHealthPotion == false) {
            System.out.println("You do not have any health potions.");
        }
    }

    public void equipItem(Item item) {
    	    if (item.getDamage() > 0) {
    	        this.equippedWeapon = item;
    	        this.baseDamage = item.getDamage();
    	    } else {
    	        System.out.println("Item is not a weapon.");
    	    }
    	}

    public int getDamage() {
        int totalDamage = this.baseDamage;
        if (this.equippedWeapon != null) {
            totalDamage += this.equippedWeapon.getDamage();
        }
        return totalDamage;
    }
    
    public int getHealth() {
        return health;
    }

    public boolean isAlive() {
        return health > 0;
    }

    public void takeDamage(int damage) {
        health -= damage;
        if (health < 0) {
            health = 0;
        }
    }

    public ArrayList<Item> getInventory() {
        return inventory;
    }

    public void setInventory(ArrayList<Item> inventory) {
        this.inventory = inventory;
    }

    public void addItem(Item item) {
        inventory.add(item);
    }

    public void setLocation(String currentRoom) {
        this.location = currentRoom;
    }

    public String getLocation() {
        return location;
    }
    
    public Item getEquippedWeapon() {
        return equippedWeapon;
    }
}

Here is the part of the CombatTest class that is relevant:

else if (input.equalsIgnoreCase("h") || input.equalsIgnoreCase("health")) {
            if (playerStats.hasHealthPotion()) {
            	playerStats.useHealthPotion();
                System.out.println("You used a health potion and gained 5 health!");
          }
        }


Any ideas as to why it is not finding the "health" would be useful. Thanks for any responses

What I have tried:

I have tried to change the returns, and I have tried to change the methods multiple times, but I was not able to make it work. Most of the time it resulted in an error, this is the best iteration of it. It still does not work, but it does not result in an error.

Any ideas as to why it is not finding the "health" would be useful. Thanks for any responses.
Posted
Comments
Richard MacCutchan 19-Apr-23 10:08am    
You need to use the debugger to examine the content of the object that you are referring to when the input suggests health.
WirePirate 19-Apr-23 10:17am    
How do I do that? I am new to debugging.
Richard MacCutchan 19-Apr-23 11:16am    
It depends on which IDE you are using, as they have their own rules. Use the Help menu to find the guide.
FreedMalloc 19-Apr-23 11:44am    
Is the item in your inventory named "health" or "health potion"? Those 2 strings are not equal. Perhaps you instead need to change your comparison from equals to contains "health".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900