954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

CARD GAME-HW assignment hell

In this program we are going to simulate a simple card game. The rules are that the player selects a card out of a deck of 52 cards. The computer picks a random card out of the deck. The person with the highest card face number wins.

In this program it would be very helpful to use the examples of card games in the book. This will give you a majority of the code to setup this program. Then you need to add your own classes and code.

You must have 2 classes in your program. One class will be an individual card. The second class will be the game, which must contain an array of cards (your card class). You will need to shuffle the deck of cards each time the player plays the game. Make main() as small as possible and write most of your code in your objects. You must use constructors to setup your cards and your deck of cards. The computer player will select a random card from the deck of cards. You must not allow the player and the computer to choose the same card. Warn the user and get a new selection from them if they select an illegal card from the deck.

You must use classes and objects correctly to get full credit. Using any global variables will result in a 0 for the assignment. If your program does not compile, it will result in a 0 for the assignment.

Sample Output:

Welcome to Highest Card Wins. You will pick a card in the deck
and I will pick a card out of the deck. The player with the
highest card face value will win!

What card out of the deck would you like to choose? (1 to 52) 0
Sorry, that is not a valid card number.

Would you like to play again? (Y/N) y

What card out of the deck would you like to choose? (1 to 52) 5

Your card is: 7 of spades
My card is : 7 of diamonds
It's a tie.

Would you like to play again? (Y/N) y

What card out of the deck would you like to choose? (1 to 52) 50

Your card is: 3 of spades
My card is : 4 of hearts
Sorry you lose.

Would you like to play again? (Y/N) y

What card out of the deck would you like to choose? (1 to 52) 21

Your card is: jack of clubs
My card is : 10 of spades
You win!

Would you like to play again? (Y/N) n


//***********************************************

#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;

// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;          //from 2 to 10 are
const int queen = 12;         //integers without names
const int king = 13;
const int ace = 14;

// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
   {
   private:
      int number;             //2 to 10, jack, queen, king, ace
      Suit suit;
                    //clubs, diamonds, hearts, spades
   public:

      card ()                 //constructor (no args)
         {  }
                              //constructor (two args)
      void set (int n, Suit s)
         {  number = n; suit = s; }
      void display();         //display card

     // bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //display the card
   {
   if( number >= 2 && number <= 10 )
      cout << number;
   else
      switch(number)
         
         {
         case jack:
              cout << "J";
              break;
         case queen:
              cout << "Q";
              break;
         case king:
              cout << "K";
              break;
         case ace:
              cout << "A";
              break;
         }
         
   switch(suit)
      {
         case clubs:
              cout << static_cast<char>(5);
              break;
         case diamonds:
              cout << static_cast<char>(4);
              break;
         case hearts:
              cout << static_cast<char>(3);
              break;
         case spades:
              cout << static_cast<char>(6);
              break;
      }
   }
//--------------------------------------------------------------
 //ORDERED DECK AND SHUFFLE DECK
/////////////////////////////////////////
class deck
    {
    private:
         int k;
         int j;
    public:

         deck ()                 //constructor (no args)
            {  }
    void shuffle(); //shuffle and
    void game();  //play.

    };
//---------------------------------------------------------------------------------------------
void deck::shuffle() // creates an ordered deck and shuffles it.
    {
	    card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
        for (j=0; j<52; j++) //ordered deck
               {
               int num = (j%13)+2;
               Suit su = Suit (j /13);
               deck[j].set(num, su);
               }
        srand(time(NULL)+_getpid());  // initialize seed "randomly"
        for(j=0; j<52; j++)
               {
      		   int k = rand()%52;
               card temp = deck[j];
		       deck[j] = deck[k];
		       deck[k] = temp;
               }
/*        for(j=0; j<52; j++)//garbage code..sets the card's faces and repeats until all cards are displayed
		       {//garbage code..
			   deck[j].display();
			   cout<< ", ";//garbage code
			   if (!((j+1) % 13))//newline every thirteen...... garbage code
                      cout << endl;  //garbage code..
               }
*/
      }
//---------------------------------------------------------------------------------------------
void deck:game()
     {
     for(j=0; j<52; j++)//garbage code..sets the card's faces and repeats until all cards are displayed
		 {//garbage code..
         deck[j].display();
         cout<< ", ";//garbage code
		 if (!((j+1) % 13))//newline every thirteen...... garbage code
                      cout << endl;  //garbage code..
         }
     }

//---------------------------------------------------------------------------------------------
/*class game
      {
      private:

      public:
         game ()                 //constructor (no args)
            {  }
      }; */
//---------------------------------------------------------------------------------------------
int main()
      {
        deck.shuffle(); //creates and shuffles deck  which calls class "card"

        system("pause");
        return 0;



      }


what I am confused about is....
what do I need to put in main(), is it just class::function calls?
the compare card and the player /computers card is going to be assigned by class "card" according to the assignment, and I could only get it to work in class "deck".
Then there is the issue about passing the player's/npc's card object values from the card class to the deck class... and if we could move the deck::shuffle() to card, it would be better... but i couldn't figure out how to get the array values of "card deck[j]" build within the card class, only main and in my new deck class.

no *pointers yet..... we haven't got that far
and never any global variables( instructor says "Auto-F" on assignment)

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks for elaborating on your question. You need to step back from this and consider the design aspects. Deck should contain an array of card objects. In the constructor for deck you can simply use a loop to deal the cards into your deck (since you are going to shuffle them anyway).

Then there is the issue about passing the player's/npc's card object values from the card class to the deck class... and if we could move the deck::shuffle() to card, it would be better... but i couldn't figure out how to get the array values of "card deck[j]" build within the card class, only main and in my new deck class.

I think you're going in circles a bit. You should leave the deck as a class and create a new one called game (which is called for in the spec). See if you can map out the members of the game class (hint, you'll need a deck and two players).

