卡牌游戏类的 OOP 设计

发布于 2025-01-03 11:05:52 字数 716 浏览 0 评论 0 原文

应用设计模式设计以下类时最好的方法是什么?

  • 牌组 - addCard、deal、shuffle、getTopCard、removeTopCard、removeAllCards
  • 手牌 - addCard、removeCard、getCard、removeAllCards
  • DiscardPile - addCard、getTopCard、removeTopCard、removeAllCards
  • MeldPile - addCard、removeAllCards

(MeldPile 保存表中的所有融合。)

对我来说,我认为 getTopCardremoveTopCard 只是 getCardremoveCard 的包装,因为它只是获取卡片的顶部位置,然后将其传递给getCardremoveCard

我应该使用组合吗?战略模式?或者只是创建另一个名为 CardPile 的类并将其用作上述类的基类?如果您能提供这方面的示例代码,我将不胜感激。

What would be the best approach when designing the following classes applying design patterns?

  • Deck - addCard, deal, shuffle, getTopCard, removeTopCard, removeAllCards
  • Hand - addCard, removeCard, getCard,removeAllCards
  • DiscardPile - addCard, getTopCard, removeTopCard, removeAllCards
  • MeldPile - addCard, removeAllCards

(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2025-01-10 11:05:52

我认为您可以使用如下所示的单个甲板类来实现您想要的目标,该甲板类本质上是 Stack,我不明白为什么任何特定的牌/堆/手不想要大多数(如果不是全部)相同的方法。

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);
    }

}

我看到的唯一真正的问题是 deal() 方法,这是否应该是 Deck 的责任?就我个人而言,我不这么认为,这让我认为也许你会有一个 Player 类和一个 Dealer 类,它们扩展 Player 并实现发牌的逻辑

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());
            } 
        }
    }

    // .....
}

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());
            } 
        }
    }

    // .....
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文