I'm writing a wee text based questing game in Java to get the hang of the language a bit better.
I'm trying to create a method that can be called from another class. The method I want to call contains repeated variables and I want the correct variable to show up when I use a switch statement.
public class Enemies {
String enemyName = "Generic Enemy";//initialised the enemy stats so something is called if the switch failed
double enemyHealth = 110.0;
int enemyAttackDamage = 10;
int enemyDefenseLevel = 1;
int hardLevel = 1;
int lootDropped = 50;
public void enemyDecided(int choice){
switch(choice)
{
case 1:
this.enemyName = "Sentient Shoelace";
this.enemyHealth = 100.0;
this.enemyAttackDamage = 1;
this.enemyDefenseLevel = 1;
this.hardLevel = 2;
this.lootDropped = 100;
break;
case 2:
this.enemyName = "Slimey Fartbox";
this.enemyHealth = 100.0;
this.enemyAttackDamage = 2;
this.enemyDefenseLevel = 1;
this.hardLevel = 3;
this.lootDropped = 100;
break;
default: System.out.println("Mess up in the enemy selection process");
}
}
}
I'm trying to call this from another class by
private void fight()
{
//I'm using a rand.nextInt() to get enemychoice
Enemies enemyToFight = new Enemies();
enemy = enemyToFight.enemyDecided(enemyChoice);//
System.out.println("You run into a " + enemy.enemyName);
}
My fight() method doesn't work at all. And for good reason. What I have wrote is pure nonsense, though I'm at my wits' end and the edge of my meager knowledge.
I'm sure anyone with eyes can see that I'm trying to build a pseudo struct. As a recent convert from C, I'm still learning how to do things better. If this is all a terribly misguided idea to do in Java then could you point me in the right direction. Though a chance to save it would make me very happy indeed.
But I would like to try and get this working right if it's possible, so any help is appreciated.
It looks like what you really want is a constructor for your Enemies class, rather than a void method:
public Enemies(int choice){
switch(choice)
{
case 1:
this.enemyName = "Sentient Shoelace";
this.enemyHealth = 100.0;
this.enemyAttackDamage = 1;
this.enemyDefenseLevel = 1;
this.hardLevel = 2;
this.lootDropped = 100;
break;
case 2:
this.enemyName = "Slimey Fartbox";
this.enemyHealth = 100.0;
this.enemyAttackDamage = 2;
this.enemyDefenseLevel = 1;
this.hardLevel = 3;
this.lootDropped = 100;
break;
default: System.out.println("Mess up in the enemy selection process");
}
}
Now you can change your fight() method as follows:
private void fight()
{
//I'm using a rand.nextInt() to get enemychoice
Enemies enemy = new Enemies(enemyChoice);
System.out.println("You run into a " + enemy.enemyName);
}
There are still some stylistic issues you might like to ponder on:
Enemy rather than Enemies.enum if you just have two possibilities for an Enemy.private constructor if you like, and that could be invoked by the public constructor that takes the int choice.This is a very weird implementation, and very un-object oriented.
You should have an Enemy class with the members that are currently in the Enemies class.
Your Enemies class should contain a List<Enemy> variable, which contains an instance for each Enemy.
Then your enemyDecided method would be as simple as :
public Enemy enemyDecided(int choice)
{
return enemies.get(choice);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With