Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Card GAme High low-- Homework that is due soon... less than two hours please help! (http://www.go4expert.com/forums/card-game-low-homework-due-soon-hours-t21878/)

tinanewtonart 24Apr2010 17:50

Card GAme High low-- Homework that is due soon... less than two hours please help!
 
We got to get this to compile and I really need your assistance!
I got sick last week and missed the lecture on these subjects and it took all night but here it is..... almost done.... but the assignment specs below say that it HAS TO COMPILE OR I FAIL!:nonod::disappoin:cryin:
Code:

#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;//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];
                  }

                }
            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.n > npc.n)
                {
                cout << "player's card= ";
                player.display();
                cout <<endl;
                cout<< " is higher than the Computer's Card= ";
                npc.display();
                cout <<"\n you Win!"<<endl;
                }
              if(player.number < npc.number)
                {
                cout << "player's card= ";
                player.display();
                cout <<endl;
                cout<< " is lower than the Computer's Card= ";
                npc.display();
                cout <<"\n you Lose!"<<endl;
                winlose = 'l';
                }
              }
            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(deck[j]);
      //card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
      game mygame; // creates the game()
      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.

      }

148 untitled2.cpp
error: 'class card' has no member named 'n'
148 untitled2.cpp
error: 'class card' has no member named 'n'
19 untitled2.cpp
error: `int card::number' is protected
157 untitled2.cpp
error: within this context
209 untitled2.cpp
error: `deck' was not declared in this scope
209 untitled2.cpp
error: `j' was not declared in this scope


************************************************** **********
requirements



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


All times are GMT +5.5. The time now is 17:52.