Website Design United States, Website Design California, Website Designing United States, Website Designing California

A deck class for a traditional card game - C++

This is the Deck class for a traditional card game. I created this in college to make a simple game of War.

You will need the following two classes to make this class work.

Random Integer

Card Class
//This is the header file, be sure to name this deck.h
// this class models a deck of cards

#ifndef _DECK_H_
#define _DECK_H_

#include "card.h"

class deck
{
public:
// constructor
deck( ); // a 52 card deck of cards

// operations on a deck
void shuffle ( ); //randomly change the order of the cards
bool is_empty( ); // true if empty, false if not
card draw ( ); // return the next card

protected:
card cards[ 52 ]; // the collection of cards
int top_card; // holds index one larger than next available card
};

#endif

//This is the class file, be sure to name this deck.cpp
//
// File: deck.cpp
// Folder: war4
//
// implementation of the deck of cards simulation

#include <algorithm>
#include "deck.h"
#include "random_integer.h"

using namespace std;

// constructor

deck::deck( ) {
// initialize a deck by creating all 52 cards
// ACE is now a value of 14 instead of 1
top_card = 0;
for( int i = 2; i <= 14; i++ )
{
card c1( diamond, i ),
c2( spade, i ),
c3( heart, i ),
c4( club, i );

cards[ top_card++ ] = c1;
cards[ top_card++ ] = c2;
cards[ top_card++ ] = c3;
cards[ top_card++ ] = c4;
}

}

// create a global object of type random_integer

random_integer randomizer;

void deck::shuffle( ) {
// randomly shuffle the cards array
// using the generic algorithm random shuffle
random_shuffle(cards, cards+52, randomizer );
}

// draw the next card from the deck (if one is there)

card deck::draw( ) {
if( !is_empty() ) {
return cards[ --top_card ];
} else {
card non_card( none, 0 );
return non_card;
}
}

// test if the deck is empty
bool deck::is_empty( ) {
return (top_card <= 0);
}


WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com