Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Im trying to make a c++ program to play the card game gin-rummy. the program wil

ID: 3827352 • Letter: I

Question

Im trying to make a c++ program to play the card game gin-rummy. the program will run three loops then give me a "vector subscript out of range " error, where is the problem?

main.cpp:

#include "deckOfCards.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

int userdead = 0;
int result = 0;
int result1 = 0;
int result2 = 0;
int input2;
int pie;
int input;
int compDead = 0;

deckOfCards deck;
card currentCard;

vector<string> playerHand;
vector<string> computerHand;
vector<string> stack;

void playGin(void);

int returnResult();
int returnComp();
int returnStack();
int discard();
int valueofCard(card temp_card)
{
   int temp = 1 + temp_card.value % 13;
   if (temp>10)
       temp = 10;
   return temp;

}

void main() {
   char keepPlaying = 'n'; //loop control variable

   do
   {
       playGin();

       //keep playing?
       cout << "Do you want to play another hand (y/n)?";
       cin >> keepPlaying;
   } while (keepPlaying == 'Y' || keepPlaying == 'y');
}
void playGin(void)
{
       deck.shuffle();
       deck.dealCard();
       currentCard = deck.dealCard();
       for( int x = 1; x < 53; x++)
       {
           if (x < 12) {
               currentCard = deck.dealCard();
               string arrayCard = currentCard.print();
               playerHand.push_back(arrayCard);
               //cout << playerHand[x] << " placement: " << x + 1 << " ";
           }
           else if ((x > 11) && (x < 23))
           {
               currentCard = deck.dealCard();
               string arrayCard = currentCard.print();
               computerHand.push_back(arrayCard);
               //cout << computerHand[x] << " placement: " << x + 1 << " ";
           }
           else
           {
               currentCard = deck.dealCard();
               string arrayCard = currentCard.print();
               stack.push_back(arrayCard);
           }

       }
      
      
returnResult();
//returnComp();
//returnStack();
discard();

       system("Pause");
}
          

int returnResult()
{
   cout << "Your cards:" << endl;;
       for (int j = 0; j < 10; j++)
       {
               cout << playerHand[j] << " |user|" << j+1<< endl;
          
      
       }
       cout << endl;
   return result;
}

int returnComp()
{
   cout << "Computer Cards:" << endl;
   for (int j = 0; j < 10; j++)
   {
       cout << computerHand[j] << " |comp|" << j + 1 << endl;
   }
   cout << endl;
   return result1;
}
int returnStack()
{
  
       cout << stack[1] << " |stack|" << endl;
  
   return result2;
}

int discard()
{
  
   for (int i = 1; i < 53; i++)
   {
       int AI = pow(-1, i);
       if (AI > 0)
       {
           cout << "input a number 1-10 " << endl;
           cin >> input;
      
           vector<string> temp = playerHand;
           cout << temp[input] << endl;
              
           playerHand.erase(playerHand.end() - input);
           /*void moveItemToBack(std::vector<T>& v, size_t itemIndex)
           {computerHand
               auto it = v.begin() + itemIndex;
               std::rotate(it, it + 1, v.end());
           }*/
       //discards.push_back(playerHand[input]);
       //discards.erase(discards.begin() + input);
       //playerHand.erase(playerHand.end() - input);
           temp.clear();
           returnResult();
       }
       else if (AI < 0)
       {
           /*string x;
           input2 = rand() % 11;
           cout << "input2 ="<< input2 << endl;
           for (auto it = computerHand.begin(); it != computerHand.end(); ++it) {
               if ( x =! (*it) computerHand) {
                  
                   x = *it; // or std::move(*it)
                   computerHand.erase(it);
                   computerHand.insert(computerHand.begin(), x /* or std::move(x) );
                   break;
               }*/
           }
           /*for (int j = 0; j < discards.size(); j++)
           {
               cout << iscards[j] << " |stack|" << endl;
           }*/
   }

  
   returnComp();
  
   /*v = orig;
   v.reserve(v.size() * 2);
   v.insert(v.end(), v.begin(), v.end());*/
   return 0;
}
          
//////////////////////////////////////////////////////////////////

card.h:

#ifndef H_card
#define H_card
#include <string>
#include <iostream>

using namespace std;

class card {
public:
   card(string cardFace, string cardSuit, int i);
   string print() const;
   card();
   int value;

private:
   string face;
   string suit;
};
card::card()
{
}


card::card(string cardFace, string cardSuit, int i) {
   face = cardFace;
   suit = cardSuit;
   value = i;
}
string card::print() const {
   return (face + " of " + suit);
}
#endif // !H_card

//////////////////////////////////////

deckofcards.h:

#ifndef H_deckOfCards
#define H_deckOfCards
#include "card.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>

using namespace std;

const int CARDS_PER_DECK = 52;

class deckOfCards {
public:
   deckOfCards();
   void shuffle();
   card dealCard();
   void printDeck() const;

private:
   card *deck;
   int currentCard;
};

