我违反了 DRY 请帮助我吗?

发布于 2024-08-18 11:48:24 字数 580 浏览 8 评论 0原文

我正在制作一个二十一点模拟游戏,我想像在赌场一样发牌,

即所有玩家都发一张牌,庄家得到一张面朝下的牌,玩家得到另一张牌,庄家得到一张面朝上的牌,

但是看我违反了规定干:(如何重做??

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请你别敷衍 2024-08-25 11:48:24

将重复的代码移到另一个函数中:

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);
}

Move the repeated code into another function:

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);
}
ぃ双果 2024-08-25 11:48:24
    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());
    }
    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());
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文