I am a new student learning C++ for the first time. I am trying to write a code to play simplified form of Blackjack. I have run into a bit of a problem with my code.
I have written the Blackjack program and it seems to work fine 95% of the time. However, the code gets stuck in an infinite loop seemingly at random.
Here is the issue:
I have created a simple boolean function to check whether or not the user-inputted bet value is acceptable. However, after multiple hands, the infinite loop will appear even though an acceptable value has been entered. Sometimes, this problem will not surface at all (I have played out 20+ hands without any issue.) Thus, I have not been able to determine what exactly is prompting this issue to arise.
Here is the portion of the code that is giving me the issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
//FUNCTION THAT CHECKS FOR A VALID BET
bool check_bet (int bank, int bet)
{
if ((bet > 0) && (bet <= bank))
return false;
else if (bet <= 0)
{
cout << "You must bet at least $1!\n" << endl;
return true;
}
else
{
cout << "You cannot bet that much!\n" << endl;
return true;
}
}
//MAIN BLACKJACK FUNCTION
int main()
{
srand( (int)time(0) ); //Allows for random numbers to be chosen
//DECLARATION OF VARIABLES
int bank = 277; //The player starts with $100
int bet = 0;
string hit;
int total = 0;
string card1, card2, card;
bool bust = false;
int d_total = 0;
string d_card1, d_card2, d_card;
bool d_bust = false;
//LOOP FOR EACH HAND
while ((bank > 0) && (bank <1000))
{
cout << "You have $" << bank << ".\n\n";
//ENTER BET AND CHECK IF VALID
do
{
cout << "Please enter your bet: $";
cin >> bet;
}while (check_bet(bank, bet));
//DEAL THE DEALER'S HAND
d_total += draw_card (d_card1, d_total);
d_total += draw_card (d_card2, d_total);
//DISPLAY THE DEALER'S UPCARD
cout << "\nThe dealer's upcard is the " << d_card1 <<".\n";
//DEAL THE PLAYER'S HAND
total += draw_card (card1,total);
total += draw_card (card2,total);
//DISPLAY THE PLAYER'S HAND
cout << endl << "You have been dealt:" << endl;
cout << "\t" << card1 << endl << "\t" << card2 << endl;
cout << "Your total is: " << endl << "\t" << total << endl << endl;
//ALLOW THE PLAYER TO HIT / STAND
do
{
cout << "Would you like to hit (Y/N)? ";
cin >> hit;
//DRAW THE PLAYER'S NEXT CARD
if ((hit == "Y") || (hit == "y"))
{
if (total < 21)
{
total += draw_card (card,total);
cout << endl << "You have been dealt:\n";
cout << "\t" << card << endl;
card = "";
}
if (total > 21)
{
cout << endl << "You bust.\n\n";
bust = true;
bank -= bet;
}
else
cout << "Your total is:\n\t" << total << endl << endl;
}
}while ((total < 21) && (hit == "y"));
if (bust == false)
{
//DEAL THE REST OF THE DEALER'S HAND
cout << endl << "The dealer's hole card is the " << d_card2 << "." << endl;
cout << "The dealer's total is: " << endl << "\t" << d_total << endl << endl;
if (d_total > 16)
cout << "The dealer stands.\n\n";
while (d_total <= 16)
{
d_total += draw_card (d_card,total);
cout << "The dealer draws the " << d_card << ".\n";
cout << "The dealer's total is: " << endl << "\t" << d_total << endl << endl;
d_card = "";
if ((d_total > 16) && (d_total <= 21))
cout << "The dealer stands.\n\n";
else if (d_total > 21)
{
cout << "The dealer busts!\n\n";
d_bust = true;
}
}
}
//CHECK THE HAND'S RESULTS AND PAYOUT/COLLECT THE BET
if ((d_total > total) && ((d_bust == false) && (bust == false)))
{
cout << "The dealer's " << d_total << " beats your " << total <<".\n";
cout << "You lose this hand.\n\n";
bank -= bet;
}
else if ((d_total < total) && ((d_bust == false) && (bust == false)))
{
cout << "Your " << total << " beats the dealer's " << d_total <<".\n";
cout << "You win this hand!\n\n";
bank += bet;
}
else if ((d_total == total) && ((d_bust == false) && (bust == false)))
{
cout << "You push.\n";
cout << "You get back your $" << bet << " bet.\n\n";
}
else if (d_bust == true)
{
cout << "You win this hand!\n\n";
bank += bet;
}
//CHECK THE PLAYER'S PAYROLL
if (bank <= 0)
{
cout << "You are out of money.\n";
cout << "Thanks for playing.\n\n";
}
else if (bank >= 1000)
{
cout << "You have $" << bank << "!\n";
cout << "You are over the table's limit.\n";
cout << "Thanks for playing!\n\n";
}
//RESET VARIABLES
bet = 0;
card = "";
card1 = "";
card2 = "";
total = 0;
bust = false;
d_card = "";
d_card1 = "";
d_card2 = "";
d_total = 0;
d_bust = false;
}
return 0;
}
|
For a better understanding of the glitch, below is a sample run in which the problem arose:
You have $277.
Please enter your bet: $-2
You must bet at least $1!
Please enter your bet: $4
The dealer's upcard is the Ace of Diamonds.
You have been dealt:
Four of Clubs
Eight of Spades
Your total is:
12
Would you like to hit (Y/N)? y
You have been dealt:
Jack of Hearts
You bust.
You have $273.
Please enter your bet: $3
The dealer's upcard is the Six of Clubs.
You have been dealt:
Ten of Hearts
Five of Diamonds
Your total is:
15
Would you like to hit (Y/N)? n
The dealer's hole card is the Seven of Clubs.
The dealer's total is:
13
The dealer draws the Queen of Hearts.
The dealer's total is:
23
The dealer busts!
You win this hand!
You have $276.
Please enter your bet: $15
You must bet at least $1!
Please enter your bet: $You must bet at least $1!
Please enter your bet: $You must bet at least $1!
...
I have spent about two hours trying to fix this glitch, and I can't seem to figure it out. If anybody could shed any light on this problem I would be greatly appreciative!