Here is my card shuffling and dealing but I want to shuffle and push onto a stack and then deal off of the stack. Any ideas?
DeckOfCards.h
Code:
class DeckOfCards
{
public:
DeckOfCards(); //constructor
void shuffle(); //shuffle
void deal(); //deal cards
private:
int deck[ 4 ][ 13 ]; //represent deck of cards
};
DeckofCards.cpp
Code:
#include <iostream>
using std::cout;
using std::left;
using std::right;
#include <iomanip>
using std::setw;
#include <cstdlib>
using std::srand;
#include <ctime>
using std::time;
#include "DeckOfCards.h"
DeckOfCards::DeckOfCards()
{
for ( int row = 0; row <= 3; row++ )
{
for ( int column = 0; column <= 12; column++ )
{
deck[ row ][ column ] = 0; //initialize slot of deck
}
}
srand( time( 0 ) );
}
void DeckOfCards::shuffle()
{
int row;
int column;
for ( int card = 1; card <= 52; card++ )
{
do
{
row = rand() % 4;
column = rand() % 13;
} while( deck[ row ][ column ] != 0 );
deck[ row ][ column ] = card;
}
}
void DeckOfCards::deal()
{
static const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" }; //suit array
static const char *face[ 13 ] =
{ "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
for( int card = 1; card <= 52; card++ )
{
for ( int row = 0; row <= 3; row++ )
{
for ( int column = 0; column <= 12; column++ )
{
if ( deck[ row ][ column ] == card )
{
cout << setw( 5 ) << right << face[ column ] << " of "
<< setw( 8 ) << left << suit[ row ] << ( card % 2 == 0 ? '\n' : '\t' );
}
}
}
}
}
main.cpp
Code:
#include <time.h>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "DeckOfCards.h"
using namespace std;
int main ()
{
DeckOfCards deckOfCards; //create deck of cards object
deckOfCards.shuffle(); //shuffle cards
deckOfCards.deal(); //deal cards
getch();
return 0;
}