To achieve the desired formatting in the console output, you'll want to add some blank lines (using cout) at the appropriate points in the game's output. Here's the updated version of the game that includes the new formatting:
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <map>
using namespace std;
enum Rank { TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
struct Card {
Rank rank;
string toString() const {
switch (rank) {
case TWO: return "Two";
case THREE: return "Three";
case FOUR: return "Four";
case FIVE: return "Five";
case SIX: return "Six";
case SEVEN: return "Seven";
case EIGHT: return "Eight";
case NINE: return "Nine";
case TEN: return "Ten";
case JACK: return "Jack";
case QUEEN: return "Queen";
case KING: return "King";
case ACE: return "Ace";
}
return "Unknown";
}
};
class Player {
public:
vector<Card> hand;
int books = 0;
void addCard(Card card) {
hand.push_back(card);
}
bool hasRank(Rank rank) const {
for (auto &card : hand) {
if (card.rank == rank) {
return true;
}
}
return false;
}
vector<Card> giveCards(Rank rank) {
vector<Card> cardsGiven;
hand.erase(remove_if(hand.begin(), hand.end(), [&](Card& card) {
if (card.rank == rank) {
cardsGiven.push_back(card);
return true;
}
return false;
}), hand.end());
return cardsGiven;
}
void checkForBook() {
map<Rank, int> rankCount;
for (auto &card : hand) {
rankCount[card.rank]++;
}
for (auto &entry : rankCount) {
if (entry.second == 4) {
books++;
hand.erase(remove_if(hand.begin(), hand.end(), [entry](Card& card) {
return card.rank == entry.first;
}), hand.end());
cout << "Book completed for " << entry.first << "!" << endl;
}
}
}
};
class Game {
public:
vector<Card> deck;
Player user;
Player computer;
Game() {
for (int i = TWO; i <= ACE; i++) {
for (int j = 0; j < 4; j++) {
deck.push_back(Card{static_cast<Rank>(i)});
}
}
srand(static_cast<unsigned int>(time(0)));
random_shuffle(deck.begin(), deck.end());
for (int i = 0; i < 7; i++) {
user.addCard(deck.back());
deck.pop_back();
computer.addCard(deck.back());
deck.pop_back();
}
}
void displayUserHand() const {
cout << "Your hand:";
for (auto &card : user.hand) {
cout << " " << card.toString();
}
cout << endl;
}
void playerTurn() {
displayUserHand();
cout << "Ask for a rank (2-10, J, Q, K, A): ";
char rankInput;
cin >> rankInput;
Rank askedRank = charToRank(rankInput);
while (!user.hasRank(askedRank)) {
cout << "You don't have any of that rank. Ask again." << endl;
cout << "Ask for a rank (2-10, J, Q, K, A): ";
cin >> rankInput;
askedRank = charToRank(rankInput);
}
if (computer.hasRank(askedRank)) {
auto cards = computer.giveCards(askedRank);
for (auto &card : cards) {
user.addCard(card);
}
cout << "\nYou captured " << cards.size() << " cards!\n" << endl;
} else {
cout << "\nGo fish!" << endl;
if (!deck.empty()) {
user.addCard(deck.back());
deck.pop_back();
}
cout << endl;
}
user.checkForBook();
}
void computerTurn() {
if (computer.hand.empty()) return;
Rank rankToAsk = computer.hand[rand() % computer.hand.size()].rank;
cout << "Computer asks for " << rankToAsk << endl;
if (user.hasRank(rankToAsk)) {
auto cards = user.giveCards(rankToAsk);
for (auto &card : cards) {
computer.addCard(card);
}
cout << "Computer captured " << cards.size() << " cards!\n" << endl;
} else {
cout << "Computer goes fish!" << endl;
if (!deck.empty()) {
computer.addCard(deck.back());
deck.pop_back();
}
cout << endl;
}
computer.checkForBook();
}
void run() {
while (!deck.empty()) {
playerTurn();
if (deck.empty()) break;
computerTurn();
}
cout << "Game over!" << endl;
cout << "Your books: " << user.books << endl;
cout << "Computer books: " << computer.books << endl;
if (user.books > computer.books) {
cout << "You win!" << endl;
} else if (user.books < computer.books) {
cout << "Computer wins!" << endl;
} else {
cout << "It's a draw!" << endl;
}
}
private:
Rank charToRank(char c) const {
switch (c) {
case '2': return TWO;
case '3': return THREE;
case '4': return FOUR;
case '5': return FIVE;
case '6': return SIX;
case '7': return SEVEN;
case '8': return EIGHT;
case '9': return NINE;
case '1': return TEN; // assume input '1' is for "Ten"
case 'J':
case 'j': return JACK;
case 'Q':
case 'q': return QUEEN;
case 'K':
case 'k': return KING;
case 'A':
case 'a': return ACE;
default: return TWO; // Default to TWO if input is invalid
}
}
};
int main() {
Game game;
game.run();
return 0;
}
Changes:
- I've added blank lines (
\n) to enhance readability between different game states:
- After confirming the player's successful capture of cards.
- After the computer's turn has a result (either capturing cards or going fish).
- Before displaying the player's hand at the beginning of their turn.
This should help make the output clearer, as it provides visual separation between actions and updates. Feel free to adjust the placement and number of blank lines (\n) if you prefer a different structure.