Question: Why isnt this code running? I belive itd have to due with the saveResultsToFile at the end of the play game. Ive been trying to

Why isnt this code running?
I belive itd have to due with the "saveResultsToFile" at the end of the play game. Ive been trying to make multiple, CardGame.h, CardGame.cpp, main.cpp and a README.txt which isnt already included since im not able to make mutiples files and get the code running. if you could explain the process of which ive shown im trying to get to id need id appreciate it.
#include
#include
#include
#include
#include
#include
#include
class CardGame
{
private:
std::vector deck;
int userScore;
int dealerScore;
public:
CardGame();
void shuffleDeck();
int drawCard();
void playGame();
void saveResultsToFile(const std::string &filename);
};
//Constructor to initialize the game
CardGame::CardGame() : userScore(0), dealerScore(0)
{
//Initialize the deck with values from 1 to 10(representing card values)
for(int i =1; i <=10; i++)
{
deck.push_back(i);
deck.push_back(i);
}
}
//Shuffle the deck using a random generator
void CardGame::shuffleDeck()
{
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(deck.begin(), deck.end(), g);
}
//Function to draw a card from the deck
int CardGame::drawCard()
{
if(deck.empty())
{
std::cout << "The deck is empty!" << std::endl;
return 0;
}
int card = deck.back();
deck.pop_back();
return card;
}
//Main game logic
void CardGame::playGame()
{
shuffleDeck();
//user's turn
std::cout << "Your turn!" << std::endl;
char choice;
while(userScore <21)
{
int card = drawCard();
userScore += card;
std::cout << "You drew a card with value: "<< card << std::endl;
std::cout << "Your current score: "<< userScore << std::endl;
if(userScore >21)
{
std::cout << "exceeded 21! You lose!" << std::endl;
return;
}
std::cout <<"Do you want to draw another card? (y/n): ";
std::cin >> choice;
if(choice !='y')
{
break;
}
}
//Dealer's turn
std::cout << "Dealer's turn!" << std::endl;
while(dealerScore <17)
{
int card = drawCard();
dealerScore += card;
std::cout << "Dealer drew a card with value: "<< card << std::endl;
std::cout << "Dealer's current score: "<< dealerScore << std:: endl;
if(dealerScore >21)
{
std::cout << "dealer exceed 21! you win!" << std::endl;
return;
}
}
//Determine winner
if(userScore > dealerScore)
{
std::cout << "Congradulation! You win!" << std::endl;
}
else if(userScore == dealerScore)
{
std::cout << "It's a tie!" << std::endl;
}
else
{
std::cout << "Dealer wins! Better luck next time." << std::endl;
}
//Save game results to a file
void CardGame::saveResultsToFile(const std::string &filename)
{
std::ofstream file(filename);
if(file.is_open())
{
file << "Game results:
";
file << "User Score: "<< userScore <<"
";
file << "Dealer Score: "<< dealerScore <<"
";
file.close();
std::cout << "Results saved to "<< filename << std::endl;
}
else
{
std::cout << "Failed to open file!" << std::endl;
}
}
}
int main()
{
CardGame game; //Create an instance of the game
game.playGame(); //Play game
game.saveResultsToFile("game_results.txt"); //save results to a file
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!