View Single Post
Old 08-14-2005, 08:43 PM   #14 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
To get you started, here is some working code. Your job is to excercise with it.
1) Show the original contents of the Deck immediatly after the shuffle.
2) Show the contents of the Deck after the player grabbed the top card from the deck.
3) Show what the player has in his/her hand.
4) Do this all in main.cpp.
Then we talk a bit about engines. Does it sound good to you?

Card.h

Code:
#ifndef CARD_H
#define CARD_H

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Card 
{ 
public:
  Card(pair<string, unsigned> crd) : aCard(crd) {}  
  friend ostream &operator<<(ostream &os, Card const &c);
  
public:
  string getSuit() const { return aCard.first;  }
  unsigned getValue() const { return aCard.second; }
  pair<string, unsigned> getCard() const { return aCard; }
private:  
  pair<string, int> aCard;   
};



#endif //CARD_H
Card.cpp
Code:
 #include "card.h"

ostream &operator<<(ostream &os, Card const &c) 
{
  cout<<resetiosflags(ios::right);
  cout<<setiosflags(ios::left);  
  
  string sName;
  char suitName[6];
  switch(c.aCard.second)  
  {
  default:
    itoa(c.aCard.second, suitName, 10); break;
  case 11:
    strcpy(suitName, "Jack"); break;
  case 12:
    strcpy(suitName, "Queen"); break;
  case 13:
    strcpy(suitName, "King"); break;
  case 14:
    strcpy(suitName, "Ace"); break;
  }
  sName = suitName;
  int len = c.aCard.first.length()+sName.length()+1;
  cout<<setw(40-len/2)<<""<<c.aCard.first<<" "<<suitName;
  
  resetiosflags(ios::left);
  return os;
}
Deck.h
Code:
 #ifndef DECK_H
#define DECK_H

#include "card.h"
#include <ctime>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>


using namespace std;

class Deck 
{ 
public:
  Deck();
  void CreateDeck();
  void RemoveTopCard();
  void Shuffle();
  void Show(ostream& os);
  vector<Card> getDeck() const;
  Card& operator [] (unsigned index);  
private:
    vector<Card> theDeck;  
};

#endif //DECK_H
Deck.cpp
Code:
 #include "Deck.h"

Deck::Deck() 
{ 
  CreateDeck();  
}

//--------------------------------------------------------

void Deck::CreateDeck()
{
  int cardtype;
    string suit;
    for (int i=0; i<52; i++)
    {
      // First 13 cards are Clubs, next 13 are Spades etc.
      cardtype = i/13;
      switch(cardtype)
      {
      case 0: suit = "Club";  break;
      case 1: suit = "Spade"; break;
      case 2: suit = "Diamond"; break;
      case 3: suit = "Heart"; break;
      }      
      theDeck.push_back(Card(pair<string,unsigned>(suit, 2+i%13) ));
    }
}

//-------------------------------------------------------

void Deck::RemoveTopCard()
{ 
  theDeck.erase(theDeck.begin()); 
}

//-------------------------------------------------------

void Deck::Shuffle()
{
  static bool seeded;
  if (!seeded)
  {
    srand(time(0));
    seeded = true;
  }
  random_shuffle(theDeck.begin(), theDeck.end());
}

//----------------------------------------------------------

void Deck::Show(ostream& os)
{
  copy(theDeck.begin(), theDeck.end(), ostream_iterator<Card>(os, "\n") );
}

//-----------------------------------------------------------

vector<Card> Deck::getDeck() const
{ 
  return theDeck; 
}

//-----------------------------------------------------------

Card& Deck::operator [] (unsigned index) 
{ 
  return theDeck[index]; 
}
player.h
Code:
 #ifndef PLAYER_H
#define PLAYER_H

#include "card.h"
#include "Deck.h"
#include <string>

class Player
{
public:
  Player();
  ~Player();
public:
  void grab_one_card(const Deck& ExDeck);
protected:
  std::pair<std::string, int> myCard_;
};

#endif // PLAYER_H
player.cpp
Code:
#include "player.h"

Player::Player()
{
  //ctor
}

//-----------------------------------------

Player::~Player()
{
  //dtor
}

//-----------------------------------------

void Player::grab_one_card(const Deck& ExDeck)
{
  myCard_.first = (*ExDeck.getDeck().begin()).getSuit();
  myCard_.second = (*ExDeck.getDeck().begin()).getValue();
}
main.cpp

Code:
#include <iostream>
#include "card.h"
#include "Deck.h"
#include "player.h"

int main()
{
  //First we create a deck and shuffle the cards in the deck.
  Deck theDeck;
  theDeck.Shuffle();
  
  //Then we create a player.
  Player thePlayer;
  //Player grabs a card from the (main) Deck.
  thePlayer.grab_one_card(theDeck);
  //ENGINE removes the top card of the (main) Deck because player has one in hand.
  theDeck.RemoveTopCard();
  
  return 0;    
}
__________________

Last edited by Valmont; 08-15-2005 at 08:51 AM.
Valmont is offline   Reply With Quote