【问题标题】:BlackJack Creating Dynamic Deck C OOP二十一点创建动态甲板CO OOP
【发布时间】:2012-10-17 04:18:36
【问题描述】:

我的教授给我们布置了这个作业,但是我不知道如何做第一步。 我不是要求任何人做作业,只是请有人帮我解决第一个问题

  1. 动态创建卡组

这个问题是基于两个文件(他提供的)

“卡片.h”

//*********************************************************
//  CLASS DECLARATION
//*********************************************************
class Card
{   
    //*****************************************************
    //  Public Members         
    //*****************************************************
   public: 
    // Exception classes
    class NotInitalized {};

    // Enumeration for Suit
    enum Suit { Clubs, Diamonds, Hearts, Spades, 
               UNKNOWN };

    // Enumeration for Card Name
       enum CardName { Ace, Two, Three, Four, Five, Six,
                    Seven, Eight, Nine, Ten, Jack, 
                    Queen, King, UNKNOWN };

    // constructors
//*****************************************************
// Card
//  
// Create uninitialized card.  Must be initialized with 
//  'setCard()' before use.
//*****************************************************
    Card();    // card is not initialized
//*****************************************************
// Card
//  
// Create a card based its ordinal position.
//     cards are ordered by suit first in the order 
//     Clubs, Diamonds, Hearts and Spades, and within 
//     the suit they are ordered Act thru King.
//******************************************************
    Card(int); // number between 1-52

//******************************************************
// Card
//  
// Create a card with the given name and suit.
//*******************************************************
       Card(Suit, CardName);

    // methods
    //*******************************************************
    // setCard
    //  
    //  Set the Suit and Name of the card
    //*******************************************************
        void setCard(Suit, CardName);

    //*******************************************************
    // getSuit
    //  
    //  returns the element of the Suit enumeration 
    // representing the suit of the card
    //*******************************************************
    int      getSuit();

    //*******************************************************
    // getCardName
    //  
    //  returns the element of the CardName enumeration 
    //  representing the card
    //*******************************************************
    int    getCardName();

    //*******************************************************
    // getCardValue
    //  
    //  returns face value of card.  For Ace -1 is the value.
    //*******************************************************
    int         getCardValue();

     //*****************************************************
     // toString
     // 
     // return the string representation of the card. 
     //    e.g.,"Ace of Spades"
     //*****************************************************
       string      toString();

//************************************************
//  Private Members    
//************************************************
private:
    // the Card’s suit (uses Suit enum)
    Suit      suit = Suit::UNKNOWN; 

    // the Card’s name (uses CardName enum)
    CardName  name = CardName::UNKNOWN; 
};

第二类是Deck类。此类代表标准扑克牌中的 52 张牌。在内部,卡片组中的卡片应保存在 Card 对象数组中。还应该有一个并行的卡片指针数组,可以存储每次洗牌后卡片的顺序。

创建 Deck 对象时,它会创建 52 张牌并将它们洗牌。如果牌组在重新洗牌之前用完牌,则 dealCard() 方法应抛出 DeckEmpty 异常。

因为这个类创建了卡片对象,所以它应该有一个析构函数,当Deck被删除时,它会删除所有相关的卡片对象。

下面是 Deck 类的类声明。

//*********************************************************
//  CLASS DECLARATION
//*********************************************************
#include “Card.h”
class Deck
{
//*****************************************************
// Public Members         
//*****************************************************
public: 
    // Exception classes
    class DeckEmpty {};

    // Constructors/Destructors
    Deck(); // creates the cards and sorts them
   ~Deck(); // frees all the cards

    // Methods
    //****************************************************
    // dealCard
    //
    // return the next available card in the shuffled deck
    //****************************************************
    Card      dealCard();

    //****************************************************
    // shuffle
    //
    // shuffle the cards
    //****************************************************
    Void      shuffle();      // shuffle the deck


    //****************************************************
    // getCardCount
    //
    // return the number of unused cards in the shuffled 
    //     deck
    //****************************************************
       int       getCardCount(); // how many cards left

    //****************************************************
    // toString
    //
    // return a newline (\n) delimited list of the shuffled 
    //    cards 
    //*****************************************************
    string    toString();

//*****************************************************
// Private Members    
//*****************************************************
private:
    // array to hold unshuffled cards
       Card      cards[DECK_SIZE];

    // array to hold shuffled cards
    Card*     shuffledCards[DECK_SIZE];

    // index of next card to deal from shuffled cards
       int       nextCardIndex;    
};

【问题讨论】:

    标签: c++ oop blackjack


    【解决方案1】:

    我的教授给我们布置了这个作业,但是我不知道如何做第一步。我不是要求任何人做作业,只是请有人帮我解决第一个问题

    1. 动态创建卡组

    这就是答案:

    Deck* obj = new Deck();
    

    你在上面看到的是新的表达方式。 http://en.cppreference.com/w/cpp/language/new 使用此表达式可以动态创建新对象。

    【讨论】:

      【解决方案2】:

      这对我来说也没有任何意义。特别是关于平行排列的洗牌的一点不清楚。并且因为它创建 Card 对象而需要析构函数的说法是不正确的。也许他的意思是它动态创建 Card 对象,但首先不是他说的,其次我看不出有必要。我认为你应该和你的教授谈谈。

      不过第一步很简单。

      '1.动态创建卡组'

      Deck *my_deck = new Deck;
      

      解决了。

      为什么你必须动态创建套牌是另一个问题,但这就是他要求你做的事情。

      我完全不确定你的教授知道他在说什么。

      【讨论】:

      • 感谢您的帮助。我已经尝试过了,但在 Dev C++ 中我收到错误“西装不是类或名称空间”我相信它是指 Card.h 中的私有方法我已经尝试了所有方法,但我不明白他为什么使用“未知”?谢谢
      • 更多证据表明您的教授不知道他在说什么。我认为他对 Java 感到困惑。无论如何,只需将Suit suit = Suit::UNKNOWN; 替换为Suit suit;,将CardName name = CardName::UNKNOWN; 替换为CardName name;。当他的代码实际上没有编译时,我认为你有义务去告诉他。毫无疑问,他会找借口。
      猜你喜欢
      • 2012-06-29
      • 2020-08-12
      • 2016-11-09
      • 2016-02-08
      • 2012-07-27
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 2015-02-08
      相关资源
      最近更新 更多