Thread: A function to find matching characters

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    Lightbulb A function to find matching characters

    I am making a poker scoring system. One way I thought of doing it was to be able to identify if each card ( 3h - 3 of hearts, 6c - 6 of clubs etc etc) has matching integers or characters. Then use this to say if there is 5 matching integers then display flush. I have looked on this forum for answers and can't seem to find anything that really helps. If this is not possible what would be the best way to design this, bubble sort maybe?
    thanks

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Sorting is definitely the first step to take. Bubble sort is a nice and easy approach you can implement yourself.

    I did something like this not too long ago. While there are probably better ways of approaching this, my method included checking each hand for various conditions. If there was a flush, I'd set a "flush" flag. If there was a straight, I'd set a "straight" flag. Etc. I also checked for multiples (four of a kind, three of a kind, two of a kind/pair - note that it's possible for two pairs to be present). Then, based on the flags, I'd determine the hand, starting with checking for the best hand down to the least.

    If "straight" and "flush" and high card is Ace, then hand is "royal flush"
    Else if "straight" and "flush", then hand is "straight flush"
    ... etc

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thank you for your reply.
    What function do I use to search for multiples to identify hands?
    Also without making it too completely can within the bubble sort, is it possible to use scanf function, which would allow me/anyone to chance the cards in the input window.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    This is the code I have come up for for the bubble sort, however I am a little unsure on how to implement all five cards into being sorted.
    1. void sort( int array[], int n)
    2. {
    3. float card1, card2, card3, card4, card5

    4. for ( card1 = 0; card2 < n ; card1 ++ )
    5. for ( card2 = card1 + 1; card2 < n; card2 ++)
    6. for ( card3 = card2 + 1; card3 < n; card3 ++)
    7. for ( card4 = card3 + 1; card4 < n; card4 ++)
    8. if ( array[ card1 ] > array[ card2 ] {
    9. card3 = array[ card1 ];
    10. array[ card1 ] = array[ card2 ] ;
    11. array[ card2 ] = card3;

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What function do I use to search for multiples to identify hands?
    You'll have to figure out the necessary logic and write that function yourself.

    Also without making it too completely can within the bubble sort, is it possible to use scanf function, which would allow me/anyone to chance the cards in the input window.
    I don't understand what you're asking here. Can you please clarify?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This is the code I have come up for for the bubble sort, however I am a little unsure on how to implement all five cards into being sorted.
    This doesn't look quite right, but I applaud your efforts. Reading up on the bubble sort might help you with your code.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by SDH View Post
    I am making a poker scoring system. One way I thought of doing it was to be able to identify if each card ( 3h - 3 of hearts, 6c - 6 of clubs etc etc) has matching integers or characters.
    I don't know about the rules of poker, but it seems a useful intermediate step would be to represent a hand as you have described and then categorize it based on (1) card values and (2) suits. Based on the results of these categorizations you can determine if it is a flush, etc. I think a hand is 5 cards but for illustration suppose it is 7 for the example and represent card value 10 with character '0', Jack with 'J', and hearts, clubs, diamonds and spades each with their initial small letters).

    Code:
    char hand[7][2] = {
    "6s",
    "7h",
    "8d",
    "9c",
    "0s",
    "Jh",
    "Qd",
    }
    Then categorizing should based on card value should produce a result something like this:

    Code:
    aces: 0
    twos: 0
    threes: 0
    fours: 0
    fives: 0
    sixes: 1
    sevens: 1
    eights: 1
    nines: 1
    tens: 1
    jacks: 1
    queens: 1
    kings: 0
    Categorizing by suit:

    Code:
    spades: 2
    hearts: 2
    diamonds: 2
    clubs: 1
    Use the result to determine the total value of the hand. Personally I don't see how sorting will help you here so I don't understand the comment about bubble sort. The number of cards to test is no greater than 10. Sorting does not help to solve the problem and it does not improve the efficiency.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No matching function for call to...
    By 20120903 in forum C Programming
    Replies: 13
    Last Post: 09-07-2012, 01:44 PM
  2. No matching function? [C++ Error]
    By GianluTB in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2012, 10:33 AM
  3. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  4. Program does not find any matching file
    By tilex in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 09:59 PM
  5. Replies: 5
    Last Post: 04-16-2004, 01:29 AM