Thread: A function to find matching characters

1. 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. 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

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. 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. 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.

6. 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.

7. Originally Posted by SDH
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.