#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#include <random>
#include <string>
using namespace std;
const string Rank[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //撲克牌點數(shù)
const string Suits[4] = {"SPADES","HEARTS","DIAMONDS","CLUBS"}; //撲克牌花色
class Card
{
private:
int rank;
int suit;
public:
Card(){}
~Card(){}
Card(int rank,int suit)
{
this->rank = rank; //(1)
this->suit = suit; //(2)
}
int getRank()
{
return rank;
}
int getSuit()
{
return suit;
}
void printCard()
{
cout << "(" << Rank[rank] << " , " << Suits[suit] << ")";
}
};
class DeckOfCards
{
private:
Card deck[52];
public:
DeckOfCards() //初始化牌桌并進行洗牌
{
for(int i = 0; i < 52; i++)
{
deck[i] = Card(i%13, i%4); //(3)
}
// srand((unsigned)time(0)); //設(shè)置隨機種子
std::random_device rd;
shuffle(&deck[0], &deck[51],std::default_random_engine(rd())); //洗牌
}
~DeckOfCards()
{
}
void printCards()
{
for(int i = 0; i < 52 ; i++)
{
deck[i].printCard(); //(4)
if((i + 1) % 4 == 0) cout << endl;
else cout << "\t";
}
}
};
int main()
{
DeckOfCards* d = new DeckOfCards(); //(5)
d->printCards(); //(6)
delete d;
return 0;
}
答案:
(1) this->
(2) this->
(3) deck[i]
(4) deck[i].
(5) new DeckOfCards()
(6) d->printCards()