开发者

i violated D.R.Y. help me please?

I'm making a blackjack sim and I want to deal the cards how it would be in a casino,

开发者_StackOverflow

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