comparing multiple values...

C

Chaz

Hello, I hope someone can help me out. I am going to be taking the
third step in a programming class soon(I took the previous two a while
ago at a different school) and in an effort to get back up to speed, I
bought the Dietel book How To Program 5th ed. I am going through some
examples trying to work them out, and have been doing pretty good, but
came across one exercise that has me stumped. It's a card shuffling
program that deals 5 cards, and stores the face value and suit in
arrays respectively. I have pasted the code which I hope shows that.

// to deal only 5 cards for a poker hand
for ( int card = 1; card <= 5; card++ )
{
// loop through rows of deck
for ( int row = 0; row <= 3; row++ )
{
// loop through columns of deck for current row
for ( int column = 0; column <= 12; column++ )
{
// if slot contains current card, display card
if ( deck[ row ][ column ] == card )
{
cout << setw( 5 ) << right << face[ column ]
<< " of " << setw( 8 ) << left << suit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
} // end if
} // end innermost for
} // end inner for
} // end outer for

What this example asks to do is to modify the program so that it can
determine if the hand has a pair. (and three of a kind and so on)
Considering the "cards" are stored in arrays, how would you compare all
5 face values for example and be able to tell their are two of a kind?

Thanks for taking the time to look at this and any help you can
provide.

Chaz
 
J

Jack Klein

Hello, I hope someone can help me out. I am going to be taking the
third step in a programming class soon(I took the previous two a while
ago at a different school) and in an effort to get back up to speed, I
bought the Dietel book How To Program 5th ed. I am going through some
examples trying to work them out, and have been doing pretty good, but
came across one exercise that has me stumped. It's a card shuffling
program that deals 5 cards, and stores the face value and suit in
arrays respectively. I have pasted the code which I hope shows that.

// to deal only 5 cards for a poker hand
for ( int card = 1; card <= 5; card++ )
{
// loop through rows of deck
for ( int row = 0; row <= 3; row++ )
{
// loop through columns of deck for current row
for ( int column = 0; column <= 12; column++ )
{
// if slot contains current card, display card
if ( deck[ row ][ column ] == card )
{
cout << setw( 5 ) << right << face[ column ]
<< " of " << setw( 8 ) << left << suit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
} // end if
} // end innermost for
} // end inner for
} // end outer for

What this example asks to do is to modify the program so that it can
determine if the hand has a pair. (and three of a kind and so on)
Considering the "cards" are stored in arrays, how would you compare all
5 face values for example and be able to tell their are two of a kind?

Thanks for taking the time to look at this and any help you can
provide.

Chaz

I'll give you a suggestion, not the code.

Create an array of 13 ints, one for each face value, and initialize
all the elements of this array to 0. Decide on a mapping between the
13 different card faces, if the program does not provide one already.

Now look at each of the 5 cards in the hand. For each card, increment
the element of the array that corresponds to its face value.

Final step, loop through the 13 elements of the array. If any element
has the value 2, you have a pair of the corresponding card face. If
any element has 3, you have three of a kind, a 4 means four of a kind,
and a 5 means you are playing with a crooked deck.
 
C

Chaz

Jack,

Thanks, I think I follow.

The program maps the cards in a 2-d array, 4x13. Normally, it shuffles
all 52 cards, and randomly selects a number between 1-52 and puts it
into an element on the array. I changed it so that instead of it
randomly selecting 52 cards, it only selects 5 as to select 5 cards(I
would post the code but I am at work and don't have it with me).

I will work on it tonight and see what I come up with.

Thanks again.

Chaz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top