patterncppMinor
21 game similar to Blackjack
Viewed 0 times
similargameblackjack
Problem
I just finished coding a game of 21, similar to Blackjack, but with a simplified dealing mechanism.
#include
using namespace std;
#include
#include
#include
int main()
{
bool loop = true;
int loopchoice;
srand(time(0));
int player1 = 1+(rand()%10);
int player2 = 1+(rand()%10);
int banker1 = 1+(rand()%10);
int banker2 = 1+(rand()%10);
int playertotal = player1 + player2;
int bankertotal = banker1 + banker2;
int choice;
int playerOVERWRITE;
int bankerOVERWRITE;
bool loop1 = true;
bool loop2 = true;
cout > choice;
while( loop1 == true ){
if( choice == 1 ){
playerOVERWRITE = 1+(rand()%10);
playertotal = playertotal + playerOVERWRITE;
cout 21 ){
loop1 = false;
cout > choice;
if( choice == 2 ){
loop1 = false;
cout 21 ){
cout 18 && bankertotal bankertotal ){
cout << "You win!" << endl;
cout << "Your total was " << playertotal << endl;
cout << "Bankers total was " << bankertotal << endl;
cout << "Banker started with a " << banker1 << " and a " << banker2 << endl;
cout << " " << endl;
return 0;
}
else if( playertotal == bankertotal ) {
cout << "It's a draw! Banker wins!" << endl;
cout << "Your total was " << playertotal << endl;
cout << "Bankers total was " << bankertotal << endl;
cout << "Banker started with a " << banker1 << " and a " << banker2 << endl;
cout << " " << endl;
return 0;
}
else {
cout << "Banker wins!" << endl;
cout << "Your total was " << playertotal << endl;
cout << "Bankers total was " << bankertotal << endl;
cout << "Banker started with a " << banker1 << " and a " << banker2 << endl;
cout << " " << endl;
return 0;
}
}Solution
The Player Loop
What we have here is an operation that we want to run once, and then repeat as necessary. That calls for a
The Banker Loop
While for the player, we loop based on the player choices, for the dealerbanker we loop based on a concrete condition: while he's not in range [18, 21]. We don't need an additional variable for this:
At the end of this loop, the banker either busted or stuck.
Naming
There are lots of different naming conventions in C++, the most common of which are
Next, when you find yourself naming things
This way you can display all the cards that each player had.
What we have here is an operation that we want to run once, and then repeat as necessary. That calls for a
do...while loop rather than a normal while loop. This will avoid you having to retype your prompt:do {
int choice;
cout > choice;
if (choice == 1) {
...
}
else if (choice == 2) {
break; // <== this is how we terminate the loop in the non-bust case
}
else {
// maybe print some sort of error here?
}
} while (true);The Banker Loop
While for the player, we loop based on the player choices, for the dealerbanker we loop based on a concrete condition: while he's not in range [18, 21]. We don't need an additional variable for this:
while (banker_total < 18) {
int next = draw_card();
banker_total += next;
// ...
}At the end of this loop, the banker either busted or stuck.
Naming
There are lots of different naming conventions in C++, the most common of which are
camelCase and snake_case. I don't have a strong preference between the two, but both are strongly preferred to noseparationwhatsoever. Something like bankertotal is much harder to read than either bankerTotal or banker_total, so prefer to use one or the other.Next, when you find yourself naming things
player1 and player2 - consider that there are two cases: 1 thing and N things. You either need to hold onto one card, or lots of cards. Since you need more than one, it's better to simply have:std::vector player_cards;
std::vector banker_cards;This way you can display all the cards that each player had.
Code Snippets
do {
int choice;
cout << "Would you like to 1: Twist or 2: Stick " << endl;
cin >> choice;
if (choice == 1) {
...
}
else if (choice == 2) {
break; // <== this is how we terminate the loop in the non-bust case
}
else {
// maybe print some sort of error here?
}
} while (true);while (banker_total < 18) {
int next = draw_card();
banker_total += next;
// ...
}std::vector<int> player_cards;
std::vector<int> banker_cards;Context
StackExchange Code Review Q#113816, answer score: 6
Revisions (0)
No revisions yet.