我正在为模拟扑克游戏的入门课程编写 C++ 程序。我创建了一个带有整数的卡片对象来存储面部和套装的值。然后我创建了一个包含 52 个卡片对象的 C++11 数组的 DeckOfCards 对象。我正在尝试为卡片分配它们的值并在deckOfCards 构造函数中洗牌。经过一些测试后,我相信我已经将问题追溯到这张脸和西装的生成过程。我还试图通过在 dealCard() 方法中返回对这些对象的引用来与另一个类(Hand)共享这些对象,但我认为错误是在实例化手之前发生的。该错误发生在我的 Arch-Linux VM 和 Visual Studio 上的 GCC 中,因此它似乎与编译器无关(尽管 clang 甚至不会编译它)。甲板构造器:
DeckOfCards::DeckOfCards(bool startShuffled) //Constructor.
{
/************************************************************
Constructor iterates through the four suits and 13 faces,
generating a card for each combination. This adds up to a
deck of 52 unique cards. By default it shuffles the deck to
prevent accidental usage of an unshuffled deck in a card
game. Passing 'false' as an arguement prevents the initial
shuffle.
************************************************************/
int arrayLoc = 0; //The card being initialized (its a subscript of deck).
for (int i = 0; i < 4; i++) //For each of the four suits.
{
for (int j = 0; j < 13; j++) //For each face in that suit.
{
deck[arrayLoc].setFace(j); //Create a card with that face.
deck[arrayLoc].setSuit(i); //And with that suit.
arrayLoc++; //Then move to the next card.
}
}
currentCard = 0; //Sets the top of the deck.
if (startShuffled)
shuffle(); //Shuffles the deck by default.
}
VS2013 的调试器说它在第一次迭代后无法读取甲板[0] 的脸和西装数据。它对其他人的行为也类似。主程序本身(此处未显示)运行到我尝试通过指向牌组的指针数组将牌发给手牌的地步,我认为这与牌组生成期间的不正确初始化有关。如果没有人发现我在这里的问题,我可以提供更多代码。
卡片的获取器/设置器:
void Card::setFace(int faceVal)
{
if (faceVal >= 0 && faceVal < 13) //Validates value before setting.
face = faceVal; //Sets the value of Card's face.
else
throw invalid_argument("Face value must be between 0 and 12 (inclusive)");
}
void Card::setSuit(int suitVal)
{
if (suitVal >= 0 && suitVal < 4) //Validates value before setting.
suit = suitVal; //Sets the value of Card's suit.
else
throw invalid_argument("Suit value must be between 0 and 3 (inclusive)");
}
//Getters
int Card::getFace() const //Returns face by value.
{
return face;
}
int Card::getSuit() const //Returns suit by value.
{
return suit;
}
编辑:(添加信息)我的手类有一个包含五个指向 Card 对象的指针的数组,它通过调用 DeckOfCard 的 dealCard() 方法从卡组中检索这些对象。
const Card DeckOfCards::dealCard()
{
/*************************************************
If the deck has not depreciated, this returns a
const reference to the card at the top of the deck
and increments to the next card. If it is depreciated
it throws an exception.
*************************************************/
if (moreCards())
{
unsigned int tempCurrentCard = currentCard;
currentCard++;
return deck[tempCurrentCard];
}
else
{
throw invalid_argument("The deck is out of unused cards. Must shuffle to continue.");
}
}
Hand 的构造函数:
Hand::Hand(const Card &card1, const Card &card2,
const Card &card3, const Card &card4, const Card &card5)
{
/*****************************************************
Constructor initializes a c++11 array of pointers to
card objects.
*****************************************************/
//Initialize the cards in hand. (as pointers)
cardsInHand[0] = &card1;
cardsInHand[1] = &card2;
cardsInHand[2] = &card3;
cardsInHand[3] = &card4;
cardsInHand[4] = &card5;
//Calculate the value of the hand.
calcHandScore();
}
我通过使用 myDeck.dealCard() 作为手的构造函数中的每个参数来实例化 main 中的手。这种数据交接似乎很可能是错误发生的地方。Hand player1(deck.dealCard(), deck.dealCard(), deck.dealCard(), deck.dealCard(), deck.dealCard());
但这也可能与套牌的洗牌算法有关:
void DeckOfCards::shuffle()
{
/***********************************************************
Shuffles the deck of cards by via a random number generator.
Should be called whenever the current card is the final
card in the deck.
***********************************************************/
srand(time(NULL)); //Creates a random seed for number generation.
int randomValue; //Stores random number for card selection.
//Loops through each card in deck and swaps it with another.
for (int i = 0; i < 52; i++) //Iterates through the deck.
{
randomValue = rand() % 51; //Selects a random card to swap.
if (randomValue != i) //Prevents self assignment.
{
//Swaps the cards values, effectively swapping their locations.
int tempFace = deck[randomValue].getFace(); //Holds a copy of selected card.
int tempSuit = deck[randomValue].getSuit();
deck[randomValue].setFace(deck[i].getFace());
deck[randomValue].setSuit(deck[i].getSuit());
deck[i].setFace(tempFace);
deck[i].setSuit(tempSuit);
}
}
currentCard = 0; //Shuffle resets the "top" of the deck.
}