作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
<分区>
我正在尝试学习如何在学校的黑桃游戏项目中使用类(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();
}
我是一名优秀的程序员,十分优秀!