P
pek
I am currently developing the House Of Cards card game (I can't seem
to find any relative info on the internet), but it doesn't really
matter. My problem is a design problem.
Say you have a class that holds a set of cards. When a card is added
it has to obey certain rules. For example, if it is full and no other
cards can be added, an exception should be thrown. If the card is not
the correct suite, again, an exception should be thrown etc.
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. So I thought that I
could create an interface called Rule with a method called addCard.
The class holds a list of rules and every time a card is added it
calls them in the added order.
But, in order for this to work, I found two problems:
A) the class that implements the Rule has to have a lot of information
(the list of cards, if it is already closed etc.). How do I work
around this? Should the method of the interface have a list of the
needed information in parameters? Should the class pass itself in all
rules and provide getters for all attributes?
B) because different rules have different reactions, I also create a
Listener for that class. Every time something important happens, the
registered listeners get informed. But I have no idea how the class
that implements the Rule can inform the other class to fire an event
to the registered listeners.
Just in case I'm not clear at all, let me explain a little more.
The class I am talking about is called House. This holds a set of
cards. A card has points depending on it's suite. The house can hold
up to 31 points. When a card is added and the sum of the points are
EXACTLY 31, the player wins 20 points in his total score and the house
empties so cards can be added once again. If the sum of points exceed
31, the house closes and no more cards can be added. The game has four
houses.
Those are the standard rules. Additionally:
a) if a house has a total of six cards (not so common), the player
wins 50 points in his total score and the house empties.
b) each house is of a particular suite. If a joker of the same suite
(the jokers have suites in this game) is added, any card in the house
are erased and the player wins 50 points in his total score. But if it
doesn't the game is over.
Now, the rule with the six cards fires an event to the listeners that
the rule was satisfied. One of the listeners is the GUI and it reacts
by displaying information. The same thing goes for the 31 points rule
and any rule that others need to know about.
These are events that are fired and listeners react to. But these also
are the rules for the house.
So everything comes down to design decision. Because the rules vary,
and could be expanded, they need to be outside of the House. And
because the events depend on the rules, there has to be a bridge
between them.
Any suggestions?
Thank you very much in advance.
Regards,
P. Peikidis
to find any relative info on the internet), but it doesn't really
matter. My problem is a design problem.
Say you have a class that holds a set of cards. When a card is added
it has to obey certain rules. For example, if it is full and no other
cards can be added, an exception should be thrown. If the card is not
the correct suite, again, an exception should be thrown etc.
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. So I thought that I
could create an interface called Rule with a method called addCard.
The class holds a list of rules and every time a card is added it
calls them in the added order.
But, in order for this to work, I found two problems:
A) the class that implements the Rule has to have a lot of information
(the list of cards, if it is already closed etc.). How do I work
around this? Should the method of the interface have a list of the
needed information in parameters? Should the class pass itself in all
rules and provide getters for all attributes?
B) because different rules have different reactions, I also create a
Listener for that class. Every time something important happens, the
registered listeners get informed. But I have no idea how the class
that implements the Rule can inform the other class to fire an event
to the registered listeners.
Just in case I'm not clear at all, let me explain a little more.
The class I am talking about is called House. This holds a set of
cards. A card has points depending on it's suite. The house can hold
up to 31 points. When a card is added and the sum of the points are
EXACTLY 31, the player wins 20 points in his total score and the house
empties so cards can be added once again. If the sum of points exceed
31, the house closes and no more cards can be added. The game has four
houses.
Those are the standard rules. Additionally:
a) if a house has a total of six cards (not so common), the player
wins 50 points in his total score and the house empties.
b) each house is of a particular suite. If a joker of the same suite
(the jokers have suites in this game) is added, any card in the house
are erased and the player wins 50 points in his total score. But if it
doesn't the game is over.
Now, the rule with the six cards fires an event to the listeners that
the rule was satisfied. One of the listeners is the GUI and it reacts
by displaying information. The same thing goes for the 31 points rule
and any rule that others need to know about.
These are events that are fired and listeners react to. But these also
are the rules for the house.
So everything comes down to design decision. Because the rules vary,
and could be expanded, they need to be outside of the House. And
because the events depend on the rules, there has to be a bridge
between them.
Any suggestions?
Thank you very much in advance.
Regards,
P. Peikidis