What would be the best approach when designing the following classes applying design patterns?
(The MeldPile holds all the melds in the table.)
For me, I think the getTopCard
and removeTopCard
are just a wrapper of getCard
and removeCard
, as it just get the top position of a card then pass it to getCard
or removeCard
.
Should I use composition? strategy pattern? or just create a another class called CardPile and use it as the base class of the above class? Really appreciate if you could provide a sample code on this.
In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
As stated earlier, OOP is helpful because it helps create maintainable code — code that is understandable, adaptable, and extendable. It also helps create reusable code by following the DRY (Don't Repeat Yourself) method: write the code once and then reuse it, rather than copying and pasting.
We can use random. shuffle() to shuffle the deck and deck. pop() to deal cards into a player's Hand .
I think you can achieve what you want with a single deck class like below which is essentially a wrapper around Stack, I don't see why any particular deck/pile/hand would not want most if not all of the same methods.
class Deck {
private Stack<Card> cards = new Stack<Card>();
public Deck() { }
public Deck(int numberOfCards) {
for (int i=0; i<numberOfCards; i++) {
cards.push(CardFactory.createCard(i));
}
}
private void shuffle() {
Collections.shuffle(this.cards);
}
public void sort() {
Collections.sort(this.cards);
}
public void removeAllCards() {
this.cards.removeAllElements();
}
public void removeCard(Card c) {
int i = this.cards.search(c);
this.cards.remove(i);
}
public Card getCard(Card c) {
int i = this.cards.search(c);
return this.cards.get(i);
}
public Card getTopCard() {
return this.cards.pop();
}
public Card getNthCard(int i) {
return this.cards.get(i);
}
public Card addCard(Card c) {
this.cards.push(c);
}
}
The only real problem i see is with the deal()
method and whether this should be the responsibility of a Deck? Personally I would not think so, this leads me to think that perhaps you would have a Player class and a Dealer class that extends Player and implements the logic of dealing a deck
class Player() {
protected String name;
protected Deck hand = new Deck();
public void addCard(Card c) {
this.hand.addCard(c);
}
// .....
}
class Dealer() extends Player {
private Deck deck;
public Dealer(int deckSize) {
this.deck = new Deck(deckSize);
}
public void deal(Player[] players, int numberOfCards) {
for (player in players) {
for (int i=0; i<numberOfCards; i++) {
player.addCard(this.deck.getTopCard());
}
}
}
// .....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With