For my display functions in my Blackjack game, I want to control each player's stats output based on whether a turn is in progress or has ended. Normally, you could only see one card that your opponent(s) hold, while the rest are only revealed at the end of each turn. My code does that well, but I'm having trouble making it more concise.
The display function in Game
is called to display each player's stats (name, chips, and hand). It takes a Boolean argument to determine if it should display each CPU's partial stats (during each turn) or full stats (after each turn). At the function call in Game
, the bool
argument is passed to the respective display function in Player, where the human player and CPU player have individual display procedures. The display functions in Player
primarily display the chip amounts and card rank totals. They also call respective Hand
display functions which, again, solely depend on the Boolean argument for displaying the CPUs' stats.
How could I simplify this code to achieve the same output (shown below the code)?
Game.cpp
displayStats(false); // function call elsewhere in Game
void Game::displayStats(bool showCPUResults) const
{
players[0].displayPlayerStats(); // displays player stats
for (int i = 1; i < numPlrs; i++)
players[i].displayCPUStats(showCPUResults); // displays CPU stats
}
Player.cpp
void Player::displayPlayerStats() const
{
// always shown
std::stringstream ss;
std::cout << "\n* " << name << ": ";
std::cout << "($" << chips << ") ";
// displays two-digit card rank total (always shown)
if (totalCardsValue <= 9)
ss << "(0" << totalCardsValue << ") ";
else
ss << "(" << totalCardsValue << ") ";
std::cout << ss.str();
// displays human player's hand
playerHand[0].displayPlayerHand();
}
void Player::displayCPUStats(bool showResults) const
{
// always shown
std::stringstream ss;
std::cout << "\n* " << name << ": ";
std::cout << "($" << chips << ") ";
if (!showResults)
{
std::cout << " ";
playerHand[0].displayCPUHand(showResults);
}
else
{
// only shown after each turn
if (totalCardsValue <= 9)
ss << "(0" << totalCardsValue << ") ";
else
ss << "(" << totalCardsValue << ") ";
std::cout << ss.str();
// displays CPU's hand
playerHand[0].displayCPUHand(showResults);
}
}
Hand.cpp
void Hand::displayPlayerHand() const
{
// displays player's full hand (always)
for (unsigned i = 0; i < cards.size(); i++)
std::cout << cards[i] << " ";
}
void Hand::displayCPUHand(bool showCPUHand) const
{
// displays CPU's first card (always)
std::cout << cards[0] << " ";
// displays the rest of the CPU's cards (only after each turn)
if (showCPUHand)
{
for (unsigned i = 1; i < cards.size(); i++)
std::cout << cards[i] << " ";
}
}
Output during a turn...
Jamal: ($1000) (23) [8C] [6H] [9C]
CPU1: ($1000) [AD]
...and after a turn...
Jamal: ($1000) (23) [8C] [6H] [9C]
CPU1: ($1000) (21) [AD] [KH]