Click to See Complete Forum and Search --> : Help W/Arrays


ambrai5
September 7th, 2002, 05:39 PM
Hey guys,
I'm very very new to C++ and very very confused. We have to write a program to deal five "cards" from a shuffled deck of 52 which I 've done but I can't figure out how to read those "cards" into array so I can compare them to see if there are two of kind, flush etc. I need to read them into a double subscripted array i.e. one column for suit ( Hearts, Diamonds etc) and one column for face value ( one, two, three) etc...this is the code I have so far...if anyone can help me that'd be awesome!!!!

John E
September 8th, 2002, 03:38 AM
You've more or less done this correctly but I'd suggest a slightly different strategy. Firstly, you've only read 5 cards into your array using the "shuffle" function - which is fine if you only assume one player - except that, as you discovered, it isn't easy to correlate the cards when you need to do this later. To shuffle the whole deck properly, you'd need to increase your loop to 52. Bear in mind however, that because your "shuffle" function relies on random number generation, it might take quite a long time to fill a 4 x 13 array! Once you've sorted that out, try the following...

When dealing the cards, as well as sending your output to cout, write the output to a new array which belongs only to the player. You could declare a separate array for each player. With each player (just like you've done in "shuffle") only put 5 cards into his array. I'd suggest that in your new arrays you should place a '1' in the dealt card, so that if five cards are dealt, that player's array will contain 5 x '1's (representing the dealt cards) and 47 x '0's. Each player would then end up having his own array containing 5 x '1's (in different places) and 47 x '0's. This will make life easier when you need to know which cards are held by each player. For example, if a player has three consecutive '1's in a particular row, then you know he's got a flush. If a '1' occurs in the same place in two columns then he's got a pair. If '1' occurs in the same place in three columns, he's got a prile etc.

If you do it this way, you'd also have to remember that each time you deal a card, you'll need to set its entry in wdeck to zero and test for this so that it can't be dealt again.

Good luck.

John E
September 8th, 2002, 05:32 AM
Oh, - and a couple of other things - Firstly, I'd suggest that you enumerate your face cards and suits, so that if a player is dealt, say, the queen of hearts you can do something like...

player1_array[Queen][Hearts] = TRUE;

which will set the cards in a player's array as he is dealt them.

Secondly, I'm sure I saw some source code once for a Solitaire type game. I can't remember whether it was on CodeGuru or some other forum but it might be worth your while to do a search.