Menu

Friday, 5 December 2014

Chapter 7,Problem 6:Object Oriented Programming by Robert Lafore in C++ Solution Manual

//Ahmad Furqan Attari
//P7.6
#include 
#include  //for srand(), rand()
#include  //for time for srand()
#include  //for _getch()
using namespace std;
enum Suit { clubs, diamonds, hearts, spades };
//from 2 to 10 are integers without names
const int jack = 11;
const int queen = 12;
const int king = 13;
const int ace = 14;
////////////////////////////////////////////////////////////////
class card
{
private:
 int number; //2 to 10, jack, queen, king, ace
 Suit suit; //clubs, diamonds, hearts, spades
public:
 card() //constructor
 { }
 void set(int n, Suit s) //set card
 {
  suit = s; number = n;
 }
 void display(); //display 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(5); break;
 case diamonds: cout << static_cast(4); break;
 case hearts: cout << static_cast(3); break;
 case spades: cout << static_cast(6); break;
 }
}
////////////////////////////////////////////////////////////////
void main(void)
{
 card deck[52];
 int j;
 cout << endl;
 for (j = 0; j<52; j++) //make an ordered deck
 {
  int num = (j % 13) + 2; //cycles through 2 to 14, 4 times
  Suit su = Suit(j / 13); //cycles through 0 to 3, 13 times
  deck[j].set(num, su); //set card
 }
 cout << "\nOrdered deck : \n";
 for (j = 0; j<52; j++) //display ordered deck
 {
  deck[j].display();
  cout << " ";
  if (!((j + 1) % 13)) //newline every 13 cards
   cout << endl;
 }
 srand(time(NULL)); //seed random numbers with time
 for (j = 0; j<52; j++) //for each card in the deck,
 {
  int k = rand() % 52; //pick another card at random
  card temp = deck[j]; //and swap them
  deck[j] = deck[k];
  deck[k] = temp;
 }
 cout << "\nPlayers' Hands : \n";
 cout << "Player 1:";
 for (j = 0; j<52; j++) //display shuffled deck
 {
  deck[j].display();
  cout << ", ";
  if (!((j + 1) % 13)) //newline every 13 cards
  {
   cout << endl;
   if (j<51)
    cout << "Player " << (j + 1) / 13 + 1 << ":";
  }
 }
 _getch();
} //end main

4 comments:

  1. Bhai chptr 6 problem # 7 ma loop terminate nhi hota while(ch!='n'); .Plz check the problem and tell.

    ReplyDelete
    Replies
    1. It's working fine
      All the solutions on this blog are tested and I tested it again
      make sure your caps Lock is off or you can change it to while(ch!='n' && ch!='N')

      Delete
    2. Hi you forgot the standar libraries: iostream, cstdlib, ctime and conio.h. Also need to add the type variables in the switch statement:
      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;
      }
      After this corrections works perfectly.

      Despite it works, I've been thinking about the solution and I believe that a two dimentions array will be better, but I couldn't pass it to code yet.

      If you can help me with my idea I will apreciate it.

      Best regards.-

      Gustavo

      Delete
    3. Hi! Gustavo
      Sorry for late reply that's because I shifted this site to http://code.electrocomputing.com
      You are right type is necessary
      but this happens sometimes when I copy the code inside here some words like char etc. don't appear in text.
      Which are variable are you suggesting to convert to two dimentional.

      Delete