Search code examples
design-patternsstrategy-pattern

Stategy pattern for game


Hi I should use strategy pattern to write code of this game. I create Country class and Character Interface. But i'm little confused i don't know where should i add abilities?

player first has to select a country for his country, which could be: Latveria, Ecotopia,Dinotopia. After selecting the country, player has to assign a class for his/her character by selecting one of the following: King,Queen,Troll and Knight. Depending on its class a character can have different abilities: a Knight can handle any
kind of weapon easily, which includesBow and Arrow, Swords, Knifes and Axes. King doesn't use any weapon, he can make strategy. Queen uses speacial Magic. Troll uses Knifes and Axes. All types of characters can fight using its specific abilities. Besides, all can ride horse and swim.


Solution

  • Looking at the diagram you gave I would do this differently. Firstly you don't need a Country class instead just have it as a list and have your Character class have a Country Property which gets is value from this list depending on what the user picked.

    Create a character class which contains all methods and properties which are the same for any character.

    Now create the four different character types as classes containing all the attributes relative to that type, these four classes will inherit from the Character class.

    When the user creates a new character you create a new CharacterType instance and pass through the constructor the country and class type and then proceed to create the Character type as per data given then continue to set the character up as you need.

    Something like this should work ok