c++ - 以一种有凝聚力的方式将班级聚集在一起
问题描述
我一直在做一个模拟战争纸牌游戏的工作,以收集关于需要多少回合才能完成的统计数据。
我创建了一个 Player 类和一个 Deck 类来处理它们各自的角色,但我对如何以一种有凝聚力的方式将它们组合在一起感到困惑。我需要一个可以玩牌的环境。我想主要处理这一切,但似乎那将是一团糟。
我还想过制作另一个由 Player 和 Deck 对象组成的类。我只是不知道将这些课程放在一起的最佳方法是什么。
玩家:
class Player
{
public:
Player();
void placeCardInHand(int); //cards placed in hand will be placed pushed to the front
int getCardFromHand(); // cards leaving the hand will be popped from the front
bool handIsEmpty(); //if the players hand is empty return true
private:
deque<int> hand; // the players hand can hold an entire deck of cards.
};
void Player::placeCardInHand(int aCard)
{
hand.push_front(aCard); //card is pushed to the front of the hand
}
int Player::getCardFromHand()
{
int temp = hand.front(); //get the first item in the hand
hand.pop_front(); // remove the first item from the hand
return temp; //return that item
}
bool Player::handIsEmpty()
{
return this->hand.empty();
}
甲板:
class Deck
{
public:
Deck(); //deck starts with 52 cards in order 0 - 51
void shuffleTheDeck(); //one call to this completely shuffles the deck
int drawCard(); //gives the card on top then pops from the deck
void showDeck(); //nessesary for testing
private:
deque<int> theDeck;
};
Deck::Deck()
{
for (int i = 0; i < 52; i++)
{
this->theDeck.push_front(i); //fill the deck with 52 cards 0 - 51
}
}
void Deck::shuffleTheDeck() //pick two random positions and swap the cards
{
int temp;
int position1;
int position2;
for (int i = 0; i < 500; i++)
{
position1 = rand() % 52; //there is only 52 positions in the deck this prevents going out of bounds
position2 = rand() % 52;
temp = this->theDeck[position1];
this->theDeck[position1] = this->theDeck[position2];
this->theDeck[position2] = temp;
}
}
int Deck::drawCard()
{
int temp = theDeck.front(); //gets the top card
theDeck.pop_front(); //pops top card
return temp; //returns top card
}
void Deck::showDeck()
{
deque<int> temporaryDeck = this->theDeck;
while (temporaryDeck.empty() != true)
{
cout << temporaryDeck.front() << " ";
temporaryDeck.pop_front();
}
cout << endl;
}
Deck::Deck()
{
for (int i = 0; i < 52; i++)
{
this->theDeck.push_front(i); //fill the deck with 52 cards 0 - 51
}
}
void Deck::shuffleTheDeck() //pick two random positions and swap the cards
{
int temp;
int position1;
int position2;
for (int i = 0; i < 500; i++)
{
position1 = rand() % 52; //there is only 52 positions in the deck this prevents going out of bounds
position2 = rand() % 52;
temp = this->theDeck[position1];
this->theDeck[position1] = this->theDeck[position2];
this->theDeck[position2] = temp;
}
}
int Deck::drawCard()
{
int temp = theDeck.front(); //gets the top card
theDeck.pop_front(); //pops top card
return temp; //returns top card
}
void Deck::showDeck()
{
deque<int> temporaryDeck = this->theDeck;
while (temporaryDeck.empty() != true)
{
cout << temporaryDeck.front() << " ";
temporaryDeck.pop_front();
}
cout << endl;
}
目前主要是我正在处理游戏的所有动作的地方。例如,我已经实现了交易的工作方式。但是有没有更好的方法让我可以重用代码?
int main() {
Deck cards;
Player player1, player2;
deque<int> field;
cards.shuffleTheDeck(); //shuffle the deck
for (int i = 0; i < 52; i++) //deal the cards
{
if (i % 2 == 0)
{
player1.placeCardInHand(cards.drawCard());
}
else
{
player2.placeCardInHand(cards.drawCard());
}
}
while (!player1.handIsEmpty() && !player2.handIsEmpty()) //turns start
{
} //game over
return 0;
}
解决方案
推荐阅读
- android - 尝试响应 android 数字软键盘上的刻度时,onEditor 操作不起作用
- spring-boot - 无法在 QueryBuilders.termQuery 的帮助下获取数据
- python - 计算样本的标准偏差
- java - Tesseract OCR 不适用于 Java Web 应用程序
- c# - 如何在c#中从一行删除到文本文件的末尾?
- python - 使用 Pool.map() 类型错误的多线程 PhantomJS
- c# - 使用来自多个文件的任何 CPU (x64/x86)
- tensorflow - 简单的减法在 Tensorflow 中给出了错误的值
- php - php - 如何在没有 ffmpeg 的情况下制作视频缩略图
- javascript - 在 JS 中使用 this 与使用变量