gpt4 book ai didi

C++ - 黑桃 - 类 : How to use them

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:30:27 24 4
gpt4 key购买 nike

<分区>

我正在尝试学习如何在学校的黑桃游戏项目中使用类(class)。运行这段代码时,我似乎遇到了各种错误(如下)。我正在尝试创建一张卡片,创建一副牌,洗牌,然后将牌发给玩家和计算机。我不太确定我是否正确使用了类,我还不确定如何在 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();
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com