vector - Help With Small C++ Game -


i'm writing game of solitaire runs in terminal. of now, program compiles , runs , gives player 4 cards, placed in 4 columns , allows them press 0 more cards, added onto columns. column grows, it's output should placed in vector (actually, vector of vectors).

ideally, after gathering more cards if necessary, player inputs number ranging 1-4 select column they'd compare others. should compare top card of column other top cards , see if 1 can deleted. part i'm having trouble with. first of all, i'm not sure if i'm inputing cards correctly vector of vectors , i'm not sure how compare them each other. i've tried using like:

column[2].back().getsuit() acces suit of top card of column two, giving numerical value , comparing suit of other. did similar thing compare ranks of cards i'm not having luck.

can show me example using or own code? how should compare suit , rank of top cards in each column?

here code far:

#include <iostream> #include <algorithm> #include <vector> #include <cstdlib> #include <ctime>  using namespace std;  enum suits  {     diamond, club, heart, spade };  class card { private:     int rank;     suits suit; public:     card();     card(suits, int);     int getrank() { return rank; }     suits getsuit() { return suit; }     void setrank(int rankvalue) { rank = rankvalue; }     void setsuit(suits suitvalue) { suit = suitvalue; } };  ostream & operator<<(ostream &, card);  card::card() {     rank = 1;     suit = spade; }  card::card(suits suitvalue, int rankvalue) {     rank = rankvalue;     suit = suitvalue; }  ostream & operator<<(ostream & out, card acard) {     switch (int rank = acard.getrank())     {         case 14: out << "ace"; break;         case 11: out << "jack"; break;         case 12: out << "queen"; break;         case 13: out << "king"; break;         default: out << rank;     }      switch (suits suit = acard.getsuit())     {         case diamond: out << " of diamonds"; break;         case spade: out << " of spades"; break;         case heart: out << " of hearts"; break;         case club: out << " of clubs"; break;     }      return out; }  class randominteger  { public:      randominteger();     unsigned int operator() (unsigned int max); };  randominteger::randominteger() {     srand(time(0)); }  unsigned int randominteger::operator()(unsigned int max)  {     unsigned int rval = rand();     return rval % max; }  randominteger randomizer;  class deck {     card cards[52];     int topcard; public:     deck();     void shuffle();     bool isempty() { return topcard <= 0; }     card draw(); };  extern randominteger randomizer;  deck::deck() {     topcard = 0;     (int = 1; <= 13; i++)     {         card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);         cards[topcard++] = c1;         cards[topcard++] = c2;         cards[topcard++] = c3;         cards[topcard++] = c4;     } }  card deck::draw() {     if (!isempty())         return cards[--topcard];     else     {         card spadeace(spade, 1);         return spadeace;     } }  void deck::shuffle() {     random_shuffle(cards, cards+52, randomizer); }  class player { public:     player();     void print();     card draw(deck &);     typedef vector<card> cards;     vector<cards> column; };  //ostream & operator<<(ostream &, player&);  player::player() {     column.push_back(vector<card>());     column.push_back(vector<card>());     column.push_back(vector<card>());     column.push_back(vector<card>()); }  card player::draw(deck & adeck) {     (int = 0; < 4; i++)         column[i].push_back(adeck.draw()); }  void player::print() {     cout << "col 1 \t\t col 2 \t\t col 3 \t\t col 4 \n";     bool more = true;     (int j = 0; more; j++)     {         more = false;         (int = 0; < 4; i++)             if (j < column[i].size())         {         cout << column[i][j] << "\t";         more = true;         }             else             cout << "\t\t";     cout << endl;     } }  int main() {     deck deck;     deck.shuffle();      player player;     player.draw(deck);     //while (!deck.isempty())     //{     cout << "enter column number (0 draw 4 new cards): " << endl;     //}     player.print();      int input;     int i;     vector<vector<card> > columns(4);     while (cin >> input)     if (input == 0 )     {         player.draw(deck);         player.print();         columns.push_back(vector<card>());         columns.push_back(vector<card>());     columns.push_back(vector<card>());         columns.push_back(vector<card>());     }         else while (cin >> input)             if (input == 1)             {             ( = 0; > 4; i++)                 {                         columns.push_back(vector<card>());             }             ( = 0; > 4; i++)                     {                     columns[0].back().getsuit();                 columns[1].back().getsuit();                 columns[2].back().getsuit();                 columns[3].back().getsuit();             }              }    } 

any suggestions, pointers, tips, great, thanks.

you don't need translate suit numeric value comparison. can compare enumerated types directly.

try adding comparison operator cards:

bool operator==(const card& a, const card& b) {   return a.getrank() == b.getrank() && a.getsuit() == b.getsuit(); } 

for work, you'll have mark getrank , getsuit methods const:

int getrank() const { return rank; } suits getsuit() const { return suit; } 

now should able compare 2 cards, including on "top" of 2 columns. example:

if (columns[1].back() == columns[2].back()) { ... } 

note back won't work if vector empty.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -