0

I meant more like:

int getValue() { return number;}

(no implementation is needed)

#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)
       {suit = s; number =n;}

      int getValue() { return number;}
      friend class game;


      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;//shuffle
         int j;//shuffle
         int p;
         int n;
         card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
      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' ;
     char winlose = 'w';
     int t = 1; //turncounter
     //deck[j];
     //card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
     do
       {
            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];
                   player.getValue();
                   npc.getValue();
                   }

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

                   do   // if card has already been drawn
                       {
                        p = rand()%52;
                        n = rand()%52;
                        player = deck[p];
                        npc = deck[n];
                        }
                   while(p == n);
                   //cout<< card::display(player)<<endl;
                   //cout<< card::display(npc)<<endl;
                 }
              if(player.number > npc.number)
                {
                cout << "player's card= ";
                player.display();
                cout <<endl;
                cout<< " is higher than the Computer's Card= "<< p ;
                npc.display();
                cout <<"\n you Win!"<< n <<endl;
                system("pause");
                }
              if(player.number < npc.number)
                {
                cout << "player's card= ";
                player.display();
                cout <<endl;
                cout<< " is lower than the Computer's Card= " << n;
                npc.display();
                cout <<"\n you Lose!"<<endl;
                winlose = 'l';
                system("pause");
                }
              }
            while(winlose = 'w');
       }
       while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
       cout<< "Play Again?" << endl;
       cin>> cAns;
       system("pause");
    }


//---------------------------------------------------------------------------------------------
//--------------------------------------------------------------
void game::shuffle() // creates an ordered deck and shuffles it.
    {

        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]))];
//npc.display();
//player.display();



//______________________________________________________________________________________________
int main()
      {

      game shuffle();
      //card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
      game mygame; // creates the game()
      game play();
      cout <<"dumby"<<endl;
      system("pause");
      return 0;// I see what you mean by exploiting the function's returning nothing... remove the excess if and go right to the point after game finished.

      }

IT COMPILES NOW, but the game doesn't work

1

Everywhere you were accessing ".number" use your getter (like in lines 154 and 164). I don't know that you need the friend class bit up in the card class if you use the getter. Debugging the game itself is going to take time and making sure things are getting where they need to go. Start putting some cout statements around critical parts so you can see what the values are (like the cards you drew and the value of each, etc).

Votes + Comments
Great! We appreciate your help.
0

update
I FINISHED THE CODING AND IT RUNS
THANKS JONSCA

Everywhere you were accessing ".number" use your getter (like in lines 154 and 164). I don't know that you need the friend class bit up in the card class if you use the getter. Debugging the game itself is going to take time and making sure things are getting where they need to go. Start putting some cout statements around critical parts so you can see what the values are (like the cards you drew and the value of each, etc).

/******************************************************************************
* Program Card.cpp
* Programmer Christina Backlund
* Date : April 2010
* Purpose To suffle a deck of 56 cards and play high card low card with the computer
*acknowledgements: Object Oriented Programming in C++ (referred to as "textbook")
http://www.daniweb.com/forums/post1199816.html#post1199816
*********************************************************************************/
/*You can work on this some more and turn it in before class on Monday if you would like.

Some suggestions:

This shouldn't be in main but in one of your classes:

card deck[52];// creates an multi dimensional-array of Objects with the Class "card"

For me, this code does not compile because in main you don't have an object of type card or game. In main you should have one game object so:

game MyGame;

Then call functions like this:

MyGame.shuffle(); */






//***************************
#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)
       {suit = s; number =n;}

      int getValue()
      { return number;}
      friend class game;


      void display();         //display card


      //bool isEqual(card);     //same as another card?
   };
//--------------------------------------------------------------
void card::display()          //displays the card value and suit symbol
   {
   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;//shuffle
         int j;//shuffle
         int p;
         int n;

      public:
         card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
         game ()                 //constructor (no args)
            {  }
        // void players (int p, int n)
        //     {  player = p; npc = n; }
         int play();
         void shuffle();
         //void discard(int);
         //void draw(int);
         //friend void card::display();

      };
