Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Design Pattern

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm looking for a smart implementation of cards in a card game java implemented. Cards differ in the effect; some has one effect others two. There are a few different type of effect. I was thinking about a pattern (factory, decorator) to use with a Parser (cards effects are parsed from xml file).
 
Saloon Keeper
Posts: 13294
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

What do the effects affect? Depending on the answer, you might want to look into the Visitor pattern.
 
Matteo Rai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are different effects that affect or the board pawn or the winning condition
 
Marshal
Posts: 74085
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be more specific; we need details to be able to help.
 
Matteo Rai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are effects that can be used:
- in player turn
- in opponents turn
One effect affects build
One effect affects win condition
 
Stephan van Hulst
Saloon Keeper
Posts: 13294
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, assume you have a game model that consists of Game, Board, Pawn, Pawn.Color, Player, WinCondition, Card and Effect. A possible Effect type could be "card player switches two target pawns". It is implemented by asking the player who used the card to pick two pawns and then it switches the location of the two pawns. You can do this by giving the effect access to the entire game state. A card then simply consists of a list of effects. You can load a card from XML by referencing an identifier that the effect is known by inside the code.

It gets hairy if the effect has to interact with subclasses of those that I listed above. For instance, how do you make a subclass of Effect interact with a subclass of WinCondition? Here's where you can use the Visitor pattern.

You might also want to add new effects in "add-ons". You achieve this by adding a service provider interface (SPI) to your project, such as EffectProvider. In a plugin you implement EffectProvider and have it return new subtypes of the Effect interface.

What the best approach is depends strongly on your requirements. You need to tell us more if you want more help.
reply
    Bookmark Topic Watch Topic
  • New Topic