我违反了 DRY 请帮助我吗?
我正在制作一个二十一点模拟游戏,我想像在赌场一样发牌,
即所有玩家都发一张牌,庄家得到一张面朝下的牌,玩家得到另一张牌,庄家得到一张面朝上的牌,
但是看我违反了规定干:(如何重做??
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());
}
I'm making a blackjack sim and I want to deal the cards how it would be in a casino,
i.e. all players get dealt a card, dealer gets one face down, players get another card, dealer gets one face up
BUT LOOK I VIOLATED DRY :( How to redo??
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());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将重复的代码移到另一个函数中:
Move the repeated code into another function: