Simple Blackjack/21 game in console
up vote
1
down vote
favorite
I made a simple Blackjack/21 game in C++. It does not use any fancy graphics, just the console output. It is as simple as it can get.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
void printData(int pTotal, int dCard) {
std::cout << "nYour total is " << pTotal;
std::cout << "nDealer has a " << dCard << " showing.n";
}
int getCard() {
return std::rand() % 11 + 2;
}
void turn(int& pTotal, char choice, bool& stay) {
// If the user wants to hit
if (choice == 'H' || choice == 'h') {
// If the user hits 21 or above
if ((pTotal += getCard()) >= 21) {
std::cout << "Your total is " << pTotal;
stay = true;
}
return;
}
// Since the only other option can be to stay, then stay
stay = true;
}
int main() {
bool stay;
char choice = NULL;
while (true) {
std::cout << "================================";
stay = false;
// Initialize srand and get random card numbers
std::srand(std::time(0));
int dCard = getCard();
int pTotal = getCard() + getCard();
int dTotal = dCard + getCard();
// Player's turn
while (!stay) {
printData(pTotal, dCard);
std::cout << "[H]it or [S]tay?n";
std::cin >> choice;
turn(pTotal, choice, stay);
}
stay = false;
std::cout << "nnIt is now the dealer's turn!n";
// Dealer's turn
while (dTotal <= 21 && !stay) {
if (dTotal >= 17)
stay = true;
else
dTotal += getCard();
}
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
}
}
- Is this code efficient?
- Is the code easy to read/understand and to follow?
- Are variable names appropriate?
c++ c++11 game playing-cards
add a comment |
up vote
1
down vote
favorite
I made a simple Blackjack/21 game in C++. It does not use any fancy graphics, just the console output. It is as simple as it can get.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
void printData(int pTotal, int dCard) {
std::cout << "nYour total is " << pTotal;
std::cout << "nDealer has a " << dCard << " showing.n";
}
int getCard() {
return std::rand() % 11 + 2;
}
void turn(int& pTotal, char choice, bool& stay) {
// If the user wants to hit
if (choice == 'H' || choice == 'h') {
// If the user hits 21 or above
if ((pTotal += getCard()) >= 21) {
std::cout << "Your total is " << pTotal;
stay = true;
}
return;
}
// Since the only other option can be to stay, then stay
stay = true;
}
int main() {
bool stay;
char choice = NULL;
while (true) {
std::cout << "================================";
stay = false;
// Initialize srand and get random card numbers
std::srand(std::time(0));
int dCard = getCard();
int pTotal = getCard() + getCard();
int dTotal = dCard + getCard();
// Player's turn
while (!stay) {
printData(pTotal, dCard);
std::cout << "[H]it or [S]tay?n";
std::cin >> choice;
turn(pTotal, choice, stay);
}
stay = false;
std::cout << "nnIt is now the dealer's turn!n";
// Dealer's turn
while (dTotal <= 21 && !stay) {
if (dTotal >= 17)
stay = true;
else
dTotal += getCard();
}
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
}
}
- Is this code efficient?
- Is the code easy to read/understand and to follow?
- Are variable names appropriate?
c++ c++11 game playing-cards
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I made a simple Blackjack/21 game in C++. It does not use any fancy graphics, just the console output. It is as simple as it can get.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
void printData(int pTotal, int dCard) {
std::cout << "nYour total is " << pTotal;
std::cout << "nDealer has a " << dCard << " showing.n";
}
int getCard() {
return std::rand() % 11 + 2;
}
void turn(int& pTotal, char choice, bool& stay) {
// If the user wants to hit
if (choice == 'H' || choice == 'h') {
// If the user hits 21 or above
if ((pTotal += getCard()) >= 21) {
std::cout << "Your total is " << pTotal;
stay = true;
}
return;
}
// Since the only other option can be to stay, then stay
stay = true;
}
int main() {
bool stay;
char choice = NULL;
while (true) {
std::cout << "================================";
stay = false;
// Initialize srand and get random card numbers
std::srand(std::time(0));
int dCard = getCard();
int pTotal = getCard() + getCard();
int dTotal = dCard + getCard();
// Player's turn
while (!stay) {
printData(pTotal, dCard);
std::cout << "[H]it or [S]tay?n";
std::cin >> choice;
turn(pTotal, choice, stay);
}
stay = false;
std::cout << "nnIt is now the dealer's turn!n";
// Dealer's turn
while (dTotal <= 21 && !stay) {
if (dTotal >= 17)
stay = true;
else
dTotal += getCard();
}
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
}
}
- Is this code efficient?
- Is the code easy to read/understand and to follow?
- Are variable names appropriate?
c++ c++11 game playing-cards
I made a simple Blackjack/21 game in C++. It does not use any fancy graphics, just the console output. It is as simple as it can get.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
void printData(int pTotal, int dCard) {
std::cout << "nYour total is " << pTotal;
std::cout << "nDealer has a " << dCard << " showing.n";
}
int getCard() {
return std::rand() % 11 + 2;
}
void turn(int& pTotal, char choice, bool& stay) {
// If the user wants to hit
if (choice == 'H' || choice == 'h') {
// If the user hits 21 or above
if ((pTotal += getCard()) >= 21) {
std::cout << "Your total is " << pTotal;
stay = true;
}
return;
}
// Since the only other option can be to stay, then stay
stay = true;
}
int main() {
bool stay;
char choice = NULL;
while (true) {
std::cout << "================================";
stay = false;
// Initialize srand and get random card numbers
std::srand(std::time(0));
int dCard = getCard();
int pTotal = getCard() + getCard();
int dTotal = dCard + getCard();
// Player's turn
while (!stay) {
printData(pTotal, dCard);
std::cout << "[H]it or [S]tay?n";
std::cin >> choice;
turn(pTotal, choice, stay);
}
stay = false;
std::cout << "nnIt is now the dealer's turn!n";
// Dealer's turn
while (dTotal <= 21 && !stay) {
if (dTotal >= 17)
stay = true;
else
dTotal += getCard();
}
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
}
}
- Is this code efficient?
- Is the code easy to read/understand and to follow?
- Are variable names appropriate?
c++ c++11 game playing-cards
c++ c++11 game playing-cards
edited Feb 14 '16 at 0:40
Jamal♦
30.2k11115226
30.2k11115226
asked Feb 14 '16 at 0:18
Greg M
21138
21138
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
I want my money back!
Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s
It is now the dealer's turn!
The player has 15.
The dealer has 23. <<=====
The dealer wins! <<=====
Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard
function can only return 11 different values (2 to 12).
This means that you can lose right off the bat:
$ ./a.out
================================
Your total is 24 <<====== :-(
Dealer has a 9 showing.
[H]it or [S]tay?
In turn
, if the player enters h
and doesn't bust, you're not setting stay
- this happens to work given the code flow, but it looks odd. Set it before returning.
If the user enters gibberish, you always interpret that as stay. That's not user-friendly.
In main
:
bool stay;
char choice = NULL;
That's not a valid initializer for a char
- chars are not pointers. And leaving a variable (stay
) uninitialized is not a good idea. You should move stay
inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)
Remove choice
from main altogether. Only turn
needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)
This shouldn't be inside the loop:
std::srand(std::time(0));
You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.
These lines are too long:
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
Try something like:
// Display winner
std::cout << "nnThe player has " << pTotal
<< ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
std::cout << "The player wins!n";
} else {
std::cout << "The dealer wins!n";
}
And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs
since both scores will be at most 21.
add a comment |
up vote
1
down vote
Your deck isn't fair
This code will happily deal a card that's already been dealt:
int getCard() {
return std::rand() % 11 + 2;
}
Real Blackjack uses a finite set of cards, usually a fixed number of standard decks shuffled together. The code here is equivalent to dealing from an infinite deck (or to replacing cards to be drawn again).
The difference may well be important to anyone using this program to practice their card-counting technique.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I want my money back!
Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s
It is now the dealer's turn!
The player has 15.
The dealer has 23. <<=====
The dealer wins! <<=====
Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard
function can only return 11 different values (2 to 12).
This means that you can lose right off the bat:
$ ./a.out
================================
Your total is 24 <<====== :-(
Dealer has a 9 showing.
[H]it or [S]tay?
In turn
, if the player enters h
and doesn't bust, you're not setting stay
- this happens to work given the code flow, but it looks odd. Set it before returning.
If the user enters gibberish, you always interpret that as stay. That's not user-friendly.
In main
:
bool stay;
char choice = NULL;
That's not a valid initializer for a char
- chars are not pointers. And leaving a variable (stay
) uninitialized is not a good idea. You should move stay
inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)
Remove choice
from main altogether. Only turn
needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)
This shouldn't be inside the loop:
std::srand(std::time(0));
You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.
These lines are too long:
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
Try something like:
// Display winner
std::cout << "nnThe player has " << pTotal
<< ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
std::cout << "The player wins!n";
} else {
std::cout << "The dealer wins!n";
}
And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs
since both scores will be at most 21.
add a comment |
up vote
4
down vote
accepted
I want my money back!
Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s
It is now the dealer's turn!
The player has 15.
The dealer has 23. <<=====
The dealer wins! <<=====
Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard
function can only return 11 different values (2 to 12).
This means that you can lose right off the bat:
$ ./a.out
================================
Your total is 24 <<====== :-(
Dealer has a 9 showing.
[H]it or [S]tay?
In turn
, if the player enters h
and doesn't bust, you're not setting stay
- this happens to work given the code flow, but it looks odd. Set it before returning.
If the user enters gibberish, you always interpret that as stay. That's not user-friendly.
In main
:
bool stay;
char choice = NULL;
That's not a valid initializer for a char
- chars are not pointers. And leaving a variable (stay
) uninitialized is not a good idea. You should move stay
inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)
Remove choice
from main altogether. Only turn
needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)
This shouldn't be inside the loop:
std::srand(std::time(0));
You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.
These lines are too long:
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
Try something like:
// Display winner
std::cout << "nnThe player has " << pTotal
<< ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
std::cout << "The player wins!n";
} else {
std::cout << "The dealer wins!n";
}
And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs
since both scores will be at most 21.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I want my money back!
Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s
It is now the dealer's turn!
The player has 15.
The dealer has 23. <<=====
The dealer wins! <<=====
Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard
function can only return 11 different values (2 to 12).
This means that you can lose right off the bat:
$ ./a.out
================================
Your total is 24 <<====== :-(
Dealer has a 9 showing.
[H]it or [S]tay?
In turn
, if the player enters h
and doesn't bust, you're not setting stay
- this happens to work given the code flow, but it looks odd. Set it before returning.
If the user enters gibberish, you always interpret that as stay. That's not user-friendly.
In main
:
bool stay;
char choice = NULL;
That's not a valid initializer for a char
- chars are not pointers. And leaving a variable (stay
) uninitialized is not a good idea. You should move stay
inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)
Remove choice
from main altogether. Only turn
needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)
This shouldn't be inside the loop:
std::srand(std::time(0));
You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.
These lines are too long:
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
Try something like:
// Display winner
std::cout << "nnThe player has " << pTotal
<< ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
std::cout << "The player wins!n";
} else {
std::cout << "The dealer wins!n";
}
And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs
since both scores will be at most 21.
I want my money back!
Your total is 15
Dealer has a 4 showing.
[H]it or [S]tay?
s
It is now the dealer's turn!
The player has 15.
The dealer has 23. <<=====
The dealer wins! <<=====
Your casino is cheating! Also you're not handling aces, all face cards are supposed to have a value of 10, and there are 13 ranks in a suit but your getCard
function can only return 11 different values (2 to 12).
This means that you can lose right off the bat:
$ ./a.out
================================
Your total is 24 <<====== :-(
Dealer has a 9 showing.
[H]it or [S]tay?
In turn
, if the player enters h
and doesn't bust, you're not setting stay
- this happens to work given the code flow, but it looks odd. Set it before returning.
If the user enters gibberish, you always interpret that as stay. That's not user-friendly.
In main
:
bool stay;
char choice = NULL;
That's not a valid initializer for a char
- chars are not pointers. And leaving a variable (stay
) uninitialized is not a good idea. You should move stay
inside the loop, only declare it when you give it its first value. (I'd also use two separate variables for the dealer and the player.)
Remove choice
from main altogether. Only turn
needs it, so handle the user input in there completely. (And have turn return hit/stay so you don't need that out parameter.)
This shouldn't be inside the loop:
std::srand(std::time(0));
You should initialize the RNG only once, do that at the very start of main. C++11 introduced new random generation facilities, you should look into them.
These lines are too long:
// Display winner
std::cout << "nnThe player has " << pTotal << ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) std::cout << "The player wins!n";
else std::cout << "The dealer wins!n";
Try something like:
// Display winner
std::cout << "nnThe player has " << pTotal
<< ".nThe dealer has " << dTotal << ".nn";
if ((std::abs(21 - pTotal)) < (std::abs(21 - dTotal))) {
std::cout << "The player wins!n";
} else {
std::cout << "The dealer wins!n";
}
And once you fix your logic so that a bust directly ends the players turn, and a dealer bust makes the player win, you can get rid of the std::abs
since both scores will be at most 21.
answered Feb 14 '16 at 8:08
Mat
2,72511323
2,72511323
add a comment |
add a comment |
up vote
1
down vote
Your deck isn't fair
This code will happily deal a card that's already been dealt:
int getCard() {
return std::rand() % 11 + 2;
}
Real Blackjack uses a finite set of cards, usually a fixed number of standard decks shuffled together. The code here is equivalent to dealing from an infinite deck (or to replacing cards to be drawn again).
The difference may well be important to anyone using this program to practice their card-counting technique.
add a comment |
up vote
1
down vote
Your deck isn't fair
This code will happily deal a card that's already been dealt:
int getCard() {
return std::rand() % 11 + 2;
}
Real Blackjack uses a finite set of cards, usually a fixed number of standard decks shuffled together. The code here is equivalent to dealing from an infinite deck (or to replacing cards to be drawn again).
The difference may well be important to anyone using this program to practice their card-counting technique.
add a comment |
up vote
1
down vote
up vote
1
down vote
Your deck isn't fair
This code will happily deal a card that's already been dealt:
int getCard() {
return std::rand() % 11 + 2;
}
Real Blackjack uses a finite set of cards, usually a fixed number of standard decks shuffled together. The code here is equivalent to dealing from an infinite deck (or to replacing cards to be drawn again).
The difference may well be important to anyone using this program to practice their card-counting technique.
Your deck isn't fair
This code will happily deal a card that's already been dealt:
int getCard() {
return std::rand() % 11 + 2;
}
Real Blackjack uses a finite set of cards, usually a fixed number of standard decks shuffled together. The code here is equivalent to dealing from an infinite deck (or to replacing cards to be drawn again).
The difference may well be important to anyone using this program to practice their card-counting technique.
answered 6 hours ago
Toby Speight
23.2k638110
23.2k638110
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f119915%2fsimple-blackjack-21-game-in-console%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown