制作一副纸牌



首先,我看了关于创建一副牌的各种问题但我看的每一个问题都是使用向量的东西,我不知道怎么写,因为我没有在课堂上学过这个所以我不知道如何应用它

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
{
}

我不认为你的Initialize函数需要更多的工作。

注释:

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

稍微调整一下就可以了。

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

相关内容

  • 没有找到相关文章

最新更新