Which pattern could I use for a game with complex rules?

I’m creating a 2D turn-based multiplayer (no AI) board game in Java using the MVC pattern in which there are a lot of rules and conditions, I can’t figure out a good pattern to implement them, for example:

  • The player can choose a character from a pool of 12: They have similarities between them but each character could move differently, could use different items, etc.
  • The movement ability, which could already be different based on the character, could change during the game by using items (for just one turn) or after some events (for all the game). For example moving by 3 cells instead of 2.

All these rules could be different based on the selected game-mode which could also change the winning conditions and some game strategies.

Which patterns could be suitable for this? Especially the fact that different characters could have different behaviors depending also on the gamemode and the state of the game.

I know there are already a lot of questions on game patterns on SO but I could not find something suitable to this.

Answer

If I had to design this game, I would try to identify different objects that are hidden in the problem statement as follows :

The player can choose a character from a pool of 12

Hmmm. So I am going to need a Player and Character class.

They have similarities between them but each character could move
differently, could use different items

Ok. So there is behavior that is common to each Character but Characters can also have additional behavior that is different from the common behavior. Let’s add methods in Character for each behavior that is common across characters.

But what about the behavior that is only applicable for a particular type of Character? Ok so I definitely need a way to add behavior to my characters at runtime.

Hmmmm. Let’s create an interface called Behavior which contains a method called.. umm.. behave. Let’s create a couple of classes that implement this interface and implement the behave method.

So far so good. We have a Character class that can be used to instantiate one of 12 characters. We have a Behavior hierarchy for additional behaviors not common across characters. We now need to make these two classes work together in such a way that we can add behavior at runtime. Throughout my career, I have been told to favor composition over inheritance. So I guess I am going to make character have-a Behavior by adding a Behavior reference in Character. There is a name for this pattern.. this is the Strategy pattern.

The most important take-away from the above explanation is that you don’t look at a problem and say what design pattern can I apply?. Instead, you come up with an initial draft of what your design will be like and say this looks like the XYZ pattern

There are lot of requirements that I have not covered in this answer. The idea of this answer is to explain to you the kind of thought process you can put in to come up with an initial draft of your design and keep evolving it rather than over-designing right at the beginning.

Leave a Reply

Your email address will not be published. Required fields are marked *