我目前正在尝试制作一个简单的21点程序,该程序是单人游戏,只有1-10张卡片。我遇到的问题是,每当我第三次要求购买新卡时,都无法正确汇总我的卡。该程序也无法正确重启这里是输出示例:
Welcome to Blackjack!
Here are your first cards: 10, 2
Your total is 12.
Would you like to draw another card? (y/n)
y
You got a new card with a value of 6.
Your total is now 18.
Would you like to draw another card? (y/n)
y
You got a new card with a value of 4.
Your total is now 16.
Would you like to draw another card? (y/n)
n
Welcome to Blackjack!
Here are your first cards: 6, 4
Your total is 10.
Would you like to draw another card? (y/n)
我尝试过使用do-while循环,以便每次总数<21时都会要求换一张新卡,但结局相同。我不知如何解决这个问题。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int getCard(int);
int main()
{
srand(time(0));
int total, card1, card2, newTotal = 0;
char choice, playAgain = 'y';
while (playAgain == 'y')
{
cout << "Welcome to Blackjack!\n";
card1 = rand() % 10 + 1;
card2 = rand() % 10 + 1;
total = card1 + card2;
cout << "Here are your first cards: " << card1 << ", " << card2 << endl;
cout << "Your total is " << total << "." << endl;
cout << "Would you like to draw another card? (y/n)\n";
cin >> choice;
while (choice == 'y')
{
newTotal = getCard(total);
if (newTotal > 21)
{
cout << "Bust!" << endl;
choice = 'n';
}
else if (newTotal < 21)
{
cout << "Would you like to draw another card? (y/n)\n";
cin >> choice;
}
else
{
cout << "Congratulations, you won!\n";
choice = 'n';
}
}
}
cout << "Would you like to play again? (y/n)\n";
cin >> playAgain;
return 0;
}
int getCard(int total)
{
int addCard, newTotal;
addCard = rand() % 10 + 1;
newTotal = total + addCard;
cout << "You got a new card with a value of " << addCard << "." <<
endl;
cout << "Your total is now " << newTotal << "." << endl;
return newTotal;
}
该程序将以两张卡开始播放器,然后询问他们是否要抽出另一张。如果他们输入“ y”,那么它将这样做并合计卡片。如果他们拒绝,那么游戏就结束了。如果他们回答“是”,而新卡的总数大于21,则返回“破产”。如果总数变为21,则告诉玩家获胜。在每局游戏结束时,都应询问玩家是否愿意再次玩。
答案 0 :(得分:0)
您的逻辑存在问题。
首先,newTotal = getCard(total);
应该为total = getCard(total);
,以便更新total
(因此,您到处都应将newTotal
替换为total
)。通过使用此newTotal
变量,您总是可以丢弃while循环中的前一个总数,并从循环前定义为total = card1 + card2;
的那个开始。
第二,while (playAgain == 'y')
始终为真,因为您没有从此循环内部更新playAgain
(因此您永远不会到达询问用户是否要再次播放的代码)。在此循环内移动以下两行:
cout << "Would you like to play again? (y/n)\n";
cin >> playAgain;
像这样:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int getCard(int);
int main()
{
srand(time(0));
int total, card1, card2, newTotal = 0;
char choice, playAgain = 'y';
while (playAgain == 'y')
{
cout << "Welcome to Blackjack!\n";
card1 = rand() % 10 + 1;
card2 = rand() % 10 + 1;
total = card1 + card2;
cout << "Here are your first cards: " << card1 << ", " << card2 << endl;
cout << "Your total is " << total << "." << endl;
cout << "Would you like to draw another card? (y/n)\n";
cin >> choice;
while (choice == 'y')
{
total = getCard(total);
if (total > 21)
{
cout << "Bust!" << endl;
choice = 'n';
}
else if (total < 21)
{
cout << "Would you like to draw another card? (y/n)\n";
cin >> choice;
}
else
{
cout << "Congratulations, you won!\n";
choice = 'n';
}
}
cout << "Would you like to play again? (y/n)\n";
cin >> playAgain;
}
return 0;
}
int getCard(int total)
{
int addCard, newTotal;
addCard = rand() % 10 + 1;
newTotal = total + addCard;
cout << "You got a new card with a value of " << addCard << "." << endl;
cout << "Your total is now " << newTotal << "." << endl;
return newTotal;
}