Card shuffle and deal want to put on Stack

This is a discussion on Card shuffle and deal want to put on Stack within the C++ Programming forums, part of the General Programming Boards category; Here is my card shuffling and dealing but I want to shuffle and push onto a stack and then deal ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    17

    Card shuffle and deal want to put on Stack

    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;
    
    
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Did you write DeckofCards.cpp? Just wondering.

    What are you having trouble with? You just have to use a stack to hold the results of calls to a function like deal.

    Are you having trouble implementing a stack?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Here is my card shuffling and dealing but I want to shuffle and push onto a stack and then deal off of the stack.
    You can use a vector (maybe with an index variable) or an array for that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    17
    Do I put the stack in the deal function? Instead of cout do I use a push onto a stack?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Just make the deck a stack and pop 5 cards off and there is your hand. If you use a vector or an array, it'd be easy to shuffle in the stack itself. When the hand is over, just reset the current element on the stack to the size and you have a full deck again. Reshuffle and deal again.

    Basically, your shuffle should consist of a for loop that iterates through the elements of your array and swaps the current element with a random element. Then your deal function should just consist of popping five cards off the stack for each player.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shuffle and Deal Cards
    By killmequick in forum C# Programming
    Replies: 4
    Last Post: 03-21-2008, 01:30 AM
  2. Shuffle Cards and Deal
    By killmequick in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2008, 10:53 PM
  3. Segmentation Fault - aaaaaaaah!
    By yogibear in forum C Programming
    Replies: 6
    Last Post: 10-01-2007, 03:21 AM
  4. Deck Shuffle
    By pjharris in forum C++ Programming
    Replies: 51
    Last Post: 01-06-2006, 03:59 PM
  5. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21