Making card generator random and not repeat cards

This is a discussion on Making card generator random and not repeat cards within the C++ Programming forums, part of the General Programming Boards category; Hello im making a card game that deals cards for a poker game. My problem is that my card dealer ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    Making card generator random and not repeat cards

    Hello

    im making a card game that deals cards for a poker game. My problem is that my card dealer generates the same cards for both the player and the community cards. i was wondering how i could make it so that it wouldnt do that. If i could get any hints or something, it would be greatly appreciated. also im looking for a way to score cards
    Code:
    #include<iostream>
    #include<fstream>
    #include<ctime>
    #include<cstdlib>
    #include<string>
    using namespace std;
    
    /* 
    	Randall Foor
    	Texas Holdem Game
    	Last Edited:12/11/2010
    */
    
    /*******************************
    ********Functions***************
    *******************************/
    void shuffle( int wdeck[][13]);
    void deal2( const int wdeck[][13], const char *wface[],
    	 const char *wsuit[]);
    void dealcommunity( const int wdeck[][13], const char *wface[],
    	 const char *wsuit[]);
    void pair( int a[]);
    
    /**********************************
    ***********Classes*****************
    **********************************/
    class Playerinfo
    {
    public:
    	Playerinfo();
    	void getRecipients();
    	friend ostream& operator << (ostream&, Playerinfo&);
    private:
    	string name, city, state, zip;
    };
    
    Playerinfo::Playerinfo()
    {
    	name="";
    	city="";
    	state="";
    	zip="";
    }
    
    void Playerinfo::getRecipients()
    {
    	cout << "Name: ";
    	getline(cin, name);
    	cout << "City: ";
    	getline(cin, city);
    	cout << "State: ";
    	getline(cin, state);
    	cout << "Zip: ";
    	getline(cin, zip);
    }
    
    ostream& operator << (ostream& out, Playerinfo& info)
    {
    	out << info.name << endl << info.city << endl;
    	out << info.state << endl << info.zip << endl;
    	return out;
    }
    
    /*****************************************
    ****************Main**********************
    *****************************************/
    int main()
    {
    	const char *suit[4] = {" of Hearts", " of Diamonds", " of Clubs", " of Spades"};
    	const char *face[13] = {"A", "2", "3", "3", "5",
    		"6", "7", "8", "9", "10", "J", "Q", "K"};
    	ofstream myfile;
    	Playerinfo mine;
    	ofstream outFile;
    	outFile.open("Playerinfo.txt", ios::app);
    
    	myfile.open("numbers_file.txt",ios::trunc);					//opens and deletes everything from before
    	myfile.close();	
    
    
    	
    	int chooselevel;
    	char repeatgame;
    	
    	do{
    		int playersmoney=100;
    		/*
    		cout<<"Welcome to TEXAS HOLD'EM!\n\n";
    		cout<<"Rules:\n";
    		cout<<"you are dealt a hand of 2 cards and there are 5 cards total that can\nbe flipped after that ";
    		cout<<"once per turn a card will be flipped over. \nthe object is to beat the dealers hand to gain money until you reach\nthe amount of that level\n\n";
    		system("pause");
    		system("cls");
    		cout<<"Once you are dealt the cards, you have a choice of either\n1:Bet\n2:Folding\n3:Quit game\n"<<endl;
    		cout<<"If you lose all your money you automatically lose the game."<<endl;
    		cout<<"You will be asked for you name,city,state,and zip code for personal reasons\njust go with it!"<<endl;
    		
    		system("pause");
    		system("cls");
    		*/
    
    		/*
    		mine.getRecipients();
    		cout << mine;
    		outFile << mine;
    		*/
    
    
    
    		/*
    		cout<<"Choose a Level:\n\n";
    		cout<<"Level 1:make $100\n";
    		cout<<"Level 2:make $1000\n";
    		cout<<"Level 3:make $10000\n";
    		cout<<"Level 4:make $100000\n\n";
    
    		cout<<"Which level would you like<1-4>:\n";
    		cin>>chooselevel;
    		system("cls");
    
    		*/
    
    		
    		cout<<"                                   ";
    		cout<<"Your money:$"<<playersmoney<<"\n\n\n";
    		
    		
    		int deck[4][13] ={0};
    		srand( time(0));
    		shuffle(deck);
    		cout<<"Your Cards:"<<endl;
    		deal2(deck, face, suit);
    		cout<<"Community Cards:\n";
    		dealcommunity(deck, face, suit);
    
    
    
    		if(playersmoney==0)
    		{
    			cout<<"You dont have any more money...you lost"<<endl;
    			break;
    		}
    	
    	
    
    
    	
    		
    
    		cout<<"would you like to repeat game<Y or N>?";
    		cin>>repeatgame;
    		system("cls");
    	}while(repeatgame=='y'||repeatgame=='Y');
    	system("pause");
    	
    }
    /***********************************************
    ***************Functions***********************
    ***********************************************/
    void shuffle(int wdeck[][13])
    {
    
    	int row;
    	int column;
    	int card;
    
    	for(card = 1; card <= 52; card++) {
    		do {
    			row = rand() % 4;
    			column = rand() % 13;
    		}while(wdeck [row] [column] !=0);
    
    		wdeck [row] [column] = card;
    	}
    }
    
    void deal2(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    
    	for ( card = 1; card <= 2; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}
    }
    void dealcommunity(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    
    
    	for ( card = 1; card <= 3; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					
    					
    				}		
    			}
    		}
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't deal *both* the player cards and the community cards from the very top of the deck. The player might get cards 1 & 2, then the community cards get 3, 4, 5.

  3. #3
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    okay... I fixed that problem. I dont understand how to make it just draw one card after i have drawn the players and dealers cards. This is what i got so far
    Code:
    void dealcommunity(const int wdeck[][13], const char *wface[], 
    		  const char *wsuit[])
    {
    	int card;
    	int row;
    	int column;
    	int count=4;
    	
    
    	count+=count;
    	for ( card = 3; card <= count; card++ ) {
    		
    		for ( row = 0; row <= 3; row++ ){
    
    			for ( column = 0; column <=12; column++ ){
    
    				if ( wdeck[row][column] == card ) {
    
    					printf("%5s%-8s%\n", wface[column], wsuit[row],
    					card % 52 == 0 );
    					count++;
    					
    				}		
    			}
    		}
    	}
    }

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you want a real deck use list since it a fit data structure for a deck. You can then very easily draw the top card since you will be getting the top element of the list. It is easy to shuffle, easy to put a card in the bottom. In a sense everything becomes really easy.

    Having an int[][] for a deck is more complicated. It is vital for programming to map objects (like a deck) to suitable data structures which make sense and you don't need to do the virtual mapping in your head every time you deal with the object (deck). This just creates lots of nasty bugs and ugly error (or ugly bugs and nasty errors). Should be avoided like the plague. Except when you are seeking to over-optimize.

    Of course, if you have a very specific reason (i.e. not really making a real random deck) then that is another story...

Popular pages Recent additions subscribe to a feed

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