我正在制作一个二十一点的卡片,而且我想把这张卡片放在赌场里,
即。所有玩家都获得一张牌,经销商一张面朝下,玩家获得另一张牌,经销商得到一张面朝上
但是看起来我是干涉的:(如何重做?
void BlackJack::newHand()
{
resetHands();
for (unsigned int i = 0; i < players.size(); i++)
players[i]->addCard(deck->nextCard());
Card* c = deck->nextCard();
c->setVisible(false); // dealer gets a face down card
dealer->addCard(c);
for (unsigned int i = 0; i < players.size(); i++)
players[i]->addCard(deck->nextCard());
dealer->addCard(deck->nextCard());
}
答案 0 :(得分:13)
将重复的代码移动到另一个函数中:
void BlackJack::addDealerCard(bool visible) {
Card* c = deck->nextCard();
c->setVisible(visible);
dealer->addCard(c);
}
void BlackJack::addCards() {
for (unsigned int i = 0; i < players.size(); i++)
players[i]->addCard(deck->nextCard());
}
void BlackJack::newHand() {
resetHands();
addCards();
addDealerCard(false);
addCards();
addDealerCard(true);
}
答案 1 :(得分:1)
void BlackJack::newHand()
{
resetHands();
dealToPlayers();
Card* c = deck->nextCard();
c->setVisible(false); // dealer gets a face down card
dealer->addCard(c);
dealToPlayers();
dealer->addCard(deck->nextCard());
}
void BlackJack::dealToPlayers()
{
for (unsigned int i = 0; i < players.size(); i++)
players[i]->addCard(deck->nextCard());
}