In main you should instantiate just the game object. You can't use the :: in this case because your methods aren't static.

Take another crack at it and get little pieces of it working first. Then come back and post what you have finished at that point.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
Ok. What's the question?


make the card deck[52] work in class "card"?

to test thus far my conversion of deck class. from what use to be main() (that was to test the cards to see if the shuffled and displayed the characters correctly) and if as a class now they still do.

also giving a random card to a player and computer for comparison and not letting them be equal... if equal the program has to issue a new card. (has to be in class card; according to assignment guidelines.)...
then passing that to the deck class....

(that once we figure out how to construct the whole deck and deal the cards with class card, will be renamed to game like the assignment stated.)

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Thanks for elaborating on your question. You need to step back from this and consider the design aspects. Deck should contain an array of card objects. In the constructor for deck you can simply use a loop to deal the cards into your deck (since you are going to shuffle them anyway).

I think you're going in circles a bit. You should leave the deck as a class and create a new one called game (which is called for in the spec). See if you can map out the members of the game class (hint, you'll need a deck and two players).

In main you should instantiate just the game object. You can't use the :: in this case because your methods aren't static.

Take another crack at it and get little pieces of it working first. Then come back and post what you have finished at that point.

okidoky, thanks :D

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

See my edit above. It doesn't say you can't have a deck class, it just says you must have a card and a game class.

Forget about getting deck to work in card. Deck should be it's own entity. Card doesn't know deck even exists and it should stay that way. Even if you think it should not have it's own class, at least make it an entity in the game class.

EDIT: Think of it this way: If you're going to design a new game, you should be able to take your card class and reuse it. You can reuse your deck too (or retool a new deck with a different number of cards for a different game).

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

this might seem strange to ask
but I never made anything get a random index out of an array
would it look like

int r = rand()%52;// assigning a # (1-52) and storing it

now how to retrieve that random index from the array?

playercard = deck[j].  //something to do with 'r'?????????
tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

this might seem strange to ask but I never made anything get a random index out of an array would it look like

int r = rand()%52;// assigning a # (1-52) and storing it

now how to retrieve that random index from the array?


You've got the right statement but your comment is incorrect. It will give you a value from 0 to 51 which is what you want.

playercard = deck[j].  //something to do with 'r'?????????

deck[r] will work.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

