Question: Im trying to make a c++ program to play the card game gin-rummy. the program will run one loop then give me a vector subscript
Im trying to make a c++ program to play the card game gin-rummy. the program will run one loop then give me a "vector subscript out of range " error, where is the problem?
main.cpp:
#include "deckOfCards.h" #include
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
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
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
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
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
