Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using methods from an outside class to get a specific variable from that method

Tags:

java

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.

like image 566
Niallty Avatar asked Feb 11 '23 12:02

Niallty


2 Answers

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:

  1. You really want it to be called Enemy rather than Enemies.
  2. You could think about using an enum if you just have two possibilities for an Enemy.
  3. You might also think about having another constructor that takes six parameters, one for each thing you're setting. This could be a private constructor if you like, and that could be invoked by the public constructor that takes the int choice.
like image 50
chiastic-security Avatar answered Feb 14 '23 01:02

chiastic-security


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);
}
like image 38
Eran Avatar answered Feb 14 '23 01:02

Eran