//---------------------------------------------------------------------------------------------
int game::play()
     {
     char winlose = 'w';
     do
       {
       //cout<< "game.play()"<<endl;

       //int t = 1; //turncounter
       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];
       //cout<<"Drawing random card"<<endl;
       do
         {
         //cout<< "same card-- re-drawing new cards"<<endl;
         p = rand()%52;
         n = rand()%52;
         player = deck[p];
         npc = deck[n];
         player.getValue();
         npc.getValue();
         }
       while(p == n);

       if(player.number > npc.number)
         {
         //cout<<"win/lose"<<endl;
         player.getValue();
         npc.getValue();
         cout << "\n\nPlayer's card: ";
         player.display();
         //cout <<endl;
         cout<< " \nis higher than the \nComputer's Card: ";
         npc.display();
         cout <<"\n\nAwesome!\n You Win!"<<endl;
         system("pause");
         }
       if(player.number < npc.number)
         {
         //cout<<"lose/win"<<endl;
         player.getValue();
         npc.getValue();
         cout << "\n\nPlayer's card: ";
         player.display();
         //cout <<endl;
         cout<< " \nis lower than the \nComputer's Card: ";
         npc.display();
         cout <<"\n\nYou Lose!"<<endl;
         winlose = 'l';
         }
       }
    while(winlose == 'w');
    return 0;
    }






//---------------------------------------------------------------------------------------------
//--------------------------------------------------------------
void game::shuffle() // creates an ordered deck and shuffles it.
    {
        //cout<<"shuffle()"<<endl;
        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]))];
//npc.display();
//player.display();

/********************************************************************************
* Function main()
parameters: n/a (null)
purpose:  the start of the program
//assignment notes: must be the smallest I can make it.
********************************************************************************/

//______________________________________________________________________________________________
int main()

      {
      char cAns = 'y' ;
      void rules();
      rules();

      do
          {


          //cout <<"Main()"<<endl;
          //BAD CODE card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
          game MyGame; // creates the game()
          MyGame.shuffle(); 
          MyGame.play();
          cout<< "\nPlay Again? (y/n): ";
          cin>> cAns;
          cout << endl;
          }
      while(cAns == 'y' || cAns == 'Y'); // when you run out of cards in deck.
      //if (cAns == 'n' || 'N')
      return 0;// I see what you mean by exploiting the function's returning nothing... remove the excess if and go right to the point after game finished.

      }
//////////////////////////////////////////////////////////////////////////////////////
/********************************************************************************
* Function rules()
parameters: n/a (null)
purpose: displaying the rules. made into a simple function to reduce the size of main()
********************************************************************************/
void rules()
     {
     cout << static_cast<char>(5);
     cout << static_cast<char>(4);
     cout<<" Welcome to Highest Card Wins. " ;
     cout << static_cast<char>(3);
     cout << static_cast<char>(6)<< endl;
     cout<< "\n\n\nRULES"<<endl;
     cout<<"You will pick a card in the deck"<<endl;
     cout<<"and I will pick a card out of the deck. " << endl;
     cout<<"The player with the highest card face value will win!" << endl;
     cout<<"The game ends when Player loses.\n\n"<< endl;
     cout<<"To Pick a Card....   ";
     system("pause");
     }

finished and works now I am turning it in.

Edited by tinanewtonart: n/a

0

Everywhere you were accessing ".number" use your getter (like in lines 154 and 164). I don't know that you need the friend class bit up in the card class if you use the getter. Debugging the game itself is going to take time and making sure things are getting where they need to go. Start putting some cout statements around critical parts so you can see what the values are (like the cards you drew and the value of each, etc).

hi there i was wondering if i was to add graphics to card game would u be able to guide me?

1

manishwadhwa22

I'm glad you got it (Jonsca's post) helpful. Please do not hijack another thread to ask your question. If you have any questions please ask. You are welcome to start your own threads.

Thread Closed.

Votes + Comments
Right back atcha
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.