New to Java Programming - Text Based RPG

I just started taking a Java Programming Class at my school, and am definitely not the strongest programming. I’m trying to lay out the ground work for a VERY basic, and linear, text based rpg. I got stuck when I got to the battle sequences… I can’t get the code to produce what I want it too, and am looking for some advice/tips on this stuff. When I type “1” as my answer to attack, nothing happens but the program keeps running. I want to damage the goblin. Thanks!

package videogameenemy;
import java.util.Scanner; 
public class VideoGameEnemy {
	
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int answer = 0;
		
		boolean enemyAlive;
		boolean playerAlive;
		
		Enemy Goblin = new Enemy();
		Goblin.setName("Goblin");
		Goblin.setHp(100);
		Enemy Wolf = new Enemy();
		Wolf.setName("Wolf");
		Wolf.setHp(80);
		
		Player user = new Player();
		user.setDamage(15);
		user.setHp(150);
		
		
		Enemy[] enemies = new Enemy[2];
		enemies[1] = new Enemy();
		enemies[1] = Goblin; 
		enemies[1].getHp();
		enemies[0] = new Enemy();
		enemies[0] = Wolf;
		enemies[0].getHp();
	
		
		
		
		
		
		System.out.println("A " + enemies[1].getName() + " has appeared!");
		
		System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
		System.out.println("Will you attack the " + enemies[1].getName() + "?");
		System.out.println("Enter 1 to attack, or 2 to flee!");
		System.out.println("\t[1 - Attack]");
		System.out.println("\t[2 - Flee]");
		
		answer = input.nextInt();
		
		if(answer == 1)
		{
			
		while(enemies[1].getHp() > 0)
		{
			enemyAlive = true;
			playerAlive = true;
			int d;
			d = enemies[1].getHp() - user.getDamage();
			
			if (enemyAlive = false || playerAlive == false)
			{
				break;
			}
		}
		}
		else if (answer == 2)
		{
			System.out.println("You run and fall.");
		
		}
		
	}
}






/////////
class Enemy
{
	private String name;
	private int Hp;
	
	public Enemy()
	// A Enemy is created with name set to an empty string
	{
		
	}
	
	public Enemy(String name, int Hp)
	{
		name = "";
		Hp = 0;
	}
	
	
	
	public void setName(String newName)
	// Pre:  Assigned(name)
	// Post: class member name has been set to name
	{
		name = newName;
	}
	
	
	public String getName()
	// Post: Returns the name
	{
		return name;
	}
	
	public void setHp(int newHp)
	// Pre:  Assigned(hp amount)
	// Post: class member hp has been set to newHp
	{
		Hp = newHp;
	}
	
	public int getHp()
	{
		return Hp;
	}
	

	
	
}

class Player extends Enemy
{
	private int damage;
	private int hp;
	
	public Player()
	// Default constructor
	{
		damage = 0;
		hp = 0;
		
	}
	
	public void setDamage(int newDamage)
	{
		damage = newDamage;
	}
	
	public int getDamage()
	{
		return damage;
	}
	
	public void setHp(int newHp)
	{
		hp = newHp;	
	}
	
	
	public int getHp()
	{
		return hp;
	}
	
}


You never update the enemy Hp. You have this:

int d;
d = enemies[1].getHp() - user.getDamage();

Which does calculate the new Hp, but you never assign it to the enemy. You’ll need something like

enemies[1].setHp(d);

Whats up with this on line 28 (i think)

enemies[1].getHp();

Is this a get method that does not actually return a value? I would assume that it is being used wrong here.

That makes sense. So I would do that in the while loop, correct? Like this:

while(enemies[1].getHp() > 0)
		{
			enemyAlive = true;
			playerAlive = true;
			int d;
			d = enemies[1].getHp() - user.getDamage();
			enemies[1].setHp(d);

Will enemies[1].getHp() in the loop declaration get the new Hp value then (which is d now) ? And also I think something may be wrong with my if statement because when I type 1, nothing happens and the program now stops.

@wreed12345

Now that I look at it, I don’t think it was necessary at all, but I also don’t think it harms anything?

Just doing

enemies[1].getHp();

doesn’t do anything. It returns the hp, so you have to do something with that value, like

hp = enemies[1].getHp();

Also, don’t use a while, use an if - and instead of setting enemyAlive and playerAlive to true over and over again, just detect if the enemies[1].getHp() is <0 and then set them to false.

Yes. That’s what you should do. Your program stops because you put this code in a while loop. So, it’s going to keep reducing the enemy health until it is below 0, and then the program is done. If you put a System.out.println() right after the while loop, you will see this.

As Jimmt said, use an if statement instead of a while statement. Even then, though, your program will stop after you do the input.

I understand the enemies[1].getHp(); now (I think). Thanks for clearing that up.

EDIT: Just saw your post. Let me try that.

Ok so it worked (kinda). I have this now:

answer = input.nextInt();
		
		if(answer == 1)
		{
			
			if(enemies[1].getHp() >= 0)
			{
			enemyAlive = true;
			playerAlive = true;
			int d;
			d = enemies[1].getHp() - user.getDamage();
			enemies[1].setHp(d);
			
			System.out.println(enemies[1].getName() +  " has " + enemies[1].getHp() + " HP remaining.");
				if (enemies[1].getHp() <= 0)
				{
					System.out.println("enemy died");
				}
			}
		}
		
		else if (answer == 2)
		{
			System.out.println("You try to run, but fall into a pit of spikes and die!");
		
		}

It does the math and prints out
“The Goblin has 85 HP remaining.”

So now I have 2 questions. Now if I prompt the user again if they want to attack again or not and they choose to attack, do I need to put all that code (pretty much same as above) in again for the next attack?

And also, what if I wanted the entire fight to happen right there? Like my character keeps attacking the goblin.

Make it a while loop again, and check for input inside the while loop. IF they select 2, just use break;

It worked! Thank you! But I still have some questions… Say I wanted to choose if I want to attack again (or possiblty use a spell attack, etc)… Would I use If’s then, and would I need to continually copy and paste that battle sequence (with some minor changes) or could I make a method that I could call?

The reason i suggested an if statement is because i thought you had a game loop - you really should read up on them.

I will do that right now! Thank you.