views:

78

answers:

2

I originally asked this question on programmers.stackexchange.com, because I figured the answers would be too subjective for stackoverflow. However, in the comments Muad'Dib pointed this out:

this would probably be a better fit for StackOverflow ... This is a direct programming problem-related question and discusses programming solutions. One solution may be equal to another where the choice between them is subjective, but the discussion of the overall problem itself is not as some solutions are clearly not applicable. So this question does have one or a few good answers, whereas questions such as "Which is the best text editor?" does not...

So, after pondering the situation a little more (and looking at a few patterns that don't seem to fit), here is my dilemma:

The Set Up

I'm designing a simple combat strategy game.

  1. Two combatants attack each other for a certain amount of time.
  2. The winner is the one with the most health after the time is up.
  3. There is no direct intervention by the players during the fight. Players define some strategies for their combatant before the fight, and the "AI" (term used loosely) determines what the combatants will do.
  4. The fight consists of an arbitrary timer that "ticks" seconds, and a combatants' attack speed (among other things) determines how often they do something.
  5. Besides the combatant objects, there is also a "weather" object that can have some random environmental effects on the combatant's abilities.
  6. The whole thing just loops through each second, environmental effects and attacks are determined and executed, it's all logged, and the log is displayed to the user afterward as a play-by-play.

The Problem

I'm not sure what design pattern would best allow me to accomplish the above. I'm looking for a pattern that will allow me to increase the complexity of the "fight" down the road. Probably by adding more objects to the fight (more combatants, maybe a new environment object, etc.)

At first I thought the Observer pattern would work. I would have a fight object that represents the "observee/subject". It tracks a timer, and as the timer increments, it updates all of the participant observer objects. However, a lot of a defender combatants' parameters can affect actions of an attacker combatant, so the "observers" in that scenario need to be aware of each other, so I'm not sure if it's the right approach.

Finally, I'm not trying to nail a design pattern perfectly. Maybe there is no "best fit." I'm just looking for a solid base to build upon.

+2  A: 

Trying to find a single 'right' design pattern for a fairly detailed situation is a fruitless search. Design patterns address certain common low-level situations that arise in a specific programming language or languages - there isn't necessarily a specific one existing for every possible situation you might find, nor are you necessarily only going to use a single one to solve your problem.

In particular this situation is not really what design patterns are there for - design patterns primarily concern the static relationship between classes and objects, whereas what you are talking about here is inherently algorithmic in nature. Design patterns provide ways in which you can implement algorithms, but aren't really algorithms in themselves.

This should be quite straightforward. You have a while loop that runs for the specified duration. Each iteration represents one second of elapsed time, and in that iteration it would call a method or methods on every relevant object involved in the fight - the combatants, the weather, etc. (How many methods and which ones is a game design question and not (yet) a programming question.) These objects can come from a fight object that holds the entire context if you like.

There is no real need for the observer pattern here, and in fact it will seriously complicate the matter by changing it from an easily understandable iterative process to an event-driven one. A combatant can query another combatant directly if access is required to various properties.

Kylotan
+1 for 'Trying to find the 'right' design pattern is a fruitless search.'
willcodejavaforfood
I disagree. I think it's a good idea to see if there is a design pattern to fit your problem. If there is then great, use it. If there isnt don't try and squish your problem into a pattern for the sake of it.
dubbeat
I agree that sometimes there is a pattern for the problem in hand. What I was disagreeing with was the assumption in the question that every problem has 'a' pattern for it, when design patterns are not there to address absolutely every issue that might arise when making software. I'll try and edit this answer to express this more clearly.
Kylotan
Thanks for answering my question. However, to what end have you censured the pursuit of design patterns? Did you read the last sentence of my inquiry? Where is the "assumption .. that every problem has 'a' pattern"? It is *because* I have not mastered patterns that I feel compelled to seek one. Should I not discover whether my "low-level situation" has been succinctly solved by a pattern before implementing my own solution? Your advice is needlessly anti-establishment--or you didn't fully read my question. Either way your self-aggrandizement seems aimed at stifling self-education.
Stephen
Stephen, you are misunderstanding the purpose of design patterns. They don't exist to solve application design problems or algorithmic problems like this. They exist to overcome specific low-level issues with expressing certain low-level concepts in programming languages. I am not anti-design-patterns at all - it's just that the problem they solve is not the problem you have.
Kylotan
And this is not a 'low level situation' you have. It's a fairly straightforward algorithm. There is no real way you can meaningfully combine 'design-patterns' with 'language-agnostic'. The patterns only exist because of the restrictions of languages - not because of the needs of the application.
Kylotan
Oh yeah? Here's the strategy pattern in **15** different languages. http://en.wikipedia.org/wiki/Strategy_pattern That agnostic enough for you?
Stephen
Some patterns apply to multiple languages. Others only apply to one or two. The Strategy pattern is largely pointless in languages with first-class functions, for example. There is no pattern for arbitrary algorithms like yours - the patterns potentially apply to your eventual chosen implementation, and it depends on the language you use.
Kylotan
Well, thank you for the stolid response to my badgering.
Stephen
+1  A: 

If you really wanted to use a pattern here I would consider a command pattern. For example you can just que up a string of commands "fight moves" to execute over a period a time. You would also be able to easily cancel commands based on what the opponent is doing quite easily and generically. You can easilly add to moves/ strategies to this as your develop your game down the road.

But this isnt a pattern for the entire game, it's more a solution for one aspect of it.

dubbeat
Eventually I used a combination of a few patterns, but for the main task at hand, I implemented a Command pattern.
Stephen