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);
}
public boolean hasHealthPotion() {
for (Item item : inventory) {
if (item.getName().equalsIgnoreCase("health")) {
return true;
}
}
return false;
}
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.