Napoleon Sraf
Napoleon Sraf

Reputation: 67

How to use an int from another class, change it and return changed ints in the original class

I'm making a really simple text turn based game, to make it readable I diceded to separate player's turn and enemy's into different classes, but that coused a lot of problems. Basically code goes like this:

Game itself, without not related stuff.

public class Game {
public static void main(String[] args) {
int hp = 10, enemy_hp = 10;
    while (enemy_hp > 0 && hp > 0) {
        Player.main(args);
        Enemy.main(args);
            }
}
}

Player class:

public class Player{
    public static void main(String[] args) {
    int hp, enemy_hp;
    enemy_hp = enemy_hp - 2;
    hp++;
    }
    }

Enemy class:

    public class Enemy{
        public static void main(String[] args) {
        int hp, enemy_hp;
        hp = hp - 2;
        enemy_hp++;
        }
        }

How do I make Player class take int from Game class, Enemy take int from Player class, and then Game class take result and so on, while the loop is working?

Upvotes: 0

Views: 144

Answers (2)

Rares
Rares

Reputation: 49

I will give you a primitive example of what Chetan Kinger has said.

public class HelloWorld
{
    public static void main(String[] args)
    {
        Player player = new Player(100, 3);
        Enemy enemy = new Enemy(100, 4);
        while(player.isAlive() && enemy.isAlive())
        {
            enemy.takeDamage(player.damage);
            player.takeDamage(enemy.damage);
        }

        if(player.isAlive())
            System.out.println("Player won with " + player.getHp() + " left!");
        else if(enemy.isAlive())
            System.out.println("Enemy won with " + enemy.getHp() + " left!");
        else
            System.out.println("Draw!");

    }
}

public class Player
{
    int hp;
    int damage;

    public Player(int _hp, int _damage)
    {
        hp = _hp;
        damage = _damage;
    }

    public boolean isAlive()
    {
        return (this.hp > 0 ? true : false); 
    }

    public void takeDamage(int damage)
    {
        this.hp -= damage; 
    }

    public int getHp()
    {
       return this.hp; 
    }
}

public class Enemy
{
    int hp;
    int damage;

    public Enemy(int _hp, int _damage)
    {
        hp = _hp;
        damage = _damage;
    }

    public boolean isAlive()
    {
        return (this.hp > 0 ? true : false); 
    }

    public void takeDamage(int damage)
    {
        this.hp -= damage; 
    }

    public int getHp()
    {
       return this.hp; 
    }
}

It is enough for you to get a general idea of what you should be doing from now on.

Upvotes: 1

Chetan Kinger
Chetan Kinger

Reputation: 15212

There is one golden rule when it comes to passing parameters in Java. Java is always pass-by-value. Both primitives and references are passed by value. The reason for sharing this information is that the way your currrent code is structured gives me a feeling that this is the next problem that you are going to run into.

Without spoon feeding you the code, here is a list of changes to consider

  1. Don't use the main method as the interface between classes. main has special meaning. It is supposed to be the entry point of your standalone application. Only one class should ideally have a main method and this should be the Game class in your case.
  2. Add an int hp field to both Player and Enemy classes.
  3. Add mutator methods for the hp field in Player and Enemy. e.g incrementHp, decrementHp, setHp and getHp.
  4. Add a method called startGame in Game class that can be called from main.
  5. Inside the startGame method, create a new instance of Player using Player player = new Player();. Set the hp of the Player to 10 using player.setHp(10);
  6. Similarly, create a new Enemy object in startGame and set it's hp.
  7. Whenever you want to increment the hp of a Player or Enemy, call the incrementHp method. Similarly, call the decrementHp method to decrement hp.

This is where my answer will end. This is where you get the general idea about how to proceed with further refactoring.

Upvotes: 2

Related Questions