4

I'm writing the core logic of Pokemon Gen I in Java (8), and I am struggling to figure out the best way to implement the turn-based aspect of the game. Right now I have a Battle class which has two Players each with a PokemonTeam.

The idea is that one Player takes a Turn (a Battle has many Turns), and some action happens, e.g. attack with a Pokemon, use an Item, switch Pokemon, etc. Then, once that single action is taken, it needs to be applied during that turn, and then the other player gets to take their turn.

My original design, which has changed very slightly, looks something like this:

Design

I was planning on implementing an Action and ActionResult superclass, with subclasses for each of the action types (e.g. AttackAction and AttackActionResult). But that seems like it would be a bit confusing to use, and responsibility of applying damage, effects, handling Pokemon fainting, etc. all gets stuffed into those subclasses, which seems like a bad idea.

Then I stumbled across the command pattern which seems to fit the bill - "a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time". Assuming good use of the pattern, I could have good delegation of applying actions, handling results, updating the battle state, etc. without turning classes into God classes.

Is the command pattern a good fit for a turn-based game like Pokemon, or is there something more appropriate?

  • What would be the main differences between your Action superclass and a command superclass (except the name, of course)? – Doc Brown Oct 2 '16 at 20:16
  • @DocBrown naming, I suppose. It seems that my initial Action type is pretty much a Command, as you mentioned - so really, my initial design was a sort-of command pattern. At first glance, the command pattern seemed really different to what I had, but I suppose that the naming threw me off. Would you recommend such a pattern for this kind of task? It seems to fit well, at least in my opinion, but I'm no expert! – Chris Cirefice Oct 2 '16 at 20:20
2

Your initial plan using an Action class looks exactly like the command pattern to me, so if you think this is what you need here, you should try it out.

But that seems like it would be a bit confusing to use,

Why? Having just one public apply method for an action looks like a pretty simple usage scenario to me.

responsibility of applying damage, effects, handling Pokemon fainting, etc. all gets stuffed into those subclasses

Surely not. There is no need to put all that into a single Action subclass. If an Action has lots of things to do, it can delegate that stuff to dozens of other classes, if that is what is required. That way, a Action subclass can also be a facade.

Is the command pattern a good fit for a turn-based game like Pokemon

It might be a good fit for your implementation of the action part of the turns in such a game. But honestly, I can not tell you if it will suit your needs, you need try it out by yourself, see if it works, and if you cannot come up with a simpler solution, then it is probable a good one.

  • Thanks for your input Doc! I ended up going the route of the command pattern and it worked out very well! My implementation is extremely basic, at least so far, so there was no simpler solution available. – Chris Cirefice Oct 3 '16 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.