Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Beat w/ Stick

macrumors newbie
Original poster
Aug 30, 2008
15
0
Hello, I am teaching myself to program, and as part of a project I am working on, I used this code to create a deck of cards and then randomly move cards to 2 hands.
Code:
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <string>
#include <vector>
using namespace std;
int i,l,j;
string Deck [52] = {"Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades",
	"Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades",
	"Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades",
	"King of Spades",
	
	"Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts",
	"Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts",
	"Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts",
	"King of Hearts",
	
	"Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs",
	"Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs",
	"Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs",
	"King of Clubs",
	
	"Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds",
	"Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds",
	"Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds",
	"King of Diamonds"}; //the starting deck
int main ()
{
	int n,m;
	string k;
	vector<string> first;
	for (j=0; j<52; j++) {
		first.push_back (Deck[j]);//turning the deck into a vector
	}
	vector<string> Hand1 ;
	vector<string> Hand2 ;
	srand ( time(NULL) );
	while (first.size()>0)
	{
	n = rand() % (first.size()-1); //puts a card into Hand1, then deletes it from the deck
	first.at(n)=k;
	Hand1.push_back (k);
	first.erase (first.begin()+(n-1));
	
	m = rand() % (first.size()-1); //ditto with Hand2
	first.at(m)=k;
	Hand2.push_back (k);
	first.erase (first.begin()+(m-1));
	}
	cout << "The contents of Hand 1 is:"; //outputs the contents of each hand
	for (i=0; i < Hand1.size(); i++){
		cout << " " << Hand1[i];}
	
	cout << "/n.The Contents of Hand 2 is:";
	for (l=0; l < Hand2.size(); l++) {
		cout<< " " << Hand2[l];}
	return 0;
	
}
Hitting run in XCode says there are no errors.
However, when i compile this in XCode, it gives me:
Code:
Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
I looked into the debugger and saw it to be way over my head. Any help?
 
I don't know the line number. I was hoping somebody could compile this themself, so they would be in the same situation i am. If you do that, you have all the info you need. (thanks for the quick reply)
 
I'm going to be (perhaps) rude and post my own rendition of your project. Your post implies a larger project going forward and I thought perhaps a different way of representing your cards might allow you to simplify your on going project.

So, no disrespect meant.

Happy Holidays

Code:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

typedef std::vector<int>            collection_t;
typedef std::vector<int>::iterator  collection_itr;

const char*     szSuiteNames[] =
{
    "Spades", "Hearts", "Clubs", "Diamonds"
};

const char*     szValueNames[] =
{
    "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"
};

const int       kSUITES             = (sizeof(szSuiteNames) / sizeof(szSuiteNames[0]));
const int       kCARDS_PER_SUITE    = (sizeof(szValueNames) / sizeof(szValueNames[0]));
const int       kCARDS_PER_DECK     = kSUITES * kCARDS_PER_SUITE;

collection_t    deck;
collection_t    hand1, hand2;

inline const char* value_name(int n)    { return szValueNames[n % kCARDS_PER_SUITE]; }
inline const char* suite_name(int n)    { return szSuiteNames[n / kCARDS_PER_SUITE]; }

int main()
{
    using std::cout;
    using std::endl;

    // initialize the deck of cards, represent as 'int' 0 - 51 ...
    // ... dump the initialized deck ...
    for ( int i = 0; i < kCARDS_PER_DECK; i++ ) { deck.push_back(i); }
    
    cout << "The contents of the deck is:";
    for ( collection_itr itr = deck.begin(); itr != deck.end(); itr++ )
    {
        cout << "\n\t" << value_name(*itr) << " of " << suite_name(*itr);
    }
    
    cout << "\n\n";
    
    // ... shuffle and dump the shuffled deck contents ...
    random_shuffle(deck.begin(), deck.end());
    
    cout << "The contents of the shiffled deck is:";
    for ( collection_itr itr = deck.begin(); itr != deck.end(); itr++ )
    {
        cout << "\n\t" << value_name(*itr) << " of " << suite_name(*itr);
    }
    
    cout << "\n\n";
    
    // ... there are an even number of cards in the deck so give every other one to each of the two players ...
    while ( deck.size() )
    {
        hand1.push_back(deck.back());   deck.pop_back();
        hand2.push_back(deck.back());   deck.pop_back();
    }
    
    // ... dump each players hand
    cout << "The contents of Hand 1 is:";
    for ( collection_itr itr = hand1.begin(); itr != hand1.end(); itr++ )
    {
        cout << "\n\t" << value_name(*itr) << " of " << suite_name(*itr);
    }
    
    cout << "\n\n";

    cout << "The contents of Hand 2 is:";
    for ( collection_itr itr = hand2.begin(); itr != hand2.end(); itr++ )
    {
        cout << "\n\t" << value_name(*itr) << " of " << suite_name(*itr);
    }

    cout << endl;
    
    return EXIT_SUCCESS;
}
 
Last edited:
that's not rude at all, thank you.
I did end up fixing the problem, turns out i had ignored occam's razor and made it too complicated. Also the problem with
Code:
first.erase(first.begin() + (n - 1));
was that often either n or m would be 0. i fixed that.
however, i ended writing a big chunk of code for assigning a value to cards, but this system should make that a lot easier. thanks much, happy holidays.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.