struggling with application of inheritance concepts in simple java programs

If you want to call the same method on both types of players, declare Player as an interface and any methods you want sub-classes to implement… If you want each type of player to have a playerNumber/playerId, have another abstract class PlayerId with the field you want.

Looks to me like you have NPC declaring its own playerNumber (which is already declared in the parent class) Player. This is redundant and can be removed.

Unless you need to “down cast” a Player to NPC/User for a specific method call to be made, you’d typically want to avoid this.

If you can keep methods defined at Player level that apply to all players, you’ll have an easier time implementing and calling the required methods.

e.g.

class PlayerDefinitions {

    public static void main(String[] args) {
        User user = new User(0);
        NPC npcPlayer = new NPC(1, filterBehaviour, selectBehavior);
        
        List<Card> nullList_or_emptyList = user.filter(...);
        Card nullCard = user.select(...);
        
        List<Card> aListOfCards = npcPlayer.filter(...);
        Card aCard = npcPlayer.select(...);
    }
}



interface Player {
    // methods that apply to all players go here

    // You can provide a no-op implementation for User if not required by User
    ArrayList<Card> filter(Hand hand, Hand trick, Whist.Suit trumps);
    Card select(ArrayList<Card> hand, Hand trick, Whist.Suit trumps)
}

abstract class PlayerId {
    private int playerId;

    public PlayerId(int playerId) {
        this.playerId = playerId;
    }
}

class User extends PlayerId implements Player {

    public User(int playerNumber) {
        super(playerNumber);
    }

    public void clickInteraction() {
        // some code
    }

    @Override
    public ArrayList<Card> filter(Hand hand, Hand trick, Whist.Suit trumps) {
        return null;
    }

    @Override
    public Card select(ArrayList<Card> hand, Hand trick, Whist.Suit trumps) {
        return null;
    }
}

class NPC extends PlayerId implements Player {

    private Hand hand;
    private int score;
    private Actor scoreActor;

    IFilterBehaviour filterBehaviour;
    ISelectBehaviour selectBehaviour;

    public NPC(int playerNumber, IFilterBehaviour filterBehaviour, ISelectBehaviour selectBehaviour) {
        super(playerNumber);
        this.filterBehaviour = filterBehaviour;
        this.selectBehaviour = selectBehaviour;
        this.hand = null;
    }

    //strategy pattern filter
    @Override
    public ArrayList<Card> filter(Hand hand, Hand trick, Whist.Suit trumps) {...}

    //strategy pattern select
    @Override
    public Card select(ArrayList<Card> hand, Hand trick, Whist.Suit trumps) {...}
}

I’d also suggest you have a Game class that controls which player’s turn it is rather than trying to get have all players keep track of who the active player is. This separation will keep things clearer.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top