Re: Design decision for a game
On May 23, 3:40 am, Tom Anderson <t...@urchin.earth.li> wrote:
On Thu, 22 May 2008, pek wrote:
On May 21, 4:40 am, Tom Anderson <t...@urchin.earth.li> wrote:
On Tue, 20 May 2008, pek wrote:
If the card is not the correct suite
Tiny comment: the word is 'suit', like a set of clothes, not 'suite'.
I'm guessing english is not your first language - although this is the
only error i noticed in your post!
Thank you for pointing it out so politely. ;) I'm from Greece, so
indeed, my first language isn't English.
Aha. I thought someone whose english was as good as yours would appreciate
an opportunity to improve it even further, even if only in a small way!
Thank you (again). ;) Well, what really annoys is when people take for
granted that English is the only language on earth and if you make a
mistake your retarded. That's why I liked it when you said "I'm
guessing english is not your first language". ;)
Now, depending on the options of the game, some rules apply and some
don't. So clearly, the rules aren't inside the class itself, but they
are added by the engine using an addRule method.
Okay. Firstly, you have two different kinds of rules here. The first kind
is a permissibility rule: it says whether a card can be added to a house
or not. The second kind is an action rule: it says that when a certain
situation comes to pass, something should happen.
I wouldn't try and handle both kinds of rule within one framework myself.
I'd deal with permissibility and action rules separately.
Hmmm.. Good idea! I'll do that! So by now I have two types of
interfaces: PermissibilityHouseRule and ActionHouseRule. Stupid
question: Are they any way inherited by a HouseRule interface? Although
I can't find any reason why, I feel it could be.
Trust your feelings! It doesn't do any harm to inherit them from a common
interface, and it helps to document the code.
Also, both kinds of rules will need a getName() or getDescription()
method, so you can declare that in the root interface.
Great idea! ;)
(Incidentally, are people familiar with the term 'reify'? It's an old OO
term, but one that seems not to be used much these days - it simply means
to make something into an object.)
Nope. Never heard of! Nice to know though! Thanks for sharing. ;)
No problem. I'm sorry it's from a latin root, and not greek!
This whole implementation sounds like a great and easy idea. But I
have one question. What if an ActionHouseRule ends the game? Like in
the case of the Joker:
If a Joker of Spades is enter in a House of Hearts, the game is over.
If a Joker of Spades is added to a House of Spades, then the cards in
the house are emptied and the player gains points.
This isn't a permissibility rule, and there is case where the game is
over. So how do I implement the game over?
I'd add a method to the ActionRule interface through which a rule can
indicate it ends the game. This is after applying the refactoring i
mention, so the interface looks like:
interface ActionRule extends Rule {
public String description() ;
public boolean endsGame() ;
public int score() ;
public boolean applies(House h) ;
}
With the addCard looking like:
public List<Rule> addCard(Card card) {
// do permissibility checks here - i omit them
List<ActionRule> applied = new ArrayList<ActionRule> ;
for (Rule rule: rules) {
if (rule.apply(this)) applied.add(rule) ;
}
return applied ;
}
And the game controller having code like:
List<Rule> applied = house.addCard(card) ;
for (Rule rule : applied) {
if (rule.endsGame()) {
tellUser("GAME OVER: " + rule.description() + "!") ;
gameOver = true ;
}
else
tellUser("rule applied: " + rule.description() + "; scored " + rule.score()) ;
score += rule.score() ;
}
tellUser("total score: " + score) ;
}
That sounds really good! I'll definitely try this.
But if there's a permissibility rule that says you can't put a card in a
house whose suit doesn't match, how can you get to the situation where you
put a joker of spades in a house of hearts?
First, the rule that looks for wrong suit in a house can first look if
it is a joker card and permit it to be added.
Then the Rule would look something like this:
class JokerRule implements ActionRule {
private boolean gameOver = false;
private int score = 0;
public String description() { return "Joker Rule"; }
public boolean endsGame() { return gameOver; }
public int score() { return score; }
public boolean applies(House h) {
Card tmp = house.getCards().get( house.getCards().size() -
1 );
if ( tmp.getRank() == CardRank.JOKER ) {
if ( tmp.getSuit() == house.getSuit() )
house.emptyCards();
this.score = 50;
} else {
this.gameOver = true;
}
}
}
What do you think?
tom
--
:-( bad :-) bad :-| good