"Condition is always true"当我知道它不是
"Condition is always true" when I know it's not
我为我的C++课程做了一个二十一点游戏。我在检查谁赢得了比赛时遇到问题。在比赛期间,我会在每次抽奖后检查那个人是否被破坏(总共超过 21 次)。如果他们确实破坏了,我会将其存储在变量中。他们playerBust
和dealerBust
.它们被初始化为 0
。
win-check 是一个标准的 if/else-if 段。它说playerBust == 1
总是假的,dealerBust == 0
总是真的。
但是,游戏的最后一次测试我记录了这两个,并在最后dealerBust = 1
。
对我的代码的一些解释:
deck.newdeck
给了我一个新的、洗牌的套牌。
initializeGame();
将玩家和庄家的手牌总数设置为 0
。
.toString()
简单地返回一个命名卡片的字符串,例如"黑桃王牌"。
getPlayerCardValue(...)
和getDealerCardValue(...)
只是评估刚刚绘制的卡片的数值。
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;
你正在使用逻辑或(||
),而你实际上想使用逻辑和(&&
)代替。
考虑一下:
if (playerBust == 1 || dealerBust == 1){
cout << "Tie" << endl;
}
else if (playerBust == 1 || dealerBust == 0)
如果不采用第一个分支,那么我们知道playerBust
不是真的,dealerBust
也不是。因此,在else if
中,测试playerBust
是否为真(不可能)或dealerBust
是否为假(一定是)是没有意义的。
相关文章:
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 可能我知道为什么这段代码没有给出任何输出吗?
- 如果我知道只会使用现有元素,我可以在数组开头之前传递指针吗?
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- Fread C++ in C#.我知道我需要字节缓冲区,但并不完全在那里
- 带有钩子的C 二进制树,让我知道何时/何时插入或移动节点
- std::vector::reserve(未知m),我知道m<<;N(通常)并且知道N
- 大整数增加,我知道理论..在实践中仍然生锈
- 是否有预处理器宏可以让我知道应用程序是控制台类型还是其他类型
- C++ map<std::string> vs map<char *> 性能(我知道,"again?" )
- 请让我知道,如果有任何例外的说法,我想出来
- 如果我有一个固定大小的填充ascii数字的char数组,我知道它指的是一个无符号整数,那么将其转换为无符号整数的最快方法
- 为什么当我将 true 分配给 int 变量时编译器不抱怨?
- 当我知道类型时,如何避免虚拟呼叫
- "Condition is always true"当我知道它不是
- 如何初始化对象列表的向量.我知道很困惑
- 我知道 pantheios 是线程安全的,但它是进程间安全的吗?
- 使用 c++,为什么我知道它不是时会收到错误消息“字符串下标超出范围”?或者至少看起来是这样
- 如果我知道三维中的齐次变换矩阵,如何使用opencv找到旋转和平移的角度
- "missing return statement",但我知道它在那里