samedi 25 avril 2020

Creating a 'crazy eights' style game but having trouble updating my hands

I am creating a program that lets a person play the card game crazy eights vs. the computer for a class and I am in running into several problems trying to add cards from the deck to the player's hand. my hand class is

class Hand {
  private:
    Card* cards;
    int n_cards;  // Number of cards in the hand.
  public:
Hand();
~Hand();
Hand(const Hand&);
Hand& operator= (const Hand&);
void set_cards(int n, Card card);
void set_n_cards(const int n_cards);
Card get_cards(int i);
int get_n_cards();
void increase_n_cards();
bool ifPlayable(int cardNum, Card& topPile);
void isEight(Card&);
void playCard(int index);
};

and I am trying to add cards using:

      void Hand::set_cards(int n, Card card)

{

    Card** temp = &cards;
Card* new_hand = new Card[n_cards+1];

for(int i = 0; i < n_cards; i++)
{
new_hand[i].setCard((*temp)[i].getSuit(),(*temp)[i].getRank());//copy over the hands

}

new_hand[n_cards].setCard(card.getSuit(),card.getRank());//add the new card from deck

delete[] *temp;//delete the pointer and set it to the new deck
*temp = nullptr;
*temp = new_hand;
n_cards++;
}

First, I am getting a seg fault trying to delete temp that says "invalid delete", and I am confused why. I am also getting seg faults when trying to access the card array but I think that might be because n_cards is not updating. It keeps getting reset to 0 and I have looked at all the obvious(at least to me) places and can't find a reason. I am new to coding and am doing this for an assignment. Thanks!

Aucun commentaire:

Enregistrer un commentaire