You've got the right statement but your comment is incorrect. It will give you a value from 0 to 51 which is what you want.

deck[r] will work.


thanks I keep forgetting computers start with 0 when I comment
so just defining....

playercard=deck[r];

....will just choose 1 random number from deck[j] or creates a new deck with an array [j]?

Thus far I do awesome in this college course, but I got a bad flu and missed enough days to confuse me. The textbook is so vague about it, and never even says how to get a random index# from an array. the only sample we had showed the class "card" and how to assign values and shuffle it. I had to tweak it immensely already. Thank you again.

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

thanks I keep forgetting computers start with 0 when I comment so just defining....

playercard=deck[r];

....will just choose 1 random number from deck[j] or creates a new deck with an array [j]?


That's just going to get you the one card after you've called r = rand() etc. This assumes you already have the deck within whatever object you've constructed. You'll only want to make a new deck in between "rounds." For the second draw, act as though the first player kept the card.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
That's just going to get you the one card after you've called r = rand() etc. This assumes you already have the deck within whatever object you've constructed. You'll only want to make a new deck in between "rounds." For the second draw, act as though the first player kept the card.

Ah I see.... this way it is less likely to have the same card chosen by the randomizer. this is what I use to ready the rand()

#include <process.h>
#include<ctime>


int main()
      {

       srand(time(NULL)+_getpid()); // I was told that using the pid of the computer would give me better results.
       //etc code.
       }

but alas the time+pid still didn't give me a very random num then. but my instructor told me that it should be initialized in main() not my function (last assignment), because it kept resetting the randomizer. Made my craps game not so random.... it was funny because iSum=idie1+1die2.... the dice would be random, but the total would repeat like it was factoring the total... strange...
I noticed while I researched that classes get encapsulated as defined headers.... so does that mean, that the class can actually initiate the randomized code in a class function, called only once by the main()would still work, because the examples I found did it, or would it not be a good idea? (in a professional POV?)

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
That's just going to get you the one card after you've called r = rand() etc. This assumes you already have the deck within whatever object you've constructed. You'll only want to make a new deck in between "rounds." For the second draw, act as though the first player kept the card.


note I know my comments are not aligned right. I just wrote them here in this post window.
ok....

class game
      {
      private:
         int player; //player's card
         int npc; // computer's card
      public:
         game ()                 //constructor (no args)
            {  }
            void deal();
      };
