Monday, July 27, 2009

C++ question?

How could I create a program that would deal a deck of 52 cards into 4 random hands of 5 cards each and store them as an array? The hands would have to be sorted from the lowest (2) to highest (Ace) and be displayed listing the card face and suit, so basically the number and the suit (heart, spade etc).





The only viable example I could find was one using classes, and I haven't learned that or object oriented programming yet. I'm going to need to use "struct" but I'm not sure how to start. Could anyone give a clear example on how to do this. I can't seem to find information anywhere.....

C++ question?
Hi Ali,





I won't solve your problem entirely, but let me give you a few hints.





1) Your deck can simply be expressed by an array of size 52, containing the numbers from 0 to 51. Set it up with the numbers in order from zero to 51 - we will shuffle them next.





2) To shuffle the deck, just have a loop that loops over each card position in the array and exchange that card with another card at a random position in the array (hint: random(52) will be a useful function call).





3) Once you are done shuffling, the first five cards can be the first person's cards, next five the second, and so on.





4) Sort each person's hand of five cards in increasing order. Use a bubble sort for this:


http://en.wikipedia.org/wiki/Bubble_sort





5) Print out the content of each person's hand. The number is the number divided by 4 (0 corresponds to a 2, 1 to a 3, 12 to an ace), and the number mod (%) 4 corresponds to the suit (zero to diamonds, one for hearts, etc).





That should do it! Good luck!
Reply:Well, start with replacing class with struct. In C++ it is legal for a struct to have functions in its definition. Default access specifier for struct is, public.





I hope this helps you to start in the right direction.
Reply:First, please don't consider learning that much of C++ you need to implement a class as a headache (assuming you know c and struct)





Second, you really don't need to know all that object-oriented-programming thing for working with c++ source codes.





Have a look on these source codes, I wish they will help:


http://www.codeproject.com/csharp/pokerl...


http://www.codeproject.com/useritems/cri...
Reply:I would first create a "Card" struct (or class).





struct Card {


int suit;


int num;





// methods


friend std::ostream%26amp; operator%26lt;%26lt;(std::ostream%26amp;, const Card%26amp;); // need to implement this outside this struct


Card() {}// constructor


Card(int s, int n) : suit(s), num(n) {}// constructor


bool operator%26lt;(const Card%26amp; c) const {// need this for sort


// implement comparison


}


};





Once you have your Card struct defined, you can use STL containers or arrays to store them. If you need a storage with fixed number of Cards, you can use array.





Card hand1[5], hand2[5];





If the number of cards is variable, use STL containers.





vector%26lt;Card%26gt; deck;





For example, if you want to initialize the deck,





for (int s = 0; s %26lt; 4; ++s)


for (int n = 1; n %26lt;= 13; ++n)


deck.push_back(Card(s, n));





If you need to sort the deck,


sort(deck.begin(), deck.end());


No comments:

Post a Comment