void deckOfCards::printDeck() const {
   cout << left;
   for (int i = 0; i < CARDS_PER_DECK; i++) {
       cout << setw(19) << deck[i].print();
       if ((i + 1) % 4 == 0)
           cout << endl;
   }
}

deckOfCards::deckOfCards() {
   string faces[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
   string suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
   deck = new card[CARDS_PER_DECK];
   currentCard = 0;
   for (int i = 0; i < CARDS_PER_DECK; i++) //populate deck in order
       deck[i] = card(faces[i % 13], suits[i / 13], i);
}

void deckOfCards::shuffle() {
   currentCard = 0;
   for (int first = 0; first < CARDS_PER_DECK; first++)
   {
       int second = (rand() + time(0)) % CARDS_PER_DECK;
       card temp = deck[first];
       deck[first] = deck[second];
       deck[second] = temp; //
   }
}
card deckOfCards::dealCard() {
   if (currentCard > CARDS_PER_DECK)
       shuffle();
   if (currentCard < CARDS_PER_DECK)
       return (deck[currentCard++]);
   return (deck[0]);
}

#endif

Explanation / Answer

//Highlighted the problems in bold

#include "deckOfCards.h"
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
int userdead = 0;
int result = 0;
int result1 = 0;
int result2 = 0;
int input2;
int pie;
int input;
int compDead = 0;
deckOfCards deck;
card currentCard;
vector<string> playerHand;
vector<string> computerHand;
vector<string> stack;
void playGin(void);
int returnResult();
int returnComp();
int returnStack();
int discard();
int valueofCard(card temp_card)
{
   int temp = 1 + temp_card.value % 13;
   if (temp>10)
       temp = 10;
   return temp;
}
void main() {
   char keepPlaying = 'n'; //loop control variable
   do
   {
       playGin();
       //keep playing?
       cout << "Do you want to play another hand (y/n)?";
       cin >> keepPlaying;
   } while (keepPlaying == 'Y' || keepPlaying == 'y');
}
void playGin(void)
{
   deck.shuffle();
   deck.dealCard();
   currentCard = deck.dealCard();
   for (int x = 1; x < 53; x++)
   {
       if (x < 12) {
           currentCard = deck.dealCard();
           string arrayCard = currentCard.print();
           playerHand.push_back(arrayCard);
           //cout << playerHand[x] << " placement: " << x + 1 << " ";
       }
       else if ((x > 11) && (x < 23))
       {
           currentCard = deck.dealCard();
           string arrayCard = currentCard.print();
           computerHand.push_back(arrayCard);
           //cout << computerHand[x] << " placement: " << x + 1 << " ";
       }
       else
       {
           currentCard = deck.dealCard();
           string arrayCard = currentCard.print();
           stack.push_back(arrayCard);
       }
   }


   returnResult();
   //returnComp();
   //returnStack();
   discard();
   system("Pause");
}

int returnResult()
{
   cout << "Your cards:" << endl;;
   for (int j = 0; j < playerHand.size(); j++)
   {
       cout << playerHand[j] << " |user|" << j + 1 << endl;


   }
   cout << endl;
   return result;
}
int returnComp()
{
   cout << "Computer Cards:" << endl;
   for (int j = 0; j < 10; j++)
   {
       cout << computerHand[j] << " |comp|" << j + 1 << endl;
   }
   cout << endl;
   return result1;
}
int returnStack()
{

   cout << stack[1] << " |stack|" << endl;

   return result2;
}
int discard()
{

   for (int i = 1; i < 53; i++)
   {
       int AI = pow(-1, i);
       if (AI > 0)
       {
           cout << "input a number 1-10 " << endl;
           cin >> input;

           vector<string> temp = playerHand;
           cout << temp[input-1] << endl;

           playerHand.erase(playerHand.begin() +input-1);
           /*void moveItemToBack(std::vector<T>& v, size_t itemIndex)
           {computerHand
           auto it = v.begin() + itemIndex;
           std::rotate(it, it + 1, v.end());
           }*/
           //discards.push_back(playerHand[input]);
           //discards.erase(discards.begin() + input);
           //playerHand.erase(playerHand.end() - input);
           temp.clear();
           returnResult();
       }
       else if (AI < 0)
       {
           /*string x;
           input2 = rand() % 11;
           cout << "input2 ="<< input2 << endl;
           for (auto it = computerHand.begin(); it != computerHand.end(); ++it) {
           if ( x =! (*it) computerHand) {

           x = *it; // or std::move(*it)
           computerHand.erase(it);
           computerHand.insert(computerHand.begin(), x /* or std::move(x) );
           break;
           }*/
       }
       /*for (int j = 0; j < discards.size(); j++)
       {
       cout << iscards[j] << " |stack|" << endl;
       }*/
   }

   returnComp();

   /*v = orig;
   v.reserve(v.size() * 2);
   v.insert(v.end(), v.begin(), v.end());*/
   return 0;
}