mardi 10 mars 2015

Design of model of player in a simple game with computer and human players

Let's say I have a simple game, in which player can choose object X. Human can obviously do it via gui, and computer via some strategy. They both share same model class, Player:



public class Player {
private String name;
private X x;

public Player(name, x) {
...
}
}


Notice no setters, which means that every time they make a choice I have to create a new object of Player, with same name but different X.


Now, this strategy is an interface,



public interface PlayerXChoiceStrategy {
public X getPlayersChoice();
}


and there are for example two classes implementing this, HumanXChoiceStrategy and SomeBotXChoiceStrategy. I don't want to have these strategies in model and I don't want to use something like Map<Player, PlayerXChoiceStrategy> because Player is immutable, so that would create some problems. How can I possibly retrieve the strategy in a fast manner and not rely on model/map? I obviously use these strategies in a lets say a Controller class where I call every players getPlayersChoice().


My only idea is to have a Map<String name, PlayerXChoiceStrategy> and I'll probably implement unless someone comes up with a better answer.


Aucun commentaire:

Enregistrer un commentaire