tags:

views:

62

answers:

3

I have a question regarding the use of the MVC framework for a blackjack game im writing in java. As I understand, my model would be a class that stores the state of the game, and more specifically, each players hand. Within the model class, I would have an array of listeners, which would be notified each time the game state changes (ie a player has drawn a new card).

These listeners would be my viewer, which is the class that handles the display of the game. This class would implement the ActionListener interface, and each time the model changes, it would call/"notify" my viewer's actionPerformed method.

My question is as follows: I intend to have two JPanels, each devoted to displaying the respective player's hand. As a player draws a new card, a new ImageIcon would be added to the panel. My question is how would the viewer class know what card has been added to the player's hand?

I suppose I could store the player's hand before a notify event, and then upon the notification event, compare the new state with the old, to find out what has changed. I'm a complete novice here, so I could be completely wrong, but it seems a bit tedious to do this.

Is there a more efficient/common way of detecting what has changed in the model between the before and after state? Another solution would be to redraw the entire panel each time the notification occurs? Would this be a better idea?

A: 

As a player draws a new card, a new ImageIcon would be added to the panel. My question is how would the viewer class know what card has been added to the player's hand?

Well generally, you could have a BlackJackListener interface that has a method called something like notifyNewCard(player, card). (Eg, you don't have to go through ActionListener, you could have something more ellaborate.)

Another solution would be to redraw the entire panel each time the notification occurs? Would this be a better idea?

The solution I would go for in this case however, would simply be to re-read the whole state of the BlackJack model as you suggest above. The state will be really simply, so there is no "performance impact" to talk about, and you don't run the risk of getting "out of sync" with the model.

In such solution you would only need to use ChangeListener and say, "something happened, update your view of me".

aioobe
A: 

You can pass Event Object to model listeners that have details about what is happened (ADD, REMOVE, REPLACE or complex CHANGE) and information about new and old value(s). One example of this approach in JDK is javax.swing.event.ListDataEvent, javax.swing.event.ListDataListener and javax.swig.ListModel.

Ha
+1  A: 

ActionListener is rather devoted to button behavior. In your use case, I suggest you create your own listener class, which carries a custom event containing the kind of event and values. Typically, the event kind could be 'new card', 'new hand', and the values could be the player id and the card value.

For a clean MVC implementation, your panels should get all the event information they need from the event object. The more independent it is from a global context, the more reusable and robust it is.

kabado