创建一副牌

时间:2015-09-08 19:42:33

标签: c++ linked-list stack

好的,首先,我已经查看了有关创建一副卡片的各种问题,但我看到的每一个问题都是使用矢量的东西,我不知道怎么写它,因为我我没有在课堂上学习这门课程,所以我不知道如何应用它。

Card.HCard.cpp都很好,无需更改其中的内容

我需要Deck.HDeck.cpp的帮助。

我的initialize()函数没有完成,我似乎无法知道如何完成它,并且使用Deck类中的其他方法我没有尝试写任何一个,因为我无法生成一副牌。

CARD.H

Class Card
{

    int m_face; 
    char m_suit;


public:

Card(int _face = 2 , char _suit = 3);

~Card();

int GetFace() const;
char GetSuit() const;

void SetFace(int _face);
void SetSuit(char _suit);

void Show() const;
}

CARD.CPP

#include "Card.h"
Card::Card(int _face, char _suit)
{

    m_face = _face;
    m_suit = _suit;
}

Card::~Card()
{

}
int Card ::GetFace() const
{
   return m_face;
}

char Card ::GetSuit() const
{
    return m_suit;
}

void Card::SetFace(int _face)
{
   m_face = _face;
}

void Card::SetSuit(char _suit)
{
   m_suit = _suit;
}

void Card::Show() const
{
   if (m_face == 11)
      cout << " J " << m_suit << endl;
   else if (m_face == 12)
      cout << " Q " << m_suit << endl;
   else if (m_face == 13)
      cout << " K " << m_suit << endl;
   else if (m_face == 14)
      cout << " A " << m_suit << endl;
   else
      cout << m_face << m_suit << endl;
}

加入deck.h

#pragma once
#include "stdafx.h"
#include "Card.h"

Class Deck
{
    Card m_cards[52];

public:

    Deck();
    void Initialize();
    void Shuffle();
    bool Draw(Card& _card);
    void Clear();
    bool IsEmpty() const;
}

DECK.CPP

#include "Deck.h"
#include"Card.h"

void Deck::Initialize()
{
int count = 0;
    char Suits[] = { 3, 4, 5, 6 };

    for (int i = 0; i < 4; ++i) //Suits
    {

        for (int F = 2; F < 14; ++F) //faces
        {
            m_cards[count].SetSuit(Suits[i]);
            m_cards[count].SetFace(F);

        }
    }

}

    void Deck::Shuffle()
{
}
    bool Deck::Draw(Card& _card
{
}
    void Deck::Clear()
{
}
    bool Deck::IsEmpty() const
{
}

2 个答案:

答案 0 :(得分:0)

我认为您的Initialize功能不需要更多工作。

只有2条评论:

  1. 你忘记了内部for循环结束时的++count(现在你每次都在设置同一张卡片。)
  2. 在编写内部for循环时,F变量只允许最多13个(因为您使用了< 14)。这意味着你的牌组不会包含任何牌照......对我来说最合乎逻辑的是使用<= 14代替。

答案 1 :(得分:0)

稍加调整就可以了。

#include <iostream>
#include <algorithm>
using namespace std;

class Card
{
private:
    int m_face;
    char m_suit;
public:
    Card(int _face = 2 , char _suit = 3)
    {
        m_face = _face;
        m_suit = _suit;
    }
    ~Card(){}
    int GetFace() const { return m_face; }
    char GetSuit() const { return m_suit; }

    void SetFace(int _face) { m_face = _face; }
    void SetSuit(char _suit) { m_suit = _suit; }
    void Show() const
    {
        if (m_face == 11)
            cout << " J " << m_suit << endl;
        else if (m_face == 12)
            cout << " Q " << m_suit << endl;
        else if (m_face == 13)
            cout << " K " << m_suit << endl;
        else if (m_face == 14)
            cout << " A " << m_suit << endl;
        else
            cout << m_face << m_suit << endl;
    }
};
class Deck
{
private:
    Card m_cards[52];
    int current;
public:
    Deck() { Initialize(); }
    void Initialize()
    {
        current = 51;
        int count = 0;
        char Suits[] = { 3, 4, 5, 6 };

        for (int i = 0; i < 4; ++i) //Suits
        {

            for (int F = 2; F <= 14; ++F) //faces
            {
                m_cards[count++].SetSuit(Suits[i]);
                m_cards[count++].SetFace(F);

            }
        }
    }
    void Shuffle() { std::random_shuffle(m_cards, m_cards + current + 1); }
    bool Draw(Card& _card)
    {
        if (IsEmpty()) return false;
        _card = m_cards[current--];
        return true;
    }
    void Clear() { current = -1; }
    bool IsEmpty() const { return current < 0; }
};
int main()
{
    Deck deck;
    while(!deck.IsEmpty())
    {
        Card c;
        deck.Draw(c);
        c.Show();
    }
    return 0;
}