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; EgAS for Ace of Spades
CardNode next;
CardNodestring v : valuev nextnullptr
;
Deck class
class Deck
CardNode top;
public:
Deck : topnullptr
initializeDeck;
shuffle;
void initializeDeck
const string suits CDHS; Clubs, Diamonds, Hearts, Spades
const string valuesJQKA;
for char suit : suits
for const string& value : values
string card value suit;
CardNode newCard new CardNodecard;
newCardnext top;
top newCard;
void shuffle
Shuffle logic: convert to array, shuffle, convert back to linked list
vector cards;
CardNode current top;
while current
cards.pushbackcurrent;
current currentnext;
srandtime;
for int i ; i cards.size; i
int j rand cards.size;
swapcardsi cardsj;
top cards;
current top;
for int i ; i cards.size; i
currentnext cardsi;
current currentnext;
currentnext nullptr;
CardNode drawCard
if top return nullptr;
CardNode drawnCard top;
top topnext;
drawnCardnext nullptr;
return drawnCard;
;
Player class
class Player
CardNode hand;
public:
Player : handnullptr
void addCardCardNode card
if hand
hand card;
else
cardnext hand;
hand card;
void discardCardCardNode& river
if hand return;
CardNode discard hand;
hand handnext;
discardnext river;
river discard;
int calculateScore
int score ;
CardNode current hand;
while current
score getCardValuecurrentvalue;
current currentnext;
return score;
private:
int getCardValuestring card
if cardA return ;
if cardK cardQ cardJ card return ;
return ;
;
Game class
class Game
Deck deck;
Player player player;
CardNode river;
public:
Game : rivernullptr
dealInitialCards;
void dealInitialCards
for int i ; i ; i
playeraddCarddeckdrawCard;
playeraddCarddeckdrawCard;
void playRound
Basic round implementation
while true
takeTurnplayer;
if checkWinplayer break;
takeTurnplayer;
if checkWinplayer break;
determineWinner;
private:
void takeTurnPlayer& player
player.addCarddeckdrawCard;
player.discardCardriver;
bool checkWinPlayer& player
return player.calculateScore;
void determineWinner
int score playercalculateScore;
int score playercalculateScore;
if score score cout "Player wins with score points!
;
else cout "Player wins with score points!
;
;
int main
Game rupeeGame;
rupeeGame.playRound;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
