コード例 #1
0
ファイル: game.cpp プロジェクト: cunundrummer/BlackJack
/**
    Compares 2 hands and returns int of winner or 3 if a push
    @return 1: if 1st hand is > 2nd hand.
    @return 2: if 1st hand is < 2nd hand.
    @return 0: if 1st hand is = 2nd hand.
 */
int Game::comparePlayerHands(Hand playersHand, Hand DealersHand) {
    const int HAND1_IS_GREATER = 1;
    const int HAND2_IS_GREATER = 2;
    const int HAND1_IS_BUSTED = 3;
    const int HANDS_ARE_EQUAL = 0;
    const int TWENTY_ONE = 21;
    
    int h1 = playersHand.calculate();
    int h2 = DealersHand.calculate();
    
    if (h1 > TWENTY_ONE  ) {
        return HAND1_IS_BUSTED;
    }
    else if (h1 > h2) {
        return HAND1_IS_GREATER;
    }
    else if (h1 == h2) {
        return HANDS_ARE_EQUAL;
    }
    else if (h1 < h2) {
        if (h2 > TWENTY_ONE) {
            return HAND1_IS_GREATER;
        }
        else
            return HAND2_IS_GREATER;
    }
    else {
        return TWENTY_ONE;
    }
}
コード例 #2
0
ファイル: game.cpp プロジェクト: cunundrummer/BlackJack
int Game::insurancePayout(std::vector<GamePlayer*> &gPlayers, Hand dealersHand) {
    //Player shouldn't be asked for insurance if he has BJ because it is unnessacary according to most forums/wikipedia
    const int BLACKJACK_FLAG = 21;
    const int GAME_GOES_ON_FLAG = 0;
    
    if (dealersHand.calculate() == BLACKJACK_FLAG) {
        for (auto gP: gPlayers) {
            if (gP->getHand(0).calculate() == BLACKJACK_FLAG) { //dealer and player have BJ (push w/ins.)
                if (gP->getInsuranceFlag() == true) {
                    payout(INSURANCE_PUSH, *gP, 0);
                    //std::cout << gP->getName(false) << " took insurance and wins even money! (2:1)" << std::endl;
                    
                }
                else {
                    payout(NO_INSURANCE_PUSH, *gP, 0);
                    //std::cout << gP->getName(false) << " did not take insurance, therefore it's a push! (1:1)" << std::endl; //dealer and player have BJ (push/no ins.)
                }
            }
            else { //dealer has BJ but player does not
                if (gP->getInsuranceFlag() == true) {
                    payout(INSURANCE_BJ, *gP, 0);
                    //std::cout << gP->getName(false) << " took insurance, therefore " << gP->getName(false) << " loses bet, but gets back insurance! (2:1)" << std::endl;
                }
                else {
                    payout(NO_INSURANCE_BJ, *gP, 0);
                    //std::cout << gP->getName(false) << " did not take insurance, and loses bet as a result." << std::endl;
                    
                }
            }
        }
        return BLACKJACK_FLAG; //game will start next round after payout
    }
    else { //dealer does not have BJ
        std::cout << "Dealer does not have BlackJack..." << std::endl;
        for (auto gP: gPlayers) {
            if (gP->getHand(0).calculate() == BLACKJACK_FLAG) { //dealer does not have BJ, player does have BJ
                std::cout << gP->getName(false) << " has BlackJack and wins bet at 3:2 payout" << std::endl;
                payout(BLACK_JACK, *gP, 0);
            }
            else { //neither dealer or player have BJ
                if (gP->isInSession()) {
                    std::cout << gP->getName(false) << " and Dealer do not have BlackJack" << std::endl;//, checking for special plays..." << std::endl;
                }
                else
                    std::cout << gP->getName(false) << " not in play" << std::endl;
            }
        }
    }
    //game goes on
    return GAME_GOES_ON_FLAG;
}