“条件总是真实的”当我知道它不是 [英] "Condition is always true" when I know it's not

查看:157
本文介绍了“条件总是真实的”当我知道它不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的C ++课程制作了一个二十一点游戏。我有一个问题检查谁赢了游戏。在比赛中,我检查每次抽奖后,无论那个人是否突破(总共超过21)。如果他们破了我把它存储在一个变量。他们是 playerBust dealerBust 。它们初始化为 0



胜出检查是一个标准的if / else-if段。它表示 playerBust == 1 始终为false, dealerBust == 0 始终为真。 >

然而,游戏的最后一个测试我记录了这两个,并且到结束 dealerBust = 1



我的代码的几个解释:



deck.newdeck

initializeGame(); 设置玩家和庄家的手总数到 0



.toString()



getPlayerCardValue(...) getDealerCardValue(...)只是评估刚刚绘制的卡的数值。

  void Blackjack :: playGame(){
deck.newDeck();
initializeGame();
drawingInitialCards();
bool playerBust = 0;
bool dealerBust = 0;
Card newCard;

// PLAYERS TURN
if(playerHand> 21){
playerBust = 1;
}
else if(playerHand< 21){
bool stopDraw = 0;
while(stopDraw == 0){
bool playerDraw = askPlayerDrawCard();
if(playerDraw == 1){
newCard = deck.drawCard();
cout<< endl<< 你画:< newCard.toString()<< endl;
playerHand + = getPlayerCardValue(newCard);
cout<< 玩家总数:<< playerHand< endl;
if(playerHand> 21){
playerBust = 1;
stopDraw = 1;
}
}
else if(playerDraw == 0){
stopDraw = 1;
}
}
}

// DEALERS TURN
dealerHand + = getDealerCardValue(dealerFaceDown,dealerHand);
cout<< 经销商面朝下的牌是:< dealerFaceDown.toString()<< endl
<< 经销商总数:< dealerHand< endl;

if(dealerHand> 21){
dealerBust = 1;
}
else if(dealerHand< 21){
while(dealerHand< 17){
newCard = deck.drawCard
cout<< endl<< newCard.toString()<< endl;
dealerHand + = getDealerCardValue(newCard,dealerHand);
cout<< 经销商的手总数:< dealerHand< endl;
if(dealerHand> 21){
dealerBust = 1;
}
}
}

//获胜条件
if(playerBust == 1 || dealerBust == 1){
cout << Tie< endl;
}
else if(playerBust == 1 || dealerBust == 0){
cout< 经销商胜利< endl;
}
else if(playerBust == 0 || dealerBust == 1){
cout< 玩家获胜< endl;
}
else if(playerBust == 0 || dealerBust == 0){
if(playerHand> dealerHand){
cout< 玩家获胜< endl;
}
}
cout<< endl<< Player's bust:<< player<< endl<< 经销商的胸围:<经销商<< endl;


解决方案

请考虑:

  if(playerBust == 1 || dealerBust == 1){
cout< Tie<< endl;
}
else if(playerBust == 1 || dealerBust == 0)

如果不采用第一个分支,那么我们知道 playerBust 不是真的,也不是 dealerBust 。所以在 else if 没有点测试如果 playerBust 是真的(不能是) c $ c> dealerBust 为false(必须为)。


I made a blackjack game for my C++ course. I have a problem checking who won the game. During the game I check after each draw whether or not that person busted (exceeded 21 in total). If they did bust I store it in a variable. They are playerBust and dealerBust. They are initialized to 0.

The win-check is a standard if/else-if segment. It says that playerBust == 1 is always false, and that dealerBust == 0 is always true.

However, the last test of the game I logged both of these, and dealerBust = 1 by the end.

A few explanations of my code:

deck.newdeck gives me a new, shuffled deck.

initializeGame(); sets the player's and dealer's hand total to 0.

.toString() simply return a string naming a card, for instance "Ace of Spades".

getPlayerCardValue(...)and getDealerCardValue(...) just evaluates the numerical value of the card just drawn.

void Blackjack::playGame(){
deck.newDeck();
initializeGame();
drawInitialCards();
bool playerBust = 0;
bool dealerBust = 0;
Card newCard;

// PLAYERS TURN
if (playerHand > 21){
    playerBust = 1;
}
else if (playerHand < 21){
    bool stopDraw = 0;
    while (stopDraw == 0){
        bool playerDraw = askPlayerDrawCard();
        if (playerDraw == 1){
            newCard = deck.drawCard();
            cout << endl << "You drew: " << newCard.toString() << endl;
            playerHand += getPlayerCardValue(newCard);
            cout << "Player's total: " << playerHand << endl;
            if (playerHand > 21){
                playerBust = 1;
                stopDraw = 1;
            }
        }
        else if (playerDraw == 0){
            stopDraw = 1;
        }
    }
}

// DEALERS TURN
dealerHand += getDealerCardValue(dealerFaceDown, dealerHand);
cout << "Dealer's face down card is: " << dealerFaceDown.toString() << endl
<< "Dealer's total: " << dealerHand << endl;

if (dealerHand > 21){
    dealerBust = 1;
}
else if (dealerHand < 21){
    while (dealerHand < 17){
        newCard = deck.drawCard();
        cout << endl << newCard.toString() << endl;
        dealerHand += getDealerCardValue(newCard, dealerHand);
        cout << "Dealer's hand totals: " << dealerHand << endl;
        if (dealerHand > 21){
            dealerBust = 1;
        }
    }
}

// WINNING CONDITIONS
if (playerBust == 1 || dealerBust == 1){
    cout << "Tie" << endl;
}
else if (playerBust == 1 || dealerBust == 0){
    cout << "Dealer wins" << endl;
}
else if (playerBust == 0 || dealerBust == 1){
    cout << "Player wins" << endl;
}
else if (playerBust == 0 || dealerBust == 0){
    if (playerHand > dealerHand){
        cout << "Player wins" << endl;
    }
}
cout << endl << "Player's bust: " << playerBust << endl << "Dealer's bust: " << dealerBust << endl;

解决方案

Consider this:

if (playerBust == 1 || dealerBust == 1){
    cout << "Tie" << endl;
}
else if (playerBust == 1 || dealerBust == 0)

If the first branch is not taken then we know that playerBust is not true, and neither is dealerBust. So in the else if there's no point testing if playerBust is true (it can't be) or if dealerBust is false (it must be).

这篇关于“条件总是真实的”当我知道它不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