//---------------------------------------------------------------------------------------------
void game::deal();// to deal cards to players, take and store discarded card numbers, (so they cannot be drawn again) and exculding those numbers from the deck.....?
     {
     int turn=1; //turncounter.
     if (turn=1; turn<53; turn++) //deals first hand. with turncount limited by card #
        {
        int p = rand()%52;//assigning random # to player's card
        int n = rand()%52;//assigning random # to computer's card
        player = deck[p]; //selecting that card
        npc = deck[n];
        }
     if (turn > 1)//takes card and stores it away.
        {
        int temp_d[52] = p; //array of 52 empty zones for discard pile and the players current card #.
        // NPC discard next
        //then here I need to know how to exclude the newly stored array numbers from the next deck draw.
         }

//not finished...  this will be next turn, so I will only have to use one function to play.
        {
        p = rand()%52;
        n = rand()%52;
        player = deck[p];
        npc = deck[n];
        }

//______
tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Ah I see.... this way it is less likely to have the same card chosen by the randomizer. this is what I use to ready the rand()

#include <process.h>
#include<ctime>


int main()
      {

       srand(time(NULL)+_getpid()); // I was told that using the pid of the computer would give me better results.
       //etc code.
       }

but alas the time+pid still didn't give me a very random num then. but my instructor told me that it should be initialized in main() not my function (last assignment), because it kept resetting the randomizer. Made my craps game not so random.... it was funny because iSum=idie1+1die2.... the dice would be random, but the total would repeat like it was factoring the total... strange... I noticed while I researched that classes get encapsulated as defined headers.... so does that mean, that the class can actually initiate the randomized code in a class function, called only once by the main()would still work, because the examples I found did it, or would it not be a good idea? (in a professional POV?)


srand needs only to be called once. I suppose you could call it in the constructor of your class if you were getting random numbers in one of the methods. It's safe to do it at the top of main since nothing will be instantiated or called before then. I'm not completely sure what your average coding practice document would say on the subject. I've never done that thing with the pid. My guess is it wouldn't make an immense difference, but I don't have anything to back that up.note I know my comments are not aligned right. I just wrote them here in this post window.
ok....
I'll put my comments inline

class game
      {
      private:
         int player; //player's card     <strong>what is the datatype of a card?</strong>
         int npc; // computer's card
      public:
         game ()                 //constructor (no args)
            {  }
            void deal();
      };
//---------------------------------------------------------------------------------------------
void game::deal();// to deal cards to players, take and store discarded card numbers, (so they cannot be drawn again) and exculding those numbers from the deck.....?
     {
     int turn=1; //turncounter.
     if (turn=1; turn<53; turn++) //deals first hand. with turncount limited by card #
        {
        int p = rand()%52;//assigning random # to player's card
        int n = rand()%52;//assigning random # to computer's card
        player = deck[p]; //selecting that card
        npc = deck[n];    
        }//<strong>but what if n = p?  account for that</strong>
     if (turn > 1)//takes card and stores it away.
        {
        int temp_d[52] = p; //array of 52 empty zones for discard pile and the players current card #.
        // NPC discard next
        //then here I need to know how to exclude the newly stored array numbers from the next deck draw.
         }

//not finished...  this will be next turn, so I will only have to use one function to play.
        {
        p = rand()%52;
        n = rand()%52;
        player = deck[p];
        npc = deck[n];
        }

//______


Looking ok so far. However, if you represent a card as an int some information is lost. Also, what happens if the player and the computer choose the same card?

Think about breaking down your class into smaller pieces. Let each method do one job. As it stands you have deal() dealing the cards, playing a turn, and playing another turn. Do you see how you could simplify it. I was under the impression from your spec that you take a clean deck with each play, so you wouldn't have to worry about the cards from game to game.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

srand needs only to be called once. I suppose you could call it in the constructor of your class if you were getting random numbers in one of the methods. It's safe to do it at the top of main since nothing will be instantiated or called before then. I'm not completely sure what your average coding practice document would say on the subject. I've never done that thing with the pid. My guess is it wouldn't make an immense difference, but I don't have anything to back that up.

I'll put my comments inline Looking ok so far. However, if you represent a card as an int some information is lost. Also, what happens if the player and the computer choose the same card?

Think about breaking down your class into smaller pieces. Let each method do one job. As it stands you have deal() dealing the cards, playing a turn, and playing another turn. Do you see how you could simplify it. I was under the impression from your spec that you take a clean deck with each play, so you wouldn't have to worry about the cards from game to game.

#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;

// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;          //from 2 to 10 are
const int queen = 12;         //integers without names
const int king = 13;
const int ace = 14;

// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
   {
   protected:
      int number;             //2 to 10, jack, queen, king, ace
      Suit suit;
                    //clubs, diamonds, hearts, spades
   public:

      card ()                 //constructor (no args)
         {  }
                              //constructor (two args)
      void set (int n, Suit s)
         {  number = n; suit = s; }
      void display();         //display card
      //void display(int&);
      //friend class game;
      //friend class deck;


     // bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //display the card
   {
   if( number >= 2 && number <= 10 )
      cout << number;
   else
      switch(number)
         
         {
         case jack:
              cout << "J";
              break;
         case queen:
              cout << "Q";
              break;
         case king:
              cout << "K";
              break;
         case ace:
              cout << "A";
              break;
         }
         
   switch(suit)
      {
         case clubs:
              cout << static_cast<char>(5);
              break;
         case diamonds:
              cout << static_cast<char>(4);
              break;
         case hearts:
              cout << static_cast<char>(3);
              break;
         case spades:
              cout << static_cast<char>(6);
              break;
      }
   }
//--------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////
class deck
    {
    protected:
         int k;
         int j;
    public:

         deck ()                 //constructor (no args)
            {  }
    void shuffle(); //shuffle and
    //void game();  //play.


    };
//---------------------------------------------------------------------------------------------
void deck::shuffle() // creates an ordered deck and shuffles it.
    {
	    card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
        for (j=0; j<52; j++) //ordered deck
               {
               int num = (j%13)+2;
               Suit su = Suit (j /13);
               deck[j].set(num, su);
               }
        srand(time(NULL)+_getpid());  // initialize seed "randomly"
        for(j=0; j<52; j++)
               {
      		   int k = rand()%52;
               card temp = deck[j];
		       deck[j] = deck[k];
		       deck[k] = temp;
               }
      }
////////////////////////////////////////////////////////////////////////////////
class game
      {
      protected:
         int player; //player's card
         int npc; // computer's card

      public:
         game ()                 //constructor (no args)
            {  }
         void players (int p, int n)
             {  player = p; npc = n; }
         void play();
         //void discard(int);
         void draw(int);
         //friend void card::display();

      };
//---------------------------------------------------------------------------------------------
void game::play()
     {
     char cAns = 'y' ;
     int t = 1; //turncounter


     for (t=0; t<53; t++) //deals first hand. with turncount limited by card #
        {
        draw(player);
        draw(npc);
        if (player == npc)
           {
           draw(player);
           draw(npc);
           }

        }
     do
       {
       if (t > 1)//takes card and stores it away.
          {
          //discard(player);// discard player
          //discard(npc);// discard NPC
          draw(player);
          draw(npc);

          for(player == npc )  // if card has already been drawn
              {
              draw(player);
              draw(npc);
              }
          cout<< card::display(player)<<endl;
          cout<< card::display(npc)<<endl;
          }
       }
       while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
       cout<< "Play Again?" << endl;
       cin>> cAns;
       system("pause");
       if (cAns=='n' || cAns =='N')
          {
          return 0;
          }

    }


//---------------------------------------------------------------------------------------------
/*void game::discard(int d) // puts card on discard pile.
     {

     if(t==1; t++)
         {
         int top = -1;
         }
     else
         {
         int temp_d[52];//discard pile
         temp_d[top++] = var;
         }
     }   */
//---------------------------------------------------------------------------------------------
void game::draw(int var)//draws card off of deck.
     { return deck[top--];}
//---------------------------------------------------------------------------------------------



//______________________________________________________________________________________________
int main()
      {
      deck.shuffle(); //creates and shuffles deck  which calls class "card"
      game.play();
      return 0;
      }


161 untitled2.cpp
error: no matching function for call to `card::display(int)'
i could overload the function to make it work but I think it needs to be pass by reference rather than int (int&)...
what should I do next?

thanks again...

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

You need to take a quick look through your text in terms of declaring objects of a class. Unless display is a static method you can't say card::display(). Your main is going to be off too. You need an instance of game (e.g., game mygame; . Your game instance won't know about the deck as it is. I would make an instance of deck within game. That way the members of game can access it directly.

There's more confusion with your draw method. You are passing an integer to it that never gets used. In addition you still have cards as an int value. A card is of type card. Ideally you could also have a player class which holds one card but for now just draw off a card for each player to hold, then compare them.

I'll have to read the spec again, but as far as I can tell you can just shuffle the deck in between turns and start fresh.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

I see what you were saying about "card" I am getting an error message that I cannot compart an int to a card.. lol

#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;

// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;          //from 2 to 10 are
const int queen = 12;         //integers without names
const int king = 13;
const int ace = 14;

// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
   {
   protected:
      int number;             //2 to 10, jack, queen, king, ace
      Suit suit;
                    //clubs, diamonds, hearts, spades
   public:

      card ()                 //constructor (no args)
         {  }
                              //constructor (two args)
      void set (int n, Suit s)
         {  number = n; suit = s; }
      void display();         //display card
      void shuffle();


     // bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //display the card
   {
   if( number >= 2 && number <= 10 )
      cout << number;
   else
      switch(number)
         
         {
         case jack:
              cout << "J";
              break;
         case queen:
              cout << "Q";
              break;
         case king:
              cout << "K";
              break;
         case ace:
              cout << "A";
              break;
         }
         
   switch(suit)
      {
         case clubs:
              cout << static_cast<char>(5);
              break;
         case diamonds:
              cout << static_cast<char>(4);
              break;
         case hearts:
              cout << static_cast<char>(3);
              break;
         case spades:
              cout << static_cast<char>(6);
              break;
      }
   }
//--------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
class game
      {
      protected:
         int player; //player's card
         int npc; // computer's card
         int k;
         int j;
      public:
         game ()                 //constructor (no args)
            {  }
         void players (int p, int n)
             {  player = p; npc = n; }
         void play();
         //void discard(int);
         //void draw(int);
         //friend void card::display();

      };
//---------------------------------------------------------------------------------------------
void game::play()
     {
     char cAns = 'y' ;
     int t = 1; //turncounter
     card deck[52];// creates an multi dimensional-array of Objects with the Class "card"

     for (t=0; t<53; t++) //deals first hand. with turncount limited by card #
        {

        if (player == npc)
           {

           int p = rand()%52;//assigning random # to player's card
           int n = rand()%52;//assigning random # to computer's card
           player = deck[p]; //selecting that card
           npc = deck[n];
           }

        }
     do
       {
       if (t > 1)//takes card and stores it away.
          {
          p = rand()%52;
          n = rand()%52;
          player = deck[p];
          npc = deck[n];

          for (player == npc )  // if card has already been drawn
              {
              p = rand()%52;
              n = rand()%52;
              player = deck[p];
              npc = deck[n];
              }
          cout<< card::display(player)<<endl;
          cout<< card::display(npc)<<endl;
          }
       }
       while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
       cout<< "Play Again?" << endl;
       cin>> cAns;
       system("pause");
       if (cAns=='n' || cAns =='N')
          {
          return 0;
          }

    }


//---------------------------------------------------------------------------------------------
void game::shuffle() // creates an ordered deck and shuffles it.
    {
	   // card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
        for (j=0; j<52; j++) //ordered deck
               {
               int num = (j%13)+2;
               Suit su = Suit (j /13);
               deck[j].set(num, su);
               }
        srand(time(NULL)+_getpid());  // initialize seed "randomly"
        for(j=0; j<52; j++)
               {
      		   int k = rand()%52;
               card temp = deck[j];
		       deck[j] = deck[k];
		       deck[k] = temp;
               }
        return deck[j];
      }
//---------------------------------------------------------------------------------------------



//______________________________________________________________________________________________
int main()
      {
      game mygame;
      return 0;
      }

then I changed it to this

#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;

// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;          //from 2 to 10 are
const int queen = 12;         //integers without names
const int king = 13;
const int ace = 14;

// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
   {
   protected:
      int number;             //2 to 10, jack, queen, king, ace
      Suit suit;
                    //clubs, diamonds, hearts, spades
   public:

      card ()                 //constructor (no args)
         {  }
                              //constructor (two args)
      void set (int n, Suit s)
         {  number = n; suit = s; }
      void display();         //display card
      void shuffle();


     // bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //display the card
   {
   if( number >= 2 && number <= 10 )
      cout << number;
   else
      switch(number)
         
         {
         case jack:
              cout << "J";
              break;
         case queen:
              cout << "Q";
              break;
         case king:
              cout << "K";
              break;
         case ace:
              cout << "A";
              break;
         }
         
   switch(suit)
      {
         case clubs:
              cout << static_cast<char>(5);
              break;
         case diamonds:
              cout << static_cast<char>(4);
              break;
         case hearts:
              cout << static_cast<char>(3);
              break;
         case spades:
              cout << static_cast<char>(6);
              break;
      }
   }
//--------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
class game
      {
      protected:
         int player; //player's card
         int npc; // computer's card
         int k;
         int j;
         int p;
         int n;
      public:
         game ()                 //constructor (no args)
            {  }
         void players (int p, int n)
             {  player = p; npc = n; }
         void play();
         //void discard(int);
         //void draw(int);
         //friend void card::display();

      };
//---------------------------------------------------------------------------------------------
void game::play()
     {
     char cAns = 'y' ;
     int t = 1; //turncounter
     card deck[52];// creates an multi dimensional-array of Objects with the Class "card"

     for (t=0; t<53; t++) //deals first hand. with turncount limited by card #
        {

        if (p == n)
           {

           int p = rand()%52;//assigning random # to player's card
           int n = rand()%52;//assigning random # to computer's card
           player = deck[p]; //selecting that card
           npc = deck[n];
           }

        }
     do
       {
       if (t > 1)//takes card and stores it away.
          {
          p = rand()%52;
          n = rand()%52;
          player = deck[p];
          npc = deck[n];

          for (p == n )  // if card has already been drawn
              {
              p = rand()%52;
              n = rand()%52;
              player = deck[p];
              npc = deck[n];
              }
          cout<< card::display(player)<<endl;
          cout<< card::display(npc)<<endl;
          }
       }
       while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
       cout<< "Play Again?" << endl;
       cin>> cAns;
       system("pause");
       if (cAns=='n' || cAns =='N')
          {
          return 0;
          }

    }


//---------------------------------------------------------------------------------------------
void game::shuffle() // creates an ordered deck and shuffles it.
    {
	   // card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
        for (j=0; j<52; j++) //ordered deck
               {
               int num = (j%13)+2;
               Suit su = Suit (j /13);
               deck[j].set(num, su);
               }
        srand(time(NULL)+_getpid());  // initialize seed "randomly"
        for(j=0; j<52; j++)
               {
      		   int k = rand()%52;
               card temp = deck[j];
		       deck[j] = deck[k];
		       deck[k] = temp;
               }
        return deck[j];
      }
//---------------------------------------------------------------------------------------------



//______________________________________________________________________________________________
int main()
      {
      game mygame;
      return 0;
      }


and the error message is now....
123 untitled2.cpp
error: cannot convert `card' to `int' in assignment

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 
<em><strong>int</strong></em> player; //player's <strong><em>card</em></strong>
int npc; // computer's card

and what is the datatype of deck[p]? (the compiler is giving you a hint)
See post #12 again.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
<em><strong>int</strong></em> player; //player's <strong><em>card</em></strong>
int npc; // computer's card

and what is the datatype of deck[p]? (the compiler is giving you a hint) See post #12 again.

random = arr[rand() % (sizeof(arr) / sizeof(arr[0]))];


is what I found searching the web for pulling out something random from an array.... I am not quite what "arr" needs to be replaced with, but it might work better than my int to card fiasco.

also this was in a separate card game in my textbook.... (one that compared cards but had no objects nor arrays)

void card::isEqual(card c2)  // from 244 of textbook.
     {
     return (number == c2.number && suit==c2.suit )? true: false;     }
     }


which probably would work better than the comparison method I was trying before...

if (p == n)
//or
if(player=npc)
tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

You're not understanding my point. The way you pick numbers is fine (that text example is just getting the elements in the array by dividing the total size by the first element). Your player and npc need to be of type card.

card player;
card npc;

then the types match up perfectly. I'm not sure how the int crept into things... lol.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

You're not understanding my point. The way you pick numbers is fine (that text example is just getting the elements in the array by dividing the total size by the first element). Your player and npc need to be of type card.

card player;
card npc;

then the types match up perfectly. I'm not sure how the int crept into things... lol.

I did understand... and I tried it again so I can copy the error msgs I am getting LOL

#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;

// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11;          //from 2 to 10 are
const int queen = 12;         //integers without names
const int king = 13;
const int ace = 14;

// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
   {
   protected:
      int number;             //2 to 10, jack, queen, king, ace
      Suit suit;
                    //clubs, diamonds, hearts, spades
   public:

      card ()                 //constructor (no args)
         {  }
                              //constructor (two args)
      void set (int n, Suit s)
         {  number = n; suit = s; }
      void display();         //display card


      //bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //display the card
   {
   if( number >= 2 && number <= 10 )
      cout << number;
   else
      switch(number)
         
         {
         case jack:
              cout << "J";
              break;
         case queen:
              cout << "Q";
              break;
         case king:
              cout << "K";
              break;
         case ace:
              cout << "A";
              break;
         }
         
   switch(suit)
      {
         case clubs:
              cout << static_cast<char>(5);
              break;
         case diamonds:
              cout << static_cast<char>(4);
              break;
         case hearts:
              cout << static_cast<char>(3);
              break;
         case spades:
              cout << static_cast<char>(6);
              break;
      }
   }
//--------------------------------------------------------------
/*void card::isEqual(card c2)  // from 244 of textbook.
     {
     return (number == c2.number && suit==c2.suit )? true: false;     }
     }  */

////////////////////////////////////////////////////////////////////////////////
class game
      {
      protected:
         //int player; //player's card
         //int npc; // computer's card
         card player;  //defined as card now
         card npc;
         int k;
         int j;
         int p;
         int n;
      public:
         game ()                 //constructor (no args)
            {  }
         void players (int p, int n)
             {  player = p; npc = n; }
         void play();
         void shuffle();

         //void discard(int);
         //void draw(int);
         //friend void card::display();

      };
//---------------------------------------------------------------------------------------------
void game::play()
     {
     char cAns = 'y' ;
     int t = 1; //turncounter
     card deck[52];// creates an multi dimensional-array of Objects with the Class "card"

     for (t=0; t<53; t++) //deals first hand. with turncount limited by card #
        {

        if (p == n)
           {

           int p = rand()%52;//assigning random # to player's card
           int n = rand()%52;//assigning random # to computer's card
           player = deck[p]; //selecting that card
           npc = deck[n];
           }

        }
     do
       {
       if (t > 1)//takes card and stores it away.
          {
          p = rand()%52;
          n = rand()%52;
          player = deck[p];
          npc = deck[n];

          for (p == n )  // if card has already been drawn
              {
              p = rand()%52;
              n = rand()%52;
              player = deck[p];
              npc = deck[n];
              }
          cout<< card::display(player)<<endl;
          cout<< card::display(npc)<<endl;
          }
       }
       while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
       cout<< "Play Again?" << endl;
       cin>> cAns;
       system("pause");
       if (cAns=='n' || cAns =='N')
          {
          return 0;
          }

    }


//---------------------------------------------------------------------------------------------
//--------------------------------------------------------------
void game::shuffle() // creates an ordered deck and shuffles it.
    {
	   // card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
        for (j=0; j<52; j++) //ordered deck
               {
               int num = (j%13)+2;
               Suit su = Suit (j /13);
               deck[j].set(num, su);
               }
        srand(time(NULL)+_getpid());  // initialize seed "randomly"
        for(j=0; j<52; j++)
               {
      		   int k = rand()%52;
               card temp = deck[j];
		       deck[j] = deck[k];
		       deck[k] = temp;
               }
        return deck[j];
      }
//---------------------------------------------------------------------------------------------
random = arr[rand() % (sizeof(arr) / sizeof(arr[0]))];



//______________________________________________________________________________________________
int main()
      {
      game mygame;
      return 0;
      }


95 untitled2.cpp
error: no match for 'operator=' in '((game*)this)->game::player = p'

17 untitled2.cpp
note: candidates are: card& card::operator=(const card&)

95 untitled2.cpp
error: no match for 'operator=' in '((game*)this)->game::npc = n'

17 untitled2.cpp
note: candidates are: card& card::operator=(const card&)

133 untitled2.cpp
error: expected `;' before ')' token

140 untitled2.cpp
error: no matching function for call to `card::display(card&)'

i could change p & n to card too? but that would mess up the way it chooses the card in an array right... because it has to be an int to find the right index that the computer gave it 0-52 (51?)

(I am sorry I am tired so I might catch things slowly, I'm not usually like this)

tinanewtonart
Light Poster
42 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

I'm not sure you need line 94 for anything. If you really need numbers for the players make those separate. I think you were passing those numbers into 140 and 141 when they are really not necessary, since your card object should call display on itself rather than having anything to do with the players (so as in npc.display() -- without the cout since your method returns void). for (p == n ) EDIT: I think you meant if. But what if the n and p come up the same again? Use a loop.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: