Thread: Comparing elements in an array with eachother

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    45

    Comparing elements in an array with eachother

    Hi everyone,
    I'm trying to write a program that deals a 5 card poker hand and determines whether or not there is a pair and/or a flush. I have an array hand[] that the cards are dealt into and came up with his loop to see if there is a pair and/or flush. I realized after the first loop it will start comparing values in array positions that don't exist...I know there must be a simpler way of writing this, but I can't figure it out.

    Code:
    ..... 
    
         for (int k = 0; k <= 4; k++) {
                if ((hand[k].getValue() == hand[k+1].getValue()) || 
                    (hand[k].getValue() == hand[k+2].getValue()) ||
                    (hand[k].getValue() == hand[k+3].getValue()) ||
                    (hand[k].getValue() == hand[k+4].getValue()))
                    pair ++;
                if ((hand[k].getSuit() == hand[k+1].getSuit()) &&
                    (hand[k].getSuit() == hand[k+2].getSuit()) &&
                    (hand[k].getSuit() == hand[k+3].getSuit()) &&
                    (hand[k].getSuit() == hand[k+4].getSuit()))
                    flush ++;
            }
            
    .....
    Thanks for any help!
    -Ryan

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    First, sort the hand, like you do when you really play.
    Then you can find pairs and straights easily.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    45
    So I should sort the array in increasing order, then I can compare the first to the second, second to the third, etc with a loop? as for the flush, the code that I wrote works right?
    Thanks

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by ryanmcclure4 View Post
    So I should sort the array in increasing order, then I can compare the first to the second, second to the third, etc with a loop?
    Yeah, basically...

    Quote Originally Posted by ryanmcclure4 View Post
    as for the flush, the code that I wrote works right?
    Thanks
    Don't know. Didn't look at it -- [looking] -- Yep, that should do it...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ryanmcclure4 View Post
    as for the flush, the code that I wrote works right?
    No, it has a buffer overrun.
    k goes up to 4, and k+4 is then 8.

    You should either do this using nested loops where the inner one starts at the value of the outer one, or since I'm sure you've now realised that sorting the hand first may make it easier, just compare the kth card to the k+1th card, with going up to less than 4, with a single loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by iMalc View Post
    No, it has a buffer overrun.
    k goes up to 4, and k+4 is then 8.
    Ouch! Good catch. I didn't look close enough.

    Also, keep in mind that trips (three of a kind) will also register as 3 pairs. And 4 of a kind will be 6 pairs.

    One way to handle this is count all the pairs you find. If the count is
    1, 2, or 4 you have a pair.
    3 or 4 you have trips.
    6 you have quads.

    The count of 4 is actually a full house -- 3 from the trips and 1 from the pair.

    I can't think of a way to get 5....
    Last edited by WaltP; 09-15-2012 at 05:47 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Elements of 2 arrays
    By SupraMan in forum C Programming
    Replies: 28
    Last Post: 11-23-2010, 02:43 PM
  2. Replies: 4
    Last Post: 03-18-2009, 05:01 PM
  3. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  4. Comparing array elements
    By Tride in forum C Programming
    Replies: 8
    Last Post: 09-13-2003, 12:10 PM
  5. ? comparing elements of a char array
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2001, 07:55 AM