2

我正在尝试了解如何在学校的这个黑桃游戏项目中使用课程。运行此代码(如下)时,我似乎遇到了各种错误。我正在尝试创建一张牌,创建一个牌组,洗牌该牌组,然后将牌发给玩家和计算机。我不太确定我是否正确使用了类,我还不确定如何在 main() 部分实现它们。任何示例都会非常有帮助和赞赏。

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <Windows.h>

// Game Specific Files
#include "CardGameConfig.h"
#include "CardGameFunctions.h"
#include "CardGameStructs.h"

using namespace std;



// Card Class
class Card
{
private:
    int _suit;
    int _rank;
public:
   Card();
   Card(int &iSuit, int &iRank); 
   ~Card(); // Deconstructor
   int GetSuit(); //Method
   int GetRank(); //Method
};


// sets a blank card
Card::Card(){      
    _suit = 0;
    _rank = 0;
}


// creates a card with a given suit
Card::Card(int &iSuit, int &iRank){
    _suit = iSuit;
    _rank = iRank;

} // end of Card function (class member)

int Card::GetSuit(){
        return _suit;
}

int Card::GetRank(){
    return _rank;
}

Card::~Card(){
}


// Deck Class
class Deck
{
private:
    Card cards[TOTAL_CARDS_IN_DECK];
    Card *_pCardList[52];
public:
    Deck();
   ~Deck();
   void Shuffle();
   Card DrawCard();
   Card *_pCardList; // holds an array of cards (52 of them)
   int _index;
};

Deck::Deck(){


    // Start deck index at zero
    _index = 0;
    _pCardList = new Card[TOTAL_CARDS_IN_DECK];

    // TOTAL_CARDS_IN_SUIT = 13
    for(int iRank = 0; iRank < TOTAL_CARDS_IN_SUIT; iRank++)
    {
        int iRankOffset = 2;
        iRank = iRank + iRankOffset;

        //TOTAL_SUITS = 4
        for(int iSuit = 0; iSuit < TOTAL_SUITS; iSuit++){
            iSuit = ESuits(iSuit);
            _pCardList[_index] = Card(iRank, iSuit);        
            _index++;
        } // end of iSuit for loop
    } // end of iRank for loop */
}


void Deck::Shuffle(){
    srand((int) time( NULL ) );

    for(int iCardNum = 0; iCardNum < TOTAL_CARDS_IN_DECK; iCardNum++)
    {
        int iRandomNumber = ( rand() % TOTAL_CARDS_IN_DECK-1);
        Card card = _pCardList[iCardNum];
        _pCardList[iCardNum] = _pCardList[iRandomNumber];
        _pCardList[iRandomNumber] = card;
    }
}

Card Deck::DrawCard(){

    _index++;
    return _pCardList[_index-1];

}

Deck::~Deck()
{
    delete [] cards;
}

int main(int argc, char* argv[]){

    Card CardGame;
    Deck Deck;



//  cout << Deck._pCardList[1].GetRank() << " " << Deck._pCardList[1].GetSuit() <<     endl;

    return 0;
    std::cin.get();
    std::cin.get();
}
4

1 回答 1

1

你不能声明_pCardList两次:

class Deck {
private:
    Card cards[TOTAL_CARDS_IN_DECK];
    // Card *_pCardList[52]; // FIRST TIME (useless)
    Card *_pCardList;         // SECOND TIME (this should be private)
    int _index;               // This should also be private
public:
    Deck();
    ~Deck();
    void Shuffle();
    Card DrawCard();
};

一点好处:对于您的绘图功能,您可以这样做:

Card Deck::DrawCard(){
    return _pCardList[_index++];
}

这是有效的,因为在递增之前_index++返回值。_index

于 2012-10-20T07:01:11.590 回答