help with vectors please.

This is a discussion on help with vectors please. within the C++ Programming forums, part of the General Programming Boards category; i'm trying to build a class that represents a shuffled deck of cards. i start by creating and ordered deck ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    22

    help with vectors please.

    i'm trying to build a class that represents a shuffled deck of cards.
    i start by creating and ordered deck but when i try to randomize it things start to go wrong and i cant figure why.

    Code:
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    #include "deck.h"
    
    //deck::deck();
    //{
    
    //}
    
    void deck::shuffle()
    {
    	//gernerate an ordered deck of cards
    	for(int i=0; i<4; i++)
    	{
    		for(int j=1; j<14; j++)
    		{
    			m_cards.push_back(i);
    			m_suits.push_back(i);
    			m_cards[i] = j;
    			m_suits[i] = int(i + 3);
    		//	cout<<m_cards[i]<<m_suits[i]<<endl;
    
    		
    		}
    	}
    	//randomize the deck
    	int randNum;
    	srand(5);
    	for(i = 52; i>0; i--)
    	{
    		randNum = rand() % i;
    		m_deck[i] = m_cards[randNum];
    		m_suitsArray[i] = m_suits[randNum];
    		m_cards.erase(m_cards.begin() + randNum);//erase the elements that resided at
    		m_suits.erase(m_suits.begin() + randNum);//loc randNumber so that no two cards 
    							   //can be put in the deck twice
    		cout<<m_deck[i]<<m_suitsArray[i]<<endl;
    	}
    
    
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    You could also consider an STL solution - random_shuffle......looks like that's just what you are looking for!

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <vector>
    #include <algorithm>
    
    void Print(const std::vector<int>& v)
    {
    	for(std::vector<int>::const_iterator i = v.begin();
    		i < v.end();++i)
    		std::cout << *i << " ";
    
    	std::cout << std::endl;
    }
    
    int main(void)
    {	
    	const int MaxNumbers = 10;
    	std::vector<int> v;
    	std::srand(std::time(0));
    	for(int i = 0;i < MaxNumbers;++i)
    		v.push_back(i + 1);
    
    	Print(v);
    
    	std::random_shuffle(v.begin(),v.end());
    
    	Print(v);
    
    	std::random_shuffle(v.begin(),v.end());
    
    	Print(v);
    }

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    If so, move the srand(5) call to the start of main(), and ensure that it is only called ONCE
    No, i just put in srand(5) as a quick test. The problem is that duplicate cards end up in the deck.

    You could also consider an STL solution - random_shuffle......looks like that's just what you are looking for!
    thanks i'll try it out. btw, wich header contains the random suffle method?

    but i'd still like to know why my version dosn't work if any one cares to take a guess.
    Last edited by axlton; 08-03-2003 at 05:02 PM.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by axlton
    btw, wich header contains the random suffle method?

    #include <algorithm> - as in my example

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    It looks like the problem in your code is that you aren't understanding what push_back does. It doesn't create a spot at the index of the int you pass in. It just creates a new entry in the vector at the end and initializes the value at that entry to the number you pass in. Because of this, all you need to do to initialize your ordered deck of cards is to push_back each number like below:
    Code:
    //generate an ordered deck of cards
    for(int i=0; i<4; i++)
    {
    	for(int j=1; j<14; j++)
    	{
    		m_cards.push_back(j);	// switched to j
    		m_suits.push_back(i);
    //			m_cards[i] = j;
    //			m_suits[i] = int(i + 3);
    	}
    }
    Last edited by jlou; 08-03-2003 at 06:28 PM.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Later, you attempt to add these things to the "shuffled deck" m_deck. I don't know if you are initializing the size of m_deck and m_suitsArray elsewhere in your code, but if you're not, then your shuffle code won't work. You need to either push_back the randomly picked cards, or size/resize those two vectors to 52 before setting the entries in the shuffle loop.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    22
    thanks, although i've acutally found a much easier solution using an array of structs rather than vectors i plan to play around with this some more just so i can understand vectors better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

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