Reputation: 27
I'm working on a home assignment and I hope someone can give me a hint or two, because I'm stuck. I have a BlackJack game in Java which uses Model-View-Controller structure and I need to expand this program. I have to implement the observer pattern inside this program.
I hava a player class and a dealer class (the dealer extends the player) in the Model. And I have a view class which holds all code that is needed for UI. The Player class is implementing Observerable interface, and the View class is implementing the Observer interface. I also have a controller which is called PlayGame. So the View class is the observer.
My problem is: when I run the program, the program shows me only the cards of the dealer... not the player. Or sometimes it's conversely - the program shows the player's card, but not the dealer's cards... So what should I do in order to make my program "see" both the hand of the dealer and the hand of the player? I would be thankful for any help or hint on how to solve this...
I know how Observer pattern works, but for some reason I don't know how to make it "observe" two objects of the same class. I need to make it observe two different objects of the Player class (I'm counting the dealer and the player as the same type, because the dealer is extending the player class).
Here's the code:
The Player class:
public class Player implements Observable{
private List<Card> m_hand;
private ArrayList<Observer> handOfCards;
public Player()
{
m_hand = new LinkedList<Card>();
handOfCards = new ArrayList<Observer>();
}
public void DealCard(Card a_addToHand)
{
m_hand.add(a_addToHand);
notifyObservers();
}
public void addObserver(Observer o) {
handOfCards.add(o);
}
public void notifyObservers() {
for(Observer o : handOfCards){
o.update(m_hand, this);
}
}
The Dealer Class extends the Player class, and it has some extra methods for hitting, standing, etc.
In the view class I have this update method:
private List<Card> playerHand = new LinkedList<Card>();
public void update(List<Card> m_hand) {
playerHand = m_hand;
}
In the controller, I have this:
a_view.DisplayWelcomeMessage();
a_game.getPlayer().addObserver(a_view);
a_game.getDealer().addObserver(a_view);
a_view.DisplayDealerHand(a_game.GetDealerHand(), a_game.GetDealerScore());
//This method below should show player's cards, but it shows dealer's card
a_view.DisplayPlayerHand(a_game.GetPlayerHand(), a_game.GetPlayerScore());
Once again, thanks for any kind of help... I just have no clue how to solve this.
Upvotes: 1
Views: 1620
Reputation: 31699
The Observer
interface has an update
method defined like this in the javadoc:
void update(Observable o,
Object arg)
In notifyObservers
, you've written
o.update(m_hand, this);
But this
is a Player
, which implements Observable
, so you want that first:
o.update(this, m_hand);
The second parameter m_hand
, is, I presume, the argument you want passed to the observer. However, in the class that implements Observer
, the update
method must have the same parameter types as the one defined in the javadoc:
public void update(Observable o, Object hand) {
Even though you know hand
will always be List<Card>
, the parameter still has to be an Object
, because the type has to match the type in the interface. You then have to cast it to be able to use it like a List
:
public void update(Observable o, Object handArg) {
List<Card> hand = (List<Card>)handArg;
Upvotes: 2