-2

I am writing a code for a text-based adventure (Java), it's called the World of Zuul, in school, and I have to implement a health state for the player. I made an interface class:

public interface PlayerState {
    public void heal();
    public void injure(int damage);
    public String getState();
}

I also made three classes:

public class ImmobileState implements PlayerState{
    public void ImmobileState(){}
    public void heal(){
        System.out.println("Player is now healthy");
            Player.setState(new HealthyState());

    };
    public void injure(int damage){
        System.out.println("You was already immobile, so nothing happened?");
        Player.setState();
    };
    public String getState(){return "Immobile";};
}
public class InjuredState implements PlayerState{
    public void InjuredState(){}
    public void heal(){
        System.out.println("You are now healthy");
        Player.setState(new HealthyState());
    }
    public void injure(int damage) {
        if (damage == 0) {
            System.out.println("Nothing happened...");
            Player.setState(new HealthyState());
        } else {
            System.out.println("You are now immobile");
            Player.setState(new ImmobileState());
        }
    }

    public String getState(){return "Injured";}
}
public class HealthyState implements PlayerState{
    public void HealthyState(){}
    public void heal(){};
    public void injure(int damage){
        if(damage <= 10 && damage > 0) {
            System.out.println("You are now injured");
            Player.setState(new InjuredState());
        }else if(damage == 0){
            System.out.println("Nothing happened...");
            Player.setState(new HealthyState());
        }
        else {
            System.out.println("You are now immobile");
            Player.setState(new ImmobileState());
        }
    };
    public String getState(){return "Healthy";};
}

the problem here is the setState method in the Player-class:

public class Player {
   
    private PlayerState state;

    public Player(){
    }

    public void setState(PlayerState state) {
        this.state = state;
    }
    public void heal(){
        state.heal();
    }
    public void injure(int damage){
        state.injure(damage);
    }
    
    public String showStatus(){
        StringBuilder longStatus = new StringBuilder("> Status of the Player");
        longStatus.append("\nhealtj state: "+ state.getState());
        String fullStatus = longStatus.toString();
        return fullStatus;
    }
}

This is not the full player code, but the important parts. If i try the code, there comes this error:

Non-static method 'setState(model.states.PlayerState)' cannot be referenced from a static context

It comes three times, once for every state.

I hope this wasn't too much or to less to explain my problem. Hopefully someone can help me.

ChatGpt suggested me to do that:

public Player(){
    Player player = new Player();
    player.setState(new PlayerState);
}

This makes no sense because I declare the Player object already in my Game-class.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Dylan
  • 1
  • why dont you just implement PlayerState in the player class?like this Player implements PlayerState ? – Anon Jan 27 '23 at 16:25
  • or use the classes the implements PlayerState like HealthyState class , etc – Anon Jan 27 '23 at 16:27
  • The error is explicit. You cannot call `setState(...)` (non static) from a static context (somewhere static like in a main methods). With other words, you must call `setState(...)` from a instance of `Player`. So to call your method, you must do something like: `Player player = new Player(); player.setState()`. Or call `setState(...)` from within the `Player` class (somewhere non-static of course). – Mohicane Jan 27 '23 at 16:29
  • This, including the "non-static variable" version, is a common question on Stack Overflow. See for example, https://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static . If you want to search for more, try https://stackoverflow.com/search?q=%5Bjava%5D+non-static+method+cannot+be+called+from+a+static+context . – Old Dog Programmer Jan 27 '23 at 16:48

1 Answers1

0

Thank you all.

The solution is to implement the player in the states. Like this:

public class ImmobileState implements PlayerState{
    Player player;
    public void ImmobileState(){}
    public void heal(){
        System.out.println("Player is now healthy");
            player.setState(new HealthyState());

    };
    public void injure(int damage){
        System.out.println("You was already immobile, so nothing happened?");
        player.setState(new ImmobileState());
    };
    public String getState(){return "Immobile";};
}

so you can take the current player. Important here is not to declare a new Player because this would interfere the current player

Dylan
  • 1