vendredi 2 juin 2017

Command pattern: commands requires user input

Good morning, I'm creating a board game in Java. In this game the user can do several different Actions, one of them is pick a card from the board. I'm using the Command pattern with this interface in order to write these actions:

void execute();
boolean isAllowed();
int getActionValue();
void setActionValue(int newValue);

In the game status class, I have a List of actions that need to be executed (there might be actions mandatorily following the previous one)

My problem is that if the a card allows the player to pay in more than one way, I have to ask the user which way he wants to pay. My idea was doing something like:

PickCardAction:

List<Requirement> satisfiedReq = new LinkedList<>();
    for(Requirement req : card.getRequirements())
    {
        if(req.isSatisfied(player.getResources()))
            satisfiedReq.add(req);
    }
    if (satisfiedReq.size() == 1){
       //pick card
    }
    else if (satisfiedReq.size() > 1){
       //notify the view and conclude the action
    }

Once I got the answer of the user I should set it as an optional parameter and start again the action:

if (this.optionalRequirement != null){
    //apply the requirement and give the card
}
else{
    //like above
}

But I don't like this idea very much (also because I would need an explicit cast - from Action to PickCardAction - in order to set the optionalRequirement). Do you have any better idea? PS: In the future I will need to be able to link an effect of the user (that changes the actionValue) to a specific action, so, maybe, this is not the best way of handling the Actions

Aucun commentaire:

Enregistrer un commentaire