Question: When I attempt to run the code that will be provided below, it gives me the following error: Unhandled exception thrown: write access violation. *

When I attempt to run the code that will be provided below, it gives me the following error: Unhandled exception thrown: write access violation. **card** was nullptr.
Can you fix my code to make it function as it needs to?
Here's the code:
#include
#include
#include
#include
#include
using namespace std;
// Card Node structure
struct CardNode {
string value; // E.g.,"AS" for Ace of Spades
CardNode* next;
CardNode(string v) : value(v), next(nullptr){}
};
// Deck class
class Deck {
CardNode* top;
public:
Deck() : top(nullptr){
initializeDeck();
shuffle();
}
void initializeDeck(){
const string suits ="CDHS"; // Clubs, Diamonds, Hearts, Spades
const string values[]={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (char suit : suits){
for (const string& value : values){
string card = value + suit;
CardNode* newCard = new CardNode(card);
newCard->next = top;
top = newCard;
}
}
}
void shuffle(){
// Shuffle logic: convert to array, shuffle, convert back to linked list
vector cards;
CardNode* current = top;
while (current){
cards.push_back(current);
current = current->next;
}
srand(time(0));
for (int i =0; i < cards.size(); ++i){
int j = rand()% cards.size();
swap(cards[i], cards[j]);
}
top = cards[0];
current = top;
for (int i =1; i < cards.size(); ++i){
current->next = cards[i];
current = current->next;
}
current->next = nullptr;
}
CardNode* drawCard(){
if (!top) return nullptr;
CardNode* drawnCard = top;
top = top->next;
drawnCard->next = nullptr;
return drawnCard;
}
};
// Player class
class Player {
CardNode* hand;
public:
Player() : hand(nullptr){}
void addCard(CardNode* card){
if (!hand){
hand = card;
}
else {
card->next = hand;
hand = card;
}
}
void discardCard(CardNode*& river){
if (!hand) return;
CardNode* discard = hand;
hand = hand->next;
discard->next = river;
river = discard;
}
int calculateScore(){
int score =0;
CardNode* current = hand;
while (current){
score += getCardValue(current->value);
current = current->next;
}
return score;
}
private:
int getCardValue(string card){
if (card[0]=='A') return 15;
if (card[0]=='K'|| card[0]=='Q'|| card[0]=='J'|| card[0]=='1') return 10;
return 5;
}
};
// Game class
class Game {
Deck deck;
Player player1, player2;
CardNode* river;
public:
Game() : river(nullptr){
dealInitialCards();
}
void dealInitialCards(){
for (int i =0; i <7; ++i){
player1.addCard(deck.drawCard());
player2.addCard(deck.drawCard());
}
}
void playRound(){
// Basic round implementation
while (true){
takeTurn(player1);
if (checkWin(player1)) break;
takeTurn(player2);
if (checkWin(player2)) break;
}
determineWinner();
}
private:
void takeTurn(Player& player){
player.addCard(deck.drawCard());
player.discardCard(river);
}
bool checkWin(Player& player){
return player.calculateScore()>=500;
}
void determineWinner(){
int score1= player1.calculateScore();
int score2= player2.calculateScore();
if (score1> score2) cout << "Player 1 wins with "<< score1<<" points!
";
else cout << "Player 2 wins with "<< score2<<" points!
";
}
};
int main(){
Game rupeeGame;
rupeeGame.playRound();
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!