1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Card GAme High low-- Homework that is due soon... less than two hours please help!

Discussion in 'C' started by tinanewtonart, Apr 24, 2010.

  1. tinanewtonart

    tinanewtonart New Member

    Joined:
    Apr 24, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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



    [FONT=Arial, sans-serif]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. [/FONT]


    [FONT=Arial, sans-serif]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. [/FONT]


    [FONT=Arial, sans-serif]You [/FONT][FONT=Arial, sans-serif]must[/FONT][FONT=Arial, sans-serif] have 2 classes in your program. One class will be an individual card. The second class will be the game, which [/FONT][FONT=Arial, sans-serif]must[/FONT][FONT=Arial, sans-serif] 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 [/FONT][FONT=Arial, sans-serif]must[/FONT][FONT=Arial, sans-serif] 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 [/FONT][FONT=Arial, sans-serif]must[/FONT][FONT=Arial, sans-serif] 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. [/FONT]


    [FONT=Arial, sans-serif]You [/FONT][FONT=Arial, sans-serif]must[/FONT][FONT=Arial, sans-serif] 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.[/FONT]


    [FONT=Arial, sans-serif]Sample Output:[/FONT]


    [FONT=Courier New, monospace]Welcome to Highest Card Wins. You will pick a card in the deck[/FONT]
    [FONT=Courier New, monospace]and I will pick a card out of the deck. The player with the[/FONT]
    [FONT=Courier New, monospace]highest card face value will win![/FONT]


    [FONT=Courier New, monospace]What card out of the deck would you like to choose? (1 to 52) 0[/FONT]
    [FONT=Courier New, monospace]Sorry, that is not a valid card number.[/FONT]


    [FONT=Courier New, monospace]Would you like to play again? (Y/N) y[/FONT]


    [FONT=Courier New, monospace]What card out of the deck would you like to choose? (1 to 52) 5[/FONT]


    [FONT=Courier New, monospace]Your card is: 7 of spades[/FONT]
    [FONT=Courier New, monospace]My card is : 7 of diamonds[/FONT]
    [FONT=Courier New, monospace]It's a tie.[/FONT]


    [FONT=Courier New, monospace]Would you like to play again? (Y/N) y[/FONT]


    [FONT=Courier New, monospace]What card out of the deck would you like to choose? (1 to 52) 50[/FONT]


    [FONT=Courier New, monospace]Your card is: 3 of spades[/FONT]
    [FONT=Courier New, monospace]My card is : 4 of hearts[/FONT]
    [FONT=Courier New, monospace]Sorry you lose.[/FONT]


    [FONT=Courier New, monospace]Would you like to play again? (Y/N) y[/FONT]


    [FONT=Courier New, monospace]What card out of the deck would you like to choose? (1 to 52) 21[/FONT]


    [FONT=Courier New, monospace]Your card is: jack of clubs[/FONT]
    [FONT=Courier New, monospace]My card is : 10 of spades[/FONT]
    [FONT=Courier New, monospace]You win![/FONT]


    [FONT=Courier New, monospace]Would you like to play again? (Y/N) n[/FONT]
     

Share